java

位置:IT落伍者 >> java >> 浏览文章

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


发布日期:2024年06月15日
 
Hibernate利用XDoclet自动生成配置文件

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结束设置 在项目中建立Hibernatecfgxml配置好SessionFactory和数据源 右键点击项目选择Myeclipse/Run XDoclet将自动创建以上两个类对应的hbm文件 注意创建完成的hbm文件存在问题里面有role和readonly属性将前者改为name后者删掉即可

上一篇:Eclipse + JBoss 5 + EJB3

下一篇:Struts+Spring+Hibernate快速入门