1、使用XDoclet2+Ant实现Hibernate3的pojoxmldb代码相互转换使用XDoclet2+Ant实现Hibernate3的pojo-xml-db代码相互转换XDoclet是一款开源的代码自动生成引擎,支持很多框架的代码自动生成。而XDoclet2作为Maven2的插件,支持Hibernate3的pojo-xml的代码自动生成。配合ant与Hibernate Tool API,可以实现pojoxmldb schema的相互转化。具体关系为: 生成处理 实用工具 pojo-xml XDoclet2 xml-pojo Hibernate Tool API pojo+xml - db
2、schema Hibernate Tool API db schema - pojo+xml 可用myeclipse,本文不涉及下面通过ANT脚本例子,来展示如何完成以上的转换。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 28 29 30 31 32 33 34 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 71 72 73 74 75 76 77 脚本很详细,看注释即可。在使用xdo
3、clet2hbm前,需要在pojo上设定javadoc注释,以告诉xdoclet如何进行解析。one2one & one2many的例子:Child.java 1 package po; 2 3 /* 4 * hibernate.class table=child 5 */ 6 SuppressWarnings(serial) 7 public class Child implements java.io.Serializable 8 9 /xdoclet注释有写在getter上与写在field上2种方法。10 /写在field上会自动生成access=field,所以写在getter上更常用
4、一些。11 12 private String id;13 private String name;14 private Father father;15 16 /*17 * hibernate.id generator-class=uuid.hex column=id length=3218 */19 public String getId() 20 return this.id;21 22 23 public void setId(String id) 24 this.id = id;25 26 27 /基本支持所有的hibernate 属性,所以想要啥设置就大胆的写吧28 /*29 *
5、hibernate.property column=name length=32 not-null=true type=java.lang.String lazy=true30 */31 public String getName() 32 return this.name;33 34 35 public void setName(String name) 36 this.name = name;37 38 39 /*40 * hibernate.many-to-one column=father_id not-null=true41 */42 public Father getFather(
6、) 43 return this.father;44 45 46 public void setFather(Father father) 47 this.father = father;48 49 50 Father.java 1 package po; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 /* 7 * hibernate.class table=father 8 */ 9 SuppressWarnings(serial)10 public class Father implements java.io.Seri
7、alizable 11 12 private String id;13 14 private String name;15 16 private Integer age;17 18 private Set children = new HashSet(0);19 20 /*21 * hibernate.id generator-class=uuid.hex column=id length=3222 */23 public String getId() 24 return this.id;25 26 27 public void setId(String id) 28 this.id = id
8、;29 30 31 /*32 * hibernate.property column=name length=32 not-null=true type=java.lang.String33 */34 public String getName() 35 return this.name;36 37 38 public void setName(String name) 39 this.name = name;40 41 42 /*43 * hibernate.property column=age length=32 type=java.lang.Integer44 */45 public
9、Integer getAge() 46 return age;47 48 49 public void setAge(Integer age) 50 this.age = age;51 52 53 /*54 * hibernate.set table=child55 * hibernate.key column=father_id56 * hibernate.one-to-many class=po.Child57 */58 public Set getChildren() 59 return children;60 61 62 public void setChildren(Set chil
10、dren) 63 this.children = children;64 65 many2many的例子:Student.java 1 package po; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 /* 7 * hibernate.class table=student 8 */ 9 SuppressWarnings(serial)10 public class Student implements java.io.Serializable 11 12 private String id;13 private Str
11、ing name;14 private Set teachers = new HashSet(0);15 16 /*17 * hibernate.id generator-class=uuid.hex column=id length=3218 */19 public String getId() 20 return this.id;21 22 23 public void setId(String id) 24 this.id = id;25 26 27 /*28 * hibernate.property column=name length=32 not-null=true type=ja
12、va.lang.String29 */30 public String getName() 31 return this.name;32 33 34 public void setName(String name) 35 this.name = name;36 37 38 /*39 * hibernate.set table=student_teacher_relation40 * hibernate.key column=student_id41 * hibernate.many-to-many class=po.Teacher column=teacher_id42 */43 public
13、 Set getTeachers() 44 return teachers;45 46 47 public void setTeachers(Set teachers) 48 this.teachers = teachers;49 50 Teacher.java 1 package po; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 /* 7 * hibernate.class table=teacher 8 */ 9 SuppressWarnings(serial)10 public class Teacher impl
14、ements java.io.Serializable 11 12 private String id;13 private String name;14 private Set students = new HashSet(0);15 16 /*17 * hibernate.id generator-class=uuid.hex column=id length=3218 */19 public String getId() 20 return this.id;21 22 23 public void setId(String id) 24 this.id = id;25 26 27 /*2
15、8 * hibernate.property column=name length=32 not-null=true type=java.lang.String29 */30 public String getName() 31 return this.name;32 33 34 public void setName(String name) 35 this.name = name;36 37 38 /*39 * hibernate.set table=student_teacher_relation40 * hibernate.key column=teacher_id41 * hiber
16、nate.many-to-many class=po.Student column=student_id42 */43 public Set getStudents() 44 return students;45 46 47 public void setStudents(Set students) 48 this.students = students;49 50 51 总结:1.本文没有涉及hibernate annnotation模式,需要者请自行查阅相关资料。2.XDoclet2没有XDoclet1代好找,推荐一个网址 Jar Search Engine3.本文未能涉及在开发中,程序运转时遇到DB schema结构变化而进行的动态自动转换技术。如有高人知晓,请不吝赐教。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1