Hibernate use Xdoclet to automatically generate configuration files

Source: Internet
Author: User
Tags generator

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.