淺談其他的Hibernate元數(shù)據(jù)(Metadata)
Hibernate元數(shù)據(jù)有很多值得學(xué)習(xí)的地方,這里我們主要介紹其他Hibernate元數(shù)據(jù)(Metadata),包括介紹使用XDoclet 標(biāo)記等方面
其他Hibernate元數(shù)據(jù)(Metadata),XML 并不適用于所有人, 因此有其他定義Hibernate O/R 映射元數(shù)據(jù)(metadata)的方法。
1.Hibernate元數(shù)據(jù) 使用 XDoclet 標(biāo)記
很多Hibernate使用者更喜歡使用XDoclet@hibernate.tags將映射信息直接嵌入到源代碼中。我們不會在本文檔中涉及這個(gè)方法,因?yàn)閲?yán)格說來,這屬于XDoclet的一部分。然而,我們包含了如下使用XDoclet映射的Cat類的例子。
- package eg;
- import java.util.Set;
- import java.util.Date;
- /**
- * @hibernate.class
- * table="CATS"
- */
- public class Cat {
- private Long id; // identifier
- private Date birthdate;
- private Cat mother;
- private Set kittens
- private Color color;
- private char sex;
- private float weight;
- /*
- * @hibernate.id
- * generator-class="native"
- * column="CAT_ID"
- */
- public Long getId() {
- return id;
- }
- private void setId(Long id) {
- this.id=id;
- }
- /**
- * @hibernate.many-to-one
- * column="PARENT_ID"
- */
- public Cat getMother() {
- return mother;
- }
- void setMother(Cat mother) {
- this.mother = mother;
- }
- /**
- * @hibernate.property
- * column="BIRTH_DATE"
- */
- public Date getBirthdate() {
- return birthdate;
- }
- void setBirthdate(Date date) {
- birthdate = date;
- }
- /**
- * @hibernate.property
- * column="WEIGHT"
- */
- public float getWeight() {
- return weight;
- }
- void setWeight(float weight) {
- this.weight = weight;
- }
- /**
- * @hibernate.property
- * column="COLOR"
- * not-null="true"
- */
- public Color getColor() {
- return color;
- }
- void setColor(Color color) {
- this.color = color;
- }
- /**
- * @hibernate.set
- * inverse="true"
- * order-by="BIRTH_DATE"
- * @hibernate.collection-key
- * column="PARENT_ID"
- * @hibernate.collection-one-to-many
- */
- public Set getKittens() {
- return kittens;
- }
- void setKittens(Set kittens) {
- this.kittens = kittens;
- }
- // addKitten not needed by Hibernate
- public void addKitten(Cat kitten) {
- kittens.add(kitten);
- }
- /**
- * @hibernate.property
- * column="SEX"
- * not-null="true"
- * update="false"
- */
- public char getSex() {
- return sex;
- }
- void setSex(char sex) {
- this.sex=sex;
- }
- }
參考Hibernate網(wǎng)站更多的Xdoclet和Hibernate的例子
2. Hibernate元數(shù)據(jù)使用 JDK 5.0 的注解(Annotation)
JDK 5.0 在語言級別引入了 XDoclet 風(fēng)格的標(biāo)注,并且是類型安全的,在編譯期進(jìn)行檢查。這一機(jī)制比XDoclet的注解更為強(qiáng)大,有更好的工具和IDE支持。例如, IntelliJ IDEA,支持JDK 5.0注解的自動(dòng)完成和語法高亮 。EJB規(guī)范的新修訂版(JSR-220)使用 JDK 5.0的注解作為entity beans的主要元數(shù)據(jù)(metadata)機(jī)制。
Hibernate 3 實(shí)現(xiàn)了JSR-220 (the persistence API)的EntityManager,支持通過Hibernate Annotations包定義映射元數(shù)據(jù)。這個(gè)包作為單獨(dú)的部分下載,支持EJB3 (JSR-220)和Hibernate3的元數(shù)據(jù)。
這是一個(gè)被注解為EJB entity bean 的POJO類的例子
- @Entity(access = AccessType.FIELD)
- public class Customer implements Serializable {
- @Id;
- Long id;
- String firstName;
- String lastName;
- Date birthday;
- @Transient
- Integer age;
- @Embedded
- private Address homeAddress;
- @OneToMany(cascade=CascadeType.ALL)
- @JoinColumn(name="CUSTOMER_ID")
- Set<Order> orders;
- // Getter/setter and business methods
- }
注意:
對 JDK 5.0 注解 (和 JSR-220)支持的工作仍然在進(jìn)行中,并未完成。更多細(xì)節(jié)請參閱Hibernate Annotations 模塊。
【編輯推薦】