Hibernate利用XDoclet自动生成配置文件
Hibernate利用XDoclet自动生成配置文件
作者: 发布时间:2008-07-2210:05:51 来源:育路计算机考试频道
many-to-many为例,有Position和User两张表,一个Position可以有多个Users,一个User也可以有多个Position,中间的关联表为test_user_position。通过在PO中加入XDoclet,自动生成hbm配置文件。不废话,看代码。
packagetest;
importjava.util.Set;
importjava.util.TreeSet;
/***//**
*@hibernate.classtable="test_position"
*/
publicclassPosition...{
privateintid;
privateintname;
privateSet<Users>users=newTreeSet<Users>();
/***//**
*@hibernate.idgenerator-class="identity"type="int"
*/
publicintgetId()...{
returnid;
}
publicvoidsetId(intid)...{
this.id=id;
}
/***//**
*@hibernate.propertylength="25"
*/
publicintgetName()...{
returnname;
}
publicvoidsetName(intname)...{
this.name=name;
}
/***//**
*@hibernate.setinverse="true"lazy="true"table="test_user_position"
*@hibernate.collection-keycolumn="position_id"
*@hibernate.collection-many-to-manyclass="test.Users"column="user_id"
*/
publicSet<Users>getUsers()...{
returnusers;
}
publicvoidsetUsers(Set<Users>users)...{
this.users=users;
}
}
packagetest;
importjava.util.Set;
importjava.util.TreeSet;
/***//**
*@hibernate.class<Atitle=tablehref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>table</A>="test_position"
*/转 publicclassPosition...{
privateintid;
privateintname;
privateSet<Users>users=newTreeSet<Users>();
/***//**
*@hibernate.idgenerator-class="identity"type="int"
*/
publicintgetId()...{
returnid;
}
publicvoidsetId(intid)...{
this.id=id;
}
/***//**
*@hibernate.propertylength="25"
*/
publicintgetName()...{
returnname;
}
publicvoidsetName(intname)...{
this.name=name;
}
/***//**
*@hibernate.setinverse="true"lazy="true"<Atitle=tablehref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>table</A>="test_user_position"
*@hibernate.collection-keycolumn="position_id"
*@hibernate.collection-many-to-manyclass="test.Users"column="user_id"
*/
publicSet<Users>getUsers()...{
returnusers;
}
publicvoidsetUsers(Set<Users>users)...{
this.users=users;
}
}
packagetest;
importjava.util.Set;
importjava.util.TreeSet;
/***//**
*@hibernate.classtable="test_position"
*/
publicclassPosition...{
privateintid;
privateintname;
privateSet<Users>users=newTreeSet<Users>();
/***//**
*@hibernate.idgenerator-class="identity"type="int"
*/
publicintgetId()...{
returnid;
}
publicvoidsetId(intid)...{
this.id=id;
}
/***//**
*@hibernate.propertylength="25"
*/
publicintgetName()...{
returnname;
}
publicvoidsetName(intname)...{
this.name=name;
}
/***//**
*@hibernate.setinverse="true"lazy="true"table="test_user_position"
*@hibernate.collection-keycolumn="position_id"
*@hibernate.collection-many-to-manyclass="test.Users"column="user_id"
贴于:Java认证考试_考试大
publicSet<Users>getUsers()...{
returnusers;
}
publicvoidsetUsers(Set<Users>users)...{
this.users=users;
}
}
接下来是Users.java
Java代码
packagetest;
importjava.util.*;
/***//**
*@hibernate.classtable="test_uses"
*/
publicclassUsers...{
privateintid;
privateStringname;
privateSet<Position>positions=newTreeSet<Position>();
/***//**
*@hibernate.idgenerator-class="identity"typ="int"
*/
publicintgetId()...{
returnid;
}
publicvoidsetId(intid)...{
this.id=id;
}
/***//**
*@hibernate.propertylength="25"
*/
publicStringgetName()...{
returnname;
}
publicvoidsetName(Stringname)...{
this.name=name;
}
/***//**
*@hibernate.settable="test_user_position"lazy="true"
*@hibernate.collection-keycolumn="user_id"
*@hibernate.collection-many-to-manyclass="test.Position"column="position_id"
*/
publicSet<Position>getPositions()...{
returnpositions;
}
publicvoidsetPositions(Set<Position>positions)...{
this.positions=positions;
}
}
viewplaincopytoclipboardprint?
packagetest;
importjava.util.*;
/***//**
*@hibernate.class<Atitle=tablehref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>table</A>="test_uses"
*/
publicclassUsers...{
privateintid;
privateStringname;
privateSet<Position>positions=newTreeSet<Position>();
/***//**
*@hibernate.idgenerator-class="identity"typ="int"
*/
publicintgetId()...{
returnid;
}
publicvoidsetId(intid)...{
this.id=id;
}
/***//**
*@hibernate.propertylength="25"
*/
publicStringgetName()...{
returnname;
}publicvoidsetName(Stringname)...{
this.name=name;
}
/***//**
*@hibernate.set<Atitle=tablehref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>table</A>="test_user_position"lazy="true"
*@hibernate.collection-keycolumn="user_id"
*@hibernate.collection-many-to-manyclass="test.Position"column="position_id"
*/
publicSet<Position>getPositions()...{
returnpositions;
}
publicvoidsetPositions(Set<Position>positions)...{
this.positions=positions;
}
}
packagetest;
importjava.util.*;
/***//**
*@hibernate.classtable="test_uses"
*/
publicclassUsers...{
privateintid;
privateStringname;
privateSet<Position>positions=newTreeSet<Position>();
/***//**
*@hibernate.idgenerator-class="identity"typ="int"
*/
publicintgetId()...{
returnid;
}
publicvoidsetId(intid)...{
this.id=id;
}
/***//**
*@hibernate.propertylength="25"
*/
publicStringgetName()...{
returnname;
}
publicvoidsetName(Stringname)...{
this.name=name;
}
/***//**
*@hibernate.settable="test_user_position"lazy="true"
*@hibernate.collection-keycolumn="user_id"
*@hibernate.collection-many-to-manyclass="test.Position"column="position_id"
*/
publicSet<Position>getPositions()...{
returnpositions;
}
publicvoidsetPositions(Set<Position>positions)...{
this.positions=positions;
}
}
育路网