manytomany为例有Position和User两张表一个Position可以有多个Users一个User也可以有多个 Position中间的关联表为 test_user_position 通过在PO中加入XDoclet自动生成hbm配置文件不废话看代码
package test;
import javautilSet;
import javautilTreeSet;
/** *//**
* @hibernateclass table=test_position
*/
public class Position {
private int id;
private int name;
private Set<Users> users = new TreeSet<Users>();
/** *//**
* @hibernateid generatorclass=identity type=int
*/
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
/** *//**
* @hibernateproperty length=
*/
public int getName() {
return name;
}
public void setName(int name) {
thisname = name;
}
/** *//**
* @hibernateset inverse=true lazy=true table=test_user_position
* llectionkey column=position_id
* llectionmanytomany class=testUsers column=user_id
*/
public Set<Users> getUsers() {
return users;
}
public void setUsers(Set<Users> users) {
thisusers = users;
}
}
package test;
import javautilSet;
import javautilTreeSet;
/** *//**
* @hibernateclass <A title=table target=_blank>table</A>=test_position
*/
public class Position {
private int id;
private int name;
private Set<Users> users = new TreeSet<Users>();
/** *//**
* @hibernateid generatorclass=identity type=int
*/
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
/** *//**
* @hibernateproperty length=
*/
public int getName() {
return name;
}
public void setName(int name) {
thisname = name;
}
/** *//**
* @hibernateset inverse=true lazy=true <A title=table target=_blank>table</A>=test_user_position
* llectionkey column=position_id
* llectionmanytomany class=testUsers column=user_id
*/
public Set<Users> getUsers() {
return users;
}
public void setUsers(Set<Users> users) {
thisusers = users;
}
}
package test;
import javautilSet;
import javautilTreeSet;
/** *//**
* @hibernateclass table=test_position
*/
public class Position {
private int id;
private int name;
private Set<Users> users = new TreeSet<Users>();
/** *//**
* @hibernateid generatorclass=identity type=int
*/
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
/** *//**
* @hibernateproperty length=
*/
public int getName() {
return name;
}
public void setName(int name) {
thisname = name;
}
/** *//**
* @hibernateset inverse=true lazy=true table=test_user_position
* llectionkey column=position_id
* llectionmanytomany class=testUsers column=user_id
*/
public Set<Users> getUsers() {
return users;
}
public void setUsers(Set<Users> users) {
thisusers = users;
}
}
接下来是Usersjava
Java代码
package test;
import javautil*;
/** *//**
* @hibernateclass table=test_uses
*/
public class Users {
private int id;
private String name;
private Set<Position> positions = new TreeSet<Position>();
/** *//**
* @hibernateid generatorclass=identity typ=int
*/
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
/** *//**
* @hibernateproperty length=
*/
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
/** *//**
* @hibernateset table=test_user_position lazy=true
* llectionkey column=user_id
* llectionmanytomany class=testPosition column=position_id
*/
public Set<Position> getPositions() {
return positions;
}
public void setPositions(Set<Position> positions) {
thispositions = positions;
}
}
view plaincopy to clipboardprint?
package test;
import javautil*;
/** *//**
* @hibernateclass <A title=table target=_blank>table</A>=test_uses
*/
public class Users {
private int id;
private String name;
private Set<Position> positions = new TreeSet<Position>();
/** *//**
* @hibernateid generatorclass=identity typ=int
*/
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
/** *//**
* @hibernateproperty length=
*/
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
/** *//**
* @hibernateset <A title=table target=_blank>table</A>=test_user_position lazy=true
* llectionkey column=user_id
* llectionmanytomany class=testPosition column=position_id
*/
public Set<Position> getPositions() {
return positions;
}
public void setPositions(Set<Position> positions) {
thispositions = positions;
}
}
package test;
import javautil*;
/** *//**
* @hibernateclass table=test_uses
*/
public class Users {
private int id;
private String name;
private Set<Position> positions = new TreeSet<Position>();
/** *//**
* @hibernateid generatorclass=identity typ=int
*/
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
/** *//**
* @hibernateproperty length=
*/
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
/** *//**
* @hibernateset table=test_user_position lazy=true
* llectionkey column=user_id
* llectionmanytomany class=testPosition column=position_id
*/
public Set<Position> getPositions() {
return positions;
}
public void setPositions(Set<Position> positions) {
thispositions = positions;
}
}
在Myeclipse中右键点击项目
选择
Properties
从界面中选择
Myeclipse/XDoclet
点击
Add Standard
添加
Standard Hibernate
点击OK
结束设置
在项目中建立Hibernate
cfg
xml
配置好SessionFactory和数据源 右键点击项目
选择
Myeclipse/Run XDoclet
将自动创建以上两个类对应的hbm文件
注意
创建完成的hbm文件存在问题
里面有role和readonly属性
将前者改为name
后者删掉即可