大家看看我的Hibernate同一个表双向1:n关联是否正确?

06-04-13 donguo
import java.util.HashSet;

import java.util.Set;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Entity;

import javax.persistence.JoinColumn;

import javax.persistence.ManyToOne;

import javax.persistence.OneToMany;

/**

* SysTDepartment generated by MyEclipse - Hibernate Tools

*/

@Entity

@javax.persistence.SequenceGenerator(

name="SEQ_GEN",

sequenceName="DEPARTMENTID"

)

public class SysTDepartment implements java.io.Serializable {

// Fields

private long deptid;

private SysTDepartment parentDepartment;

private long parentid;

private String deptcode;

private String deptname;

private String linkman;

private String businessarea;

private String address;

private String remark;

private String state;

private Set<SysTDepartment> departments = new HashSet();

//private Set users = new HashSet();

// Constructors

/** default constructor */

public SysTDepartment() {

}

/** full constructor */

public SysTDepartment(SysTDepartment sysTDepartment, long parentid, String deptcode, String deptname, String linkman, String businessarea, String address, String remark, String state, Set<SysTDepartment> sysTDepartments) {

this.parentDepartment = sysTDepartment;

this.parentid = parentid;

this.deptcode = deptcode;

this.deptname = deptname;

this.linkman = linkman;

this.businessarea = businessarea;

this.address = address;

this.remark = remark;

this.state = state;

this.departments = sysTDepartments;

}

// Property accessors

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_GEN")

public long getDeptid() {

return this.deptid;

}

public void setDeptid(long deptid) {

this.deptid = deptid;

}

/**

* @return Returns the parentid.

*/

public long getParentid() {

return parentid;

}

/**

* @param parentid The parentid to set.

*/

public void setParentid(long parentid) {

this.parentid = parentid;

}

/**

* @return Returns the parentDepartment.

*/

@ManyToOne

@JoinColumn(name="deptid", insertable=false, updatable=false)

public SysTDepartment getParentDepartment() {

return parentDepartment;

}

/**

* @param parentDepartment The parentDepartment to set.

*/

public void setParentDepartment(SysTDepartment parentDepartment) {

this.parentDepartment = parentDepartment;

}

public String getDeptcode() {

return this.deptcode;

}

public void setDeptcode(String deptcode) {

this.deptcode = deptcode;

}

public String getDeptname() {

return this.deptname;

}

public void setDeptname(String deptname) {

this.deptname = deptname;

}

public String getLinkman() {

return this.linkman;

}

public void setLinkman(String linkman) {

this.linkman = linkman;

}

public String getBusinessarea() {

return this.businessarea;

}

public void setBusinessarea(String businessarea) {

this.businessarea = businessarea;

}

public String getAddress() {

return this.address;

}

public void setAddress(String address) {

this.address = address;

}

public String getRemark() {

return this.remark;

}

public void setRemark(String remark) {

this.remark = remark;

}

public String getState() {

return this.state;

}

public void setState(String state) {

this.state = state;

}

/**

* @return Returns the users.

*/

/**

* @return Returns the departments.

*/

@OneToMany

@JoinColumn(name="parentid")

public Set<SysTDepartment> getDepartments() {

return departments;

}

/**

* @param departments The departments to set.

*/

public void setDepartments(Set<SysTDepartment> departments) {

this.departments = departments;

}

}

banq
2006-04-20 11:55
晕,还没用过同一个表的关系,技术过于专业吧?

这篇文章:O/R MAPPING (HIBERNATE)方法 指出使用hibernare正确之道:

http://www.jdon.com/jive/article.jsp?forum=16&thread=22244

猜你喜欢