Добрый день. Изучая Hibbernate и Spring столкнулся с проблемой. Есть клас Car:
Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
| package com.validateproject;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import javax.persistence.*;
import javax.persistence.Id;
/**
* Created by Roland on 10/6/2015.
*/
@Entity
@Table(name = "car")
public class Car {
private Long id;
private String licensePlate;
private String manufacturer;
private DateTime manufactureDate;
private int age;
private int version;
public Long getId() {
return id;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public void setId(Long id) {
this.id = id;
}
public String getLicensePlate() {
return licensePlate;
}
@Column(name = "licence_plate")
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
public String getManufacturer() {
return manufacturer;
}
@Column(name = "manufacturer")
public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
}
public DateTime getManufactureDate() {
return manufactureDate;
}
@Column(name = "manufacture_date")
@Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
public void setManufactureDate(DateTime manufactureDate) {
this.manufactureDate = manufactureDate;
}
public int getAge() {
return age;
}
@Column(name = "age")
public void setAge(int age) {
this.age = age;
}
public int getVersion() {
return version;
}
@Version
public void setVersion(int version) {
this.version = version;
}
public String toString() {
return "License: " + licensePlate + " - Manufacturer: " + manufacturer
+ " - Manufacture Date: " + manufactureDate + " - Age: " + age;
}
} |
|
Файл конфигурации:
XML |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
| <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
[url]http://www.springframework.org/schema/beans/spring-beans-3.1.xsd[/url]
[url]http://www.springframework.org/schema/context[/url]
[url]http://www.springframework.org/schema/context/spring-context-3.1.xsd[/url]
[url]http://www.springframework.org/schema/jdbc[/url]
[url]http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd[/url]
[url]http://www.springframework.org/schema/tx[/url]
[url]http://www.springframework.org/schema/tx/spring-tx-3.1.xsd[/url]
[url]http://www.springframework.org/schema/data/jpa[/url]
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan"
value="com.validateproject"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.H2Dialect
</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<context:annotation-config/>
<jpa:repositories base-package="com.validateproject"
entity-manager-factory-ref="emf"
transaction-manager-ref="transactionManager"/>
<context:component-scan
base-package="com.validateproject"/>
</beans> |
|
Во время запуска программы получаю ошибку создания EnityManagerFactory, в корне которой лежит ошибка
Java |
1
| org.hibernate.AnnotationException: No identifier specified for entity: com.validateproject.Car |
|
Как видно из описания класса @Entity и @Id присутствуют.
Интересно то, что если перенести аннотации по определению основного ключа с set-ра на само поле, то данная ошибка исчезает.
Можете, пожалуйста, объяснить почему так происходит?