About the hibernate4.3.8final version (annotation) annotation problem is quite a bug.
The following annotations are provided in the official hibernate documentation:
5.1. Mapping declaration
Object/relational mappings can be defined in three approaches:
-
Using Java 5 annotations (via the Java Persistence 2 annotations)
-
Using JPA 2 XML deployment descriptors (described in chapter XXX)
-
Using the Hibernate legacy XML files approach known as hbm. xml
Annotations are split in two categories, the logical mapping annotations (describing the object model, the association between two entities etc .) and the physical mapping annotations (describing the physical schema, tables, columns, indexes, etc ). we will mix annotations from both categories in the following code examples.
JPA annotations are injavax.persistence.*Package. Hibernate specific extensions are inorg.hibernate.annotations.*. You favorite IDE can auto-complete annotations and their attributes for you (even without a specific "JPA" plugin, since JPA annotations are plain Java 5 annotations ).
Here is an example of mapping
package eg;@Entity @Table(name="cats") @Inheritance(strategy=SINGLE_TABLE)@DiscriminatorValue("C") @DiscriminatorColumn(name="subclass", discriminatorType=CHAR)public class Cat { @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } private Integer id; public BigDecimal getWeight() { return weight; } public void setWeight(BigDecimal weight) { this.weight = weight; } private BigDecimal weight; @Temporal(DATE) @NotNull @Column(updatable=false) public Date getBirthdate() { return birthdate; } public void setBirthdate(Date birthdate) { this.birthdate = birthdate; } private Date birthdate; @org.hibernate.annotations.Type(type="eg.types.ColorUserType") @NotNull @Column(updatable=false) public ColorType getColor() { return color; } public void setColor(ColorType color) { this.color = color; } private ColorType color; @NotNull @Column(updatable=false) public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } private String sex; @NotNull @Column(updatable=false) public Integer getLitterId() { return litterId; } public void setLitterId(Integer litterId) { this.litterId = litterId; } private Integer litterId; @ManyToOne @JoinColumn(name="mother_id", updatable=false) public Cat getMother() { return mother; } public void setMother(Cat mother) { this.mother = mother; } private Cat mother; @OneToMany(mappedBy="mother") @OrderBy("litterId") public Set<Cat> getKittens() { return kittens; } public void setKittens(Set<Cat> kittens) { this.kittens = kittens; } private Set<Cat> kittens = new HashSet<Cat>();}@Entity @DiscriminatorValue("D")public class DomesticCat extends Cat { public String getName() { return name; } public void setName(String name) { this.name = name } private String name;}Annotation code written by yourself:
Package org. hibernate. annotation. entity;
Import javax. persistence. Entity;
Import javax. persistence. Id;
Import javax. persistence. Table;
@ Entity (name = "_ teacher ")
Public class Teacher {
Private Integer id;
Private String name;
Private String title;
@ Id
Public Integer getId (){
Return id;
}
Public void setId (Integer id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public String getTitle (){
Return title;
}
Public void setTitle (String title ){
This. title = title;
}
}
If the difference is @ Entity (name = "_ teacher"), it can be used to annotate the table and object class names in the database.
When you are in hibernate4.3.8 or a later version, follow the instructions in the hibernate official documentation.
Package org. hibernate. annotation. entity;
Import javax. persistence. Entity;
Import javax. persistence. Id;
Import javax. persistence. Table;
@ Entity
@ (Table = "_ teacher ")
Public class Teacher {
Private Integer id;
Private String name;
Private String title;
@ Id
Public Integer getId (){
Return id;
}
Public void setId (Integer id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public String getTitle (){
Return title;
}
Public void setTitle (String title ){
This. title = title;
}
}
At this moment, the compilation is passed, but the following exception is always thrown during the runtime:
Java. lang. ExceptionInInitializerError
At org. hibernate. util. HibernateUtil. buildSessionFactory (HibernateUtil. java: 26)
At org. hibernate. util. HibernateUtil. <clinit> (HibernateUtil. java: 10)
At org. hibernate. test. HibernateTest. test2 (HibernateTest. java: 20)
At sun. reflect. NativeMethodAccessorImpl. invoke0 (Native Method)
At sun. reflect. NativeMethodAccessorImpl. invoke (NativeMethodAccessorImpl. java: 57)
At sun. reflect. DelegatingMethodAccessorImpl. invoke (DelegatingMethodAccessorImpl. java: 43)
At java. lang. reflect. Method. invoke (Method. java: 606)
At org. junit. runners. model. FrameworkMethod $1. runReflectiveCall (FrameworkMethod. java: 44)
At org. junit. internal. runners. model. ReflectiveCallable. run (ReflectiveCallable. java: 15)
At org. junit. runners. model. FrameworkMethod. invokeExplosively (FrameworkMethod. java: 41)
At org. junit. internal. runners. statements. InvokeMethod. evaluate (InvokeMethod. java: 20)
At org. junit. runners. ParentRunner. runLeaf (ParentRunner. java: 263)
At org. junit. runners. BlockJUnit4ClassRunner. runChild (BlockJUnit4ClassRunner. java: 69)
At org. junit. runners. BlockJUnit4ClassRunner. runChild (BlockJUnit4ClassRunner. java: 48)
At org. junit. runners. ParentRunner $ 3.run( ParentRunner. java: 231)
At org. junit. runners. ParentRunner $1. schedule (ParentRunner. java: 60)
At org. junit. runners. ParentRunner. runChildren (ParentRunner. java: 229)
At org. junit. runners. ParentRunner. access $000 (ParentRunner. java: 50)
At org. junit. runners. ParentRunner $2. evaluate (ParentRunner. java: 222)
At org. junit. runners. ParentRunner. run (ParentRunner. java: 292)
At org. eclipse. jdt. internal. junit4.runner. JUnit4TestReference. run (JUnit4TestReference. java: 50)
At org. eclipse. jdt. internal. junit. runner. TestExecution. run (TestExecution. java: 38)
At org. eclipse. jdt. internal. junit. runner. RemoteTestRunner. runTests (RemoteTestRunner. java: 467)
At org. eclipse. jdt. internal. junit. runner. RemoteTestRunner. runTests (RemoteTestRunner. java: 683)
At org. eclipse. jdt. internal. junit. runner. RemoteTestRunner. run (RemoteTestRunner. java: 390)
At org. eclipse. jdt. internal. junit. runner. RemoteTestRunner. main (RemoteTestRunner. java: 197)
Caused by: java. lang. NoSuchMethodError: javax. persistence. Table. indexes () [Ljavax/persistence/Index;
At org. hibernate. cfg. annotations. EntityBinder. processComplementaryTableDefinitions (EntityBinder. java: 973)
At org. hibernate. cfg. AnnotationBinder. bindClass (AnnotationBinder. java: 824)
At org. hibernate. cfg. Configuration $ metaperformancequeue. processAnnotatedClassesQueue (Configuration. java: 3845)
At org. hibernate. cfg. Configuration $ metaperformancequeue. processMetadata (Configuration. java: 3799)
At org. hibernate. cfg. Configuration. secondPassCompile (Configuration. java: 1412)
At org. hibernate. cfg. Configuration. buildSessionFactory (Configuration. java: 1846)
At org. hibernate. util. HibernateUtil. buildSessionFactory (HibernateUtil. java: 23)
... 25 more
-- Although the landlord has solved this problem, but for hibernate annotations, what is provided in the official documentation is not usable and cannot be understood. Please give us some advice and discuss it together