在hibernate官方文档中给出了 如下注解方式:
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 in the javax.persistence.*
package. Hibernate specific extensions are in
org.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; }
自己写出的的注解代码:
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;
}
}
差别 如果是@Entity(name="_teacher")这样去注解当database中的table与实体类名不一直的时候这样是可以通过的
而当你在hibernate4.3.8版本也就是4.0以后的版本中,你按照hibernate官方文档去注解
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;
}
}
此刻是编译通过了而在运行时却通过不了 一直抛以下异常:
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$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3845)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.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
--楼主虽然已经解决此问题,但是针对hibernate注解,官方文档给出的不能使用,一直不太明白,还请大神指点,共同探讨