我有以下测试方法(基于以前创建和工作的测试用例):
`public class CommentGenericDaoHibernateImplTest { 私人评论评论;私人 ReqCandAssociation reqCandAssc; 私人 JobReq JOBREQ;私人候选人候选人;
private ClassPathXmlApplicationContext ctx;
private GenericDao<Comment, Integer> dao;
private GenericDao<JobReq, Integer> reqDao;
private GenericDao<Candidate, Integer> candDao;
private GenericDao<ReqCandAssociation, Integer> reqCandDao;
@SuppressWarnings("unchecked")
@Before
public void setUp() {
JOBREQ = new JobReq("TTYL", "Title", "contract", "project", "laborCategory", "summary", "jobDescription", "status", "TTONumber", new Date(), new Date(), "location", "lead", "leadEmail", "reason", "duration", "recruiter", "openingType", new Date(), "f", "2");
CANDIDATE = new Candidate("candName", "email", "phoneNum", "title", "mainStatus", new Date(), new Date(), new Date(), "none", "skillsSummary", "costRate", "clearanceType", new Date(), "contingentHire", "submittingCompany", "employingCompany", "5");
reqCandAssc = new ReqCandAssociation(CANDIDATE, JOBREQ, "Hired");
comment = new Comment("CommentGenericDaoHibernateImplTest TEST COMMENT", new Date(), reqCandAssc);
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
reqDao = (GenericDaoHibernateImpl<JobReq, Integer>) ctx.getBean("jobReqDao");
candDao = (GenericDaoHibernateImpl<Candidate, Integer>) ctx.getBean("candDao");
reqCandDao = (GenericDaoHibernateImpl<ReqCandAssociation, Integer>) ctx.getBean("reqCandDao");
dao = (GenericDaoHibernateImpl<Comment, Integer>) ctx.getBean("commentDao");
reqDao.create(JOBREQ);
candDao.create(CANDIDATE);
reqCandDao.create(reqCandAssc);
}
@Test
public void testCreatingComment() {
try {
Integer key = reqDao.create(comment);
assertTrue("attachment not created successfully!", key > 0);
} finally {
if (ctx != null) {
ctx.close();
}
}
}
@Test
public void testRetrievingCommentByIdAfterCreate() {
try {
Integer key = dao.create(comment);
Comment retrievedComment = dao.read(key);
assertEquals("Could not find attachment with id = "+key, retrievedComment, comment);
} finally {
if (ctx != null) {
ctx.close();
}
}
}
@Test
public void testUpdatingCommentAfterCreate() {
try {
Integer key = dao.create(comment);
comment.setComment("ALTERED");
dao.update(comment);
Comment retrievedComment = dao.read(key);
assertEquals("Could not update attachment with id = "+key,
comment.getComment(), retrievedComment.getComment());
} finally {
if (ctx != null) {
ctx.close();
}
}
}
@Test
public void testDeletingCommentAfterCreate() {
try {
comment.setCommentDate(new Date());
Integer key = dao.create(comment);
assertTrue("attachment not created successfully!", key > 0);
dao.delete(comment);
Comment queriedComment = dao.read(key);
assertNull("Could not delete attachment with id = "+key, queriedComment);
} finally {
if (ctx != null) {
ctx.close();
}
}
}
@Test
public void testRetrievingAllCommentsAfterCreate() {
try {
Integer key = dao.create(comment);
assertTrue("attachment not created successfully!", key > 0);
List<Comment> queriedAttachmentList = dao.findAll();
assertEquals("Could not list all attachment", 1, queriedAttachmentList.size());
} finally {
if (ctx != null) {
ctx.close();
}
}
}
}`
在这样的方法中使用休眠:
@SuppressWarnings("unchecked")
public PK create(T o) {
Session hibernateSession = this.getSession();
PK id = null;
try {
hibernateSession.beginTransaction();
id = (PK) hibernateSession.save(o);
hibernateSession.getTransaction().commit();
} finally {
hibernateSession.close();
}
return id;
}
对 DB 采取行动。现在一个Comment
实体在一个带有 ReqCandAssociation 的 OneToMany 中,而 ReqCandAssociation 又是 JobReq 和 Candidate 的 ManyToMany 解析表。所以基本上我需要创建一个 JobReq、Candidate,然后我可以创建一个 ReqCandAssociation,然后是一个注释以附加到一个 ReqCandAssociation 对象。但是,每当我尝试向数据库中添加多个对象时,我都会Waiting for table metadata lock
在 MySQL 中获得一个进程。
我曾尝试测试一次创建一个实体,它允许我创建一个 Candidate 或 jobreq 并添加一个 jobreq(而不是评论,用于测试)但是一旦我尝试同时添加一个候选人和一个 job req 然后做别的东西我得到了锁。我还尝试在@Test
方法本身而不是在设置中添加这些先决条件对象。