我试图在一个非常简单的Hibernate示例中填充一些实体对象。我的数据库由两个表组成,“部门”(Id,Name)和“Employees”(Id,DepartmentsId,FirstName,LastName)。我的SQL查询只是Employees与Departments的左连接。
我已经设置了Hibernate documentation中指定的注释,但每当我尝试序列化实体时,Hibernate进入无限循环并最终抛出StackOverFlowError异常。回答我的另一个问题的人能够确定堆栈溢出正在发生,因为“Department”对象包含一组“Employee”对象,每个对象都包含一个“Department”对象,其中包含一组Employee对象等。等
根据上面链接的文档,这种类型的双向关系应该是合法的(Department中的“mappedBy”参数应该提供Hibernate;我也尝试使用在“注释”中注释掉的“joinColumn”注释下面的代码),以及我读过的其他内容表明Hibernate 假设足够聪明,不会在这种情况下进入无限循环,但它不适合我的例子。如果我通过从Employee类中删除Department对象将双向关系更改为单向关系,一切正常,但显然这会导致丢失大量功能。
我还尝试了旧版xml映射文件的注释,并为子表设置了“inverse”参数,但它仍然会产生同样的问题。我怎样才能使这种双向关系按照预期的方式运行?
系:
package com.test.model;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.JoinTable;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.JoinColumn;
import org.hibernate.Hibernate;
import org.hibernate.proxy.HibernateProxy;
@Entity
@Table(name="Departments"
,catalog="test"
)
public class Department implements java.io.Serializable {
private Integer id;
private String name;
public Set<Employee> employees = new HashSet<Employee>(0);
public Department() {
}
public Department(String name) {
this.name = name;
}
public Department(String name, Set employees) {
this.name = name;
this.employees = employees;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="Id", unique=true, nullable=false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name="Name", nullable=false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="department")
/*@OneToMany
@JoinColumn(name="DepartmentsId")*/
public Set<Employee> getEmployees() {
return this.employees;
}
public void setEmployees(Set employees) {
this.employees = employees;
}
}
员工:
package com.test.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.JoinTable;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="Employees"
,catalog="test"
)
public class Employee implements java.io.Serializable {
private Integer id;
private Department department;
private String firstName;
private String lastName;
public Employee() {
}
public Employee(Department department, String firstName, String lastName) {
this.department = department;
this.firstName = firstName;
this.lastName = lastName;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="Id", unique=true, nullable=false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name="DepartmentsId", nullable=false, insertable=false, updatable=false)
public Department getDepartment() {
return this.department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Column(name="FirstName", nullable=false)
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name="LastName", nullable=false)
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
部门经理(包含HQL查询):
package com.test.controller;
import java.util.Collections;
import java.util.List;
import java.util.Iterator;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import com.test.model.Department;
import com.test.util.HibernateUtil;
public class DepartmentManager extends HibernateUtil {
public List<Department> list() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Department> set = null;
try {
Query q = session.createQuery("FROM Department d JOIN FETCH d.employees e");
q.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
set = (List<Department>) q.list();
} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
session.getTransaction().commit();
return set;
}
}
答案 0 :(得分:7)
通常,您不应序列化您的实体。循环依赖和代理使得这很难。相反,您应手动将需要发送的数据传输到DTO(新的仅数据类),并将其序列化。它不会有懒惰的集合,代理和诸如此类的东西。
答案 1 :(得分:0)
为了补充最高响应,我做了一个通用转换,谁为我做了工作,将实体值转移到DTO对象,你只需要从映射实体中创建具有相同名称的dto字段。
这是源代码。
/ ** * Atribui os valores de campos Correspondentes de um objeto para um outro objeto de destino。口 * campos do objeto de destino que ja estiverem preenchidos nao serao substituidos * * @param objetoOrigem * @param objetoDestino * @return * @throws NegocioException * /
public static <T1, T2> T2 convertEntity(T1 objetoOrigem, T2 objetoDestino) throws NegocioException {
if (objetoOrigem != null && objetoDestino != null) {
Class<? extends Object> classe = objetoOrigem.getClass();
Class<? extends Object> classeDestino = objetoDestino.getClass();
Field[] listaCampos = classe.getDeclaredFields();
for (int i = 0; i < listaCampos.length; i++) {
Field campo = listaCampos[i];
try {
Field campoDestino = classeDestino.getDeclaredField(campo.getName());
campo.setAccessible(true);
campoDestino.setAccessible(true);
atribuiValorAoDestino(objetoOrigem, objetoDestino, campo, campoDestino);
} catch (NoSuchFieldException e) {
LOGGER.log(Logger.Level.TRACE, (Object) e);
continue;
} catch (IllegalArgumentException | IllegalAccessException e) {
LOGGER.error(e.getMessage(), e);
throw new NegocioException(e.getMessage(), EnumTypeException.ERROR);
}
}
}
return objetoDestino;
}