5.5.1. 使用 XDoclet 标记

很多Hibernate使用者更喜欢使用XDoclet@hibernate.tags将映射信息直接嵌入到源代码中。我们不会在本文档中涉及这个方法,因为严格说来,这属于XDoclet的一部分。然而,我们包含了如下使用XDoclet映射的Cat类的例子。

  1. package eg;
  2. import java.util.Set;
  3. import java.util.Date;
  4. /**
  5. * @hibernate.class
  6. * table="CATS"
  7. */
  8. public class Cat {
  9. private Long id; // identifier
  10. private Date birthdate;
  11. private Cat mother;
  12. private Set kittens
  13. private Color color;
  14. private char sex;
  15. private float weight;
  16. /*
  17. * @hibernate.id
  18. * generator-class="native"
  19. * column="CAT_ID"
  20. */
  21. public Long getId() {
  22. return id;
  23. }
  24. private void setId(Long id) {
  25. this.id=id;
  26. }
  27. /**
  28. * @hibernate.many-to-one
  29. * column="PARENT_ID"
  30. */
  31. public Cat getMother() {
  32. return mother;
  33. }
  34. void setMother(Cat mother) {
  35. this.mother = mother;
  36. }
  37. /**
  38. * @hibernate.property
  39. * column="BIRTH_DATE"
  40. */
  41. public Date getBirthdate() {
  42. return birthdate;
  43. }
  44. void setBirthdate(Date date) {
  45. birthdate = date;
  46. }
  47. /**
  48. * @hibernate.property
  49. * column="WEIGHT"
  50. */
  51. public float getWeight() {
  52. return weight;
  53. }
  54. void setWeight(float weight) {
  55. this.weight = weight;
  56. }
  57. /**
  58. * @hibernate.property
  59. * column="COLOR"
  60. * not-null="true"
  61. */
  62. public Color getColor() {
  63. return color;
  64. }
  65. void setColor(Color color) {
  66. this.color = color;
  67. }
  68. /**
  69. * @hibernate.set
  70. * inverse="true"
  71. * order-by="BIRTH_DATE"
  72. * @hibernate.collection-key
  73. * column="PARENT_ID"
  74. * @hibernate.collection-one-to-many
  75. */
  76. public Set getKittens() {
  77. return kittens;
  78. }
  79. void setKittens(Set kittens) {
  80. this.kittens = kittens;
  81. }
  82. // addKitten not needed by Hibernate
  83. public void addKitten(Cat kitten) {
  84. kittens.add(kitten);
  85. }
  86. /**
  87. * @hibernate.property
  88. * column="SEX"
  89. * not-null="true"
  90. * update="false"
  91. */
  92. public char getSex() {
  93. return sex;
  94. }
  95. void setSex(char sex) {
  96. this.sex=sex;
  97. }
  98. }

参考Hibernate网站更多的Xdoclet和Hibernate的例子