我有三个实体 Employee、Person 和 EmailAdress,如下所示:
@Entity
public class Employee {
private Person person;
//Other data members
@ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinColumn(name="person_id")
public Partner getPerson() {
return person;
}
//Other getter and setters
}
@Entity
public class Person {
private Set<EmailAddress> emails;
//Other data members
@OneToMany(fetch=FetchType.LAZY, mappedBy="person", cascade=CascadeType.ALL)
public Set<EmailAddress> getEmails() {
return emails;
}
public void setEmails(Set<EmailAddress> emails) {
this.emails = emails;
for(EmailAddress email : this.emails) {
email.setPerson(this);
}
}
//Other getter and setters
}
@Entity
public class EmailAddress {
private Person person;
private String email;
private String emailType;
//getter and setter
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="partner_id")
public Partner getPerson() {
return person;
}
}
以及对Employee实体执行保存、更新、删除操作的EmployeeDAO。
@Repository("employeeDAO")
@Transactional
public class EmployeeDAO {
public Employee saveOrUpdate(Employee emp) {
try {
return (Employee) sessionFactory.getCurrentSession().merge(emp);
} catch(Exception excp) {
//Handle exception
}
return null;
}
//Other methods
}
EmployeeDAO 中的这个 saveOrUpdate() 将 Employee 实体保存到 DB 没有任何问题,但是当我使用相同的 saveOrUpdate() 更新 Employee 时,它失败并出现 LazyInitializationException。以下是 EmployeeDAOTest:
//Test class
public class EmployeeDAOTest extends AbstractTestNGSpringContextTests {
@Autowired
private EmployeeDAO employeeDAO;
private Employee createDummyEmployee() {
// Create dummy employee initialized with Person and EmailAddress
return employee;
}
@Test
public void testSaveOrUpdate() {
Employee emp = createDummyEmployee();
//Test Save/Insert
Employee savedEmp = employeeDAO.saveOrUpdate(emp);
Assert.assertNotNull(savedEmp); //Works fine. Employee gets saved correctly
//Test Update
savedEmp.getPerson().setName("Updated Name");
Employee updatedEmp = employeeDAO.saveOrUpdate(savedEmp);
Assert.assertNotNull(updatedEmp); // Fails... because of "org.hibernate.LazyInitializationException: could not initialize proxy - no Session"
}
}
在在这里发布这个问题之前,我做了一点谷歌搜索。并发现有两种方法可以解决它:
不要使用 LAZY 初始化,即lazy=false。但这种方法有其自身的含义。并且由于其性能问题而不能使用这种方法。
使用@PersistenceContext(type = PersistenceContextType.EXTENDED)。这解决了问题,但我认为,使用这种方法 Spring 不管理 EntityManager/TransactionManager,我必须自己管理这些。有没有什么办法可以用这种方法来管理 EntityManager/TransactionManager,这样我就不必自己管理它了。
或者有没有更好的方法来解决这个问题?