Hibernate利用XDoclet自动生成配置文件

many-to-many为例,有Position和User两张表,一个Position可以有多个Users,一个User也可以有多个 Position,中间的关联表为 test_user_position 。通过在PO中加入XDoclet,自动生成hbm配置文件。不废话,看代码。

package test;import java.util.Set;import java.util.TreeSet;/** *//*** @hibernate.class table="test_position"*/public class Position ...{private int id;private int name;private Set users = new TreeSet();/** *//*** @hibernate.id generaTor-class="identity" type="int"*/public int getId() ...{return id;}public void setId(int id) ...{this.id = id;}/** *//*** @hibernate.property length="25"*/public int getName() ...{return name;}public void setName(int name) ...{this.name = name;}/** *//*** @hibernate.set inverse="true" lazy="true" table="test_user_position"* @hibernate.collection-key column="position_id"* @hibernate.collection-many-to-many class="test.Users" column="user_id"*/public Set getUsers() ...{return users;}public void setUsers(Set users) ...{this.users = users;}}package test;import java.util.Set;import java.util.TreeSet;/** *//*** @hibernate.class table="test_position"*/public class Position ...{private int id;private int name;private Set users = new TreeSet();/** *//*** @hibernate.id generaTor-class="identity" type="int"*/public int getId() ...{return id;}public void setId(int id) ...{this.id = id;}/** *//*** @hibernate.property length="25"*/public int getName() ...{return name;}public void setName(int name) ...{this.name = name;}/** *//*** @hibernate.set inverse="true" lazy="true" table="test_user_position"* @hibernate.collection-key column="position_id"* @hibernate.collection-many-to-many class="test.Users" column="user_id"*/public Set getUsers() ...{return users;}public void setUsers(Set users) ...{this.users = users;}}package test;import java.util.Set;import java.util.TreeSet;/** *//*** @hibernate.class table="test_position"*/public class Position ...{private int id;    private int name;private Set users = new TreeSet();/** *//*** @hibernate.id generaTor-class="identity" type="int"*/public int getId() ...{return id;}public void setId(int id) ...{this.id = id;}/** *//*** @hibernate.property length="25"*/public int getName() ...{return name;}public void setName(int name) ...{this.name = name;}/** *//*** @hibernate.set inverse="true" lazy="true" table="test_user_position"* @hibernate.collection-key column="position_id"* @hibernate.collection-many-to-many class="test.Users" column="user_id"*/public Set getUsers() ...{return users;}public void setUsers(Set users) ...{this.users = users;}}接下来是Users.javaJava代码package test;import java.util.*;/** *//*** @hibernate.class table="test_uses"*/public class Users ...{private int id;private String name;private Set positions = new TreeSet();/** *//*** @hibernate.id generaTor-class="identity" typ="int"*/public int getId() ...{return id;}public void setId(int id) ...{this.id = id;}/** *//*** @hibernate.property length="25"*/public String getName() ...{return name;}public void setName(String name) ...{this.name = name;}/** *//*** @hibernate.set table="test_user_position" lazy="true"* @hibernate.collection-key column="user_id"* @hibernate.collection-many-to-many class="test.Position" column="position_id"*/public Set getPositions() ...{return positions;}public void setPositions(Set positions) ...{this.positions = positions;}}view plaincopy to clipboardprint?package test;import java.util.*;/** *//*** @hibernate.class table="test_uses"*/public class Users ...{private int id;private String name;private Set positions = new TreeSet();/** *//**    * @hibernate.id generaTor-class="identity" typ="int"*/public int getId() ...{return id;}public void setId(int id) ...{this.id = id;}/** *//*** @hibernate.property length="25"*/public String getName() ...{return name;}public void setName(String name) ...{this.name = name;}/** *//*** @hibernate.set table="test_user_position" lazy="true"* @hibernate.collection-key column="user_id"* @hibernate.collection-many-to-many class="test.Position" column="position_id"*/public Set getPositions() ...{return positions;}public void setPositions(Set positions) ...{this.positions = positions;}}package test;import java.util.*;/** *//*** @hibernate.class table="test_uses"*/public class Users ...{private int id;private String name;private Set positions = new TreeSet();/** *//*** @hibernate.id generaTor-class="identity" typ="int"*/public int getId() ...{return id;}public void setId(int id) ...{this.id = id;}/** *//*** @hibernate.property length="25"*/public String getName() ...{return name;}public void setName(String name) ...{this.name = name;}/** *//*** @hibernate.set table="test_user_position" lazy="true"* @hibernate.collection-key column="user_id"* @hibernate.collection-many-to-many class="test.Position" column="position_id"*/public Set getPositions() ...{return positions;}public void setPositions(Set positions) ...{this.positions = positions;}}

在Myeclipse中右键点击项目,选择“Properties”,从界面中选择”Myeclipse/XDoclet”,点击“Add Standard…”,添加“Standard Hibernate”,点击OK,结束设置。

在项目中建立Hibernate.cfg.xml,配置好SessionFacTory和数据源

右键点击项目,选择“Myeclipse/Run XDoclet”,将自动创建以上两个类对应的hbm文件。

注意:创建完成的hbm文件存在问题,里面有role和readonly属性,将前者改为name,后者删掉即可。

生气是拿别人做错的事来惩罚自己

Hibernate利用XDoclet自动生成配置文件

相关文章:

你感兴趣的文章:

标签云: