我正在做一個SSH項目的JUnit。 首先,我在做一個@Test方法時遇到了org.hibernate.LazyInitializationException。我做了一些研究並解決了它。 我所做的是添加用於綁定會話資源的Setup和TearDown。奇怪的org.hibernate.LazyInitializationException什麼時候做JUNIT
這2個方法是這樣的:
@Before
public void setup(){
System.out.println("Setup() running....");
String filename = "WebContent\\WEB-INF\\classes\\applicationContext.xml";
ctx = new FileSystemXmlApplicationContext(filename);
sf = (SessionFactory) ctx.getBean("sessionFactory");
Session s = sf.openSession();
TransactionSynchronizationManager.bindResource(sf, new SessionHolder(s));
}
@After
public void tearDown(){
System.out.println("TearDown() running....");
SessionHolder holder = (SessionHolder)TransactionSynchronizationManager.getResource(sf);
Session s = holder.getSession();
s.flush();
TransactionSynchronizationManager.unbindResource(sf);
SessionFactoryUtils.closeSession(s);
}
private SessionFactory sf;
private ApplicationContext ctx;
然後我寫的另一種方法,但這種例外再次上調!有點奇怪。我重新運行第一個測試方法,它的工作原理。但第二個沒有。
第一個測試方法是這樣的:
@Test
public void testFindAll() {
UserGroupService userggroupService = (UserGroupService) ctx.getBean("UserGroupService");
try {
List groups = userggroupService.findAll();
System.out.println(groups);
if (groups != null){
for (Object grp : groups){
UserGroupBean group = (UserGroupBean) grp;
System.out.printf("id=%d, name=%s, %d members\n", group.getId(), group.getName(), group.getMembers()==null?0:group.getMembers().size());
if (group.getMembers() != null){
for(UserBean user: group.getMembers()){
System.out.printf("\tuser-%d, %s\n", user.getUserID(), user.getName());
}
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
第二個測試方法是這樣的:
@Test
public void testDeleteRel() {
String filename = "WebContent\\WEB-INF\\classes\\applicationContext.xml";
ApplicationContext ctx = new FileSystemXmlApplicationContext(filename);
UserGroupService service = (UserGroupService) ctx.getBean("UserGroupService");
try {
UserGroupBean userGroup = new UserGroupBean();
userGroup.setId(1L);
UserGroupBean group = (UserGroupBean) service.getUserGroup(userGroup);
List<UserBean> members = group.getMembers();
System.out.println(members.size()+" members");
// members.remove(0);
// service.updateUserGroup(group);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
這樣的PO代碼:
@Entity
@Table(name = "UserGroupBean")
public class UserGroupBean implements UserGroup {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToMany(targetEntity = UserGroupBean.class,
cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
@JoinTable(name = "UserGroupParents", joinColumns = { @JoinColumn(name = "group_id") }, inverseJoinColumns = { @JoinColumn(name = "parent_id") })
//@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private List<UserGroupBean> parents;
@ManyToMany(targetEntity = UserBean.class,
cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
@JoinTable(name = "UserGroupMembers", joinColumns = { @JoinColumn(name = "group_id") }, inverseJoinColumns = { @JoinColumn(name = "user_id") })
//@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private List<UserBean> members;
// Getters and setters ...
我的問題是:爲什麼相同的@Before和@After代碼,但只有1個方法可以運行良好?我錯過了什麼?
感謝
是的,你是對的。我犯了一個愚蠢的錯誤:) –