Many-to-many For example, there are Position and user two tables, a Position can have multiple users, a user can have multiple Position, the middle of the association table is test_user_position. The HBM configuration file is automatically generated by adding Xdoclet to the PO. No nonsense, look at the code.
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> Users = new treeset<users> ();
/** *//**
* @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<users> getusers () ... {
return users;
}
public void Setusers (set<users> Users) ... {
this.users = users;
}
}
package test;
import Java.util.Set;
import Java.util.TreeSet;
/** *//**
* @hibernate. Class <a title=table href= "Http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid =892989 "target=_blank>table</a>=" Test_position "
*/
public class Position ... {
private int id;
private int name;
private Set<users> Users = new treeset<users> ();
/** *//**
* @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" <a title=table href= "http://www.alimama.com/membersvc/buyadzone/" buy_ad_zone.htm?adzoneid=892989 "target=_blank>table</a>=" Test_user_position "
* @hibernate. Collection-key column= "position_id"
* @hibernate. Collection-many-to-many class= "test. Users "column=" user_id "
*/
public set<users> getusers () ... {
return users;
}
public void Setusers (set<users> 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> Users = new treeset<users> ();
/** *//**
* @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<users> getusers () ... {
return users;
}
public void Setusers (set<users> Users) ... {
this.users = users;
}
}
Next is Users.java
.
Java Code
package test;
Import java.util.*;
/** *//**
* @hibernate. Class table= "Test_uses"
*/
public class Users ... {
private int id;
private String name;
private set<position> positions = new treeset<position> ();
/** *//**
* @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<position> getpositions () ... {
return positions;
}
public void Setpositions (set<position> positions) ... {
this.positions = positions;
}
}
view Plaincopy to Clipboardprint?
package test;
import java.util.*;
/** *//**
* @hibernate. Class <a title=table href= "Http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid =892989 "target=_blank>table</a>=" test_uses "
*/
public class Users ... {
private int id;
private String name;
private set<position> positions = new treeset<position> ();
/** *//** * @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 <a title=table href= "http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid= 892989 "target=_blank>table</a>=" Test_user_position "lazy=" true "
* @hibernate. Collection-key column= "user_id"
* @hibernate. Collection-many-to-many class= "test. Position "column=" position_id "
*/
Public set<position> getpositions () ... {
return positions;
}
public void Setpositions (set<position> 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<position> positions = new treeset<position> ();
/** *//**
* @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<position> getpositions () ... {
return positions;
}
public void Setpositions (set<position> positions) ... {
this.positions = positions;
}
}
In MyEclipse Right click Item, choose "Properties", select "Myeclipse/xdoclet" from the interface, click "Add Standard ...", add "Standard Hibernate", click OK, end setting.
Build Hibernate.cfg.xml in projects, configure sessionfactory and data sources
Right-click on the item, select "Myeclipse/run XDoclet", will automatically create the above two classes corresponding to the HBM file.
Note: There is a problem with creating the completed HBM file, which has role and ReadOnly attributes, change the former to name, and the latter to delete it.