-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Junitee & NonUniqueObjectException
PostPosted: Tue Aug 10, 2004 10:15 am 
Beginner
Beginner

Joined: Wed Jul 21, 2004 8:12 pm
Posts: 35
Hi Everyone:

reading all the postings on NonUniqueObjectException (as well as chapters in Hibernate in Action) and not being able to come up with a concrete solution, I was wondering if anyone can be of help in the following case:

I am using Junitee to run an in container testing for my QueueDAO class (1:m Queue : ticket). I am also using session per request. As I am trying to test serveral methods in QueueDAO, I end up with NonUniqueObjectException due to the fact that same objects are loaded into the session during a call in earlier tests. I have tried session.clear(), session.evit() and I have even tried to use locking to reassociate the detached objects loaded in previous sessions. However, I still end up getting the same exception. If anyone out there has goine around this exception, I would appreciate you feed back. The followings are the two methods in my test case.

public void testGetQueueById() {

try {
HibernateUtil util = HibernateUtil.getInstance();
QueueDAO qDAO = new QueueDAO();
Iterator it = qids.iterator();

Queue q = qDAO.getQueueById((String) it.next());
checkQueue(
"system",
"active",
"iam",
"First Queue",
"key word1",
q);

q = qDAO.getQueueById((String) it.next());
checkQueue(
"lotus",
"inactive",
"lot",
"Second Queue",
"key word2",
q);

q = qDAO.getQueueById((String) it.next());
checkQueue(
"websphere",
"active",
"was",
"third Queue",
"key word3",
q);

q = qDAO.getQueueById((String) it.next());
checkQueue(
"tivoli",
"active",
"tiv",
"Fourth Queue",
"key word3",
q);

util.commitTransaction();
// util.getSession().clear()
util.closeSession();


} catch (Exception e) {
fail("An unexpected exception has occured --> " + e.getMessage());
}
}


public void testGetQueueByName() {
try {
HibernateUtil util = HibernateUtil.getInstance();
QueueDAO qDAO = new QueueDAO();

Queue q = qDAO.getQueueByName("system");
util.getSession().lock(q, LockMode.READ);
checkQueue(
"system",
"active",
"iam",
"First Queue",
"key word1",
q);

q = qDAO.getQueueByName("lotus");
util.getSession().lock(q, LockMode.READ);
checkQueue(
"lotus",
"inactive",
"lot",
"Second Queue",
"key word2",
q);

q = qDAO.getQueueByName("websphere");
util.getSession().lock(q, LockMode.READ);
checkQueue(
"websphere",
"active",
"was",
"third Queue",
"key word3",
q);

q = qDAO.getQueueByName("tivoli");
util.getSession().lock(q, LockMode.READ);
checkQueue(
"tivoli",
"active",
"tiv",
"Fourth Queue",
"key word3",
q);

util.commitTransaction();
util.closeSession();

} catch (Exception e) {
fail("An unexpected exception has occured --> " + e.getMessage());
}
}

It is the running of second testcase aftert he first one that causes the exception despite the fact I am using the locking. There should be a way to make this situation work. Any url reference or help to solve this issue is greatly appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 6:39 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Try with session.clear(), in your case it is util.getSession().clear(), after each qDAO.getQueueById() call.

_________________
Leonid Shlyapnikov


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 9:48 am 
Beginner
Beginner

Joined: Wed Jul 21, 2004 8:12 pm
Posts: 35
HI:

Thanks for your reply, It actually worked. I was under the impression that a call to session.clear() at the end of the transaction would clear all the session not that I have to call session.clear() after each look up by ID.

I also managed to get around the problem by providing a thrid method that runs all the test methods within a transaction. It is only this method that commits the transaction and closes the session (This should work only for tests that does only lookups). For those who may want to consider it here is the code:

public void testmethods(){

try {
HibernateUtil util = HibernateUtil.getInstance();
getQueueById();
getQueueByName();
util.commitTransaction();
util.closeSession();

}catch(Exception e){

}
}

private void getQueueById() {

try {
HibernateUtil util = HibernateUtil.getInstance();
QueueDAO qDAO = new QueueDAO();

Queue q = qDAO.getQueueById(q1.getId());
checkQueue(
"system",
"active",
"iam",
"First Queue",
"key word1",
q1);

q = qDAO.getQueueById(q2.getId());
checkQueue(
"lotus",
"inactive",
"lot",
"Second Queue",
"key word2",
q);

q = qDAO.getQueueById(q3.getId());
checkQueue(
"websphere",
"active",
"was",
"third Queue",
"key word3",
q);

q = qDAO.getQueueById(q4.getId());
checkQueue(
"tivoli",
"active",
"tiv",
"Fourth Queue",
"key word3",
q);

} catch (Exception e) {
fail("An unexpected exception has occured --> " + e.getMessage());
}
}

private void getQueueByName() {
try {

Queue q = qDAO.getQueueByName(q1.getQueueName());

checkQueue(
"system",
"active",
"iam",
"First Queue",
"key word1",
q1);

q = qDAO.getQueueByName("lotus");
checkQueue(
"lotus",
"inactive",
"lot",
"Second Queue",
"key word2",
q);

q = qDAO.getQueueByName("websphere");
checkQueue(
"websphere",
"active",
"was",
"third Queue",
"key word3",
q);

q = qDAO.getQueueByName("tivoli");
checkQueue(
"tivoli",
"active",
"tiv",
"Fourth Queue",
"key word3",
q);

} catch (Exception e) {
fail("An unexpected exception has occured --> " + e.getMessage());
}
}

private void checkQueue(
String name,
String status,
String tName,
String desc,
String kWord,
Queue q) {

assertEquals(name, q.getQueueName());
assertEquals(status, q.getQueueStatus());
assertEquals(tName, q.getQueueTeamName());
assertEquals(desc, q.getQueueDescription());
assertEquals(kWord, q.getQueueKeywords());
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 9:51 am 
Beginner
Beginner

Joined: Wed Jul 21, 2004 8:12 pm
Posts: 35
siamackj wrote:
HI:

Thanks for your reply, It actually worked. I was under the impression that a call to session.clear() at the end of the transaction would clear all the session not that I have to call session.clear() after each look up by ID.

I also managed to get around the problem by providing a thrid method that runs all the test methods within a transaction. It is only this method that commits the transaction and closes the session (This should work only for tests that does only lookups). For those who may want to consider it here is the code:

public void testmethods(){

try {
HibernateUtil util = HibernateUtil.getInstance();
getQueueById();
getQueueByName();
util.commitTransaction();
util.closeSession();

}catch(Exception e){

}
}

private void getQueueById() {

try {
HibernateUtil util = HibernateUtil.getInstance();


Queue q = qDAO.getQueueById(q1.getId());
checkQueue(
"system",
"active",
"iam",
"First Queue",
"key word1",
q1);

q = qDAO.getQueueById(q2.getId());
checkQueue(
"lotus",
"inactive",
"lot",
"Second Queue",
"key word2",
q);

q = qDAO.getQueueById(q3.getId());
checkQueue(
"websphere",
"active",
"was",
"third Queue",
"key word3",
q);

q = qDAO.getQueueById(q4.getId());
checkQueue(
"tivoli",
"active",
"tiv",
"Fourth Queue",
"key word3",
q);

} catch (Exception e) {
fail("An unexpected exception has occured --> " + e.getMessage());
}
}

private void getQueueByName() {
try {

Queue q = qDAO.getQueueByName(q1.getQueueName());

checkQueue(
"system",
"active",
"iam",
"First Queue",
"key word1",
q1);

q = qDAO.getQueueByName("lotus");
checkQueue(
"lotus",
"inactive",
"lot",
"Second Queue",
"key word2",
q);

q = qDAO.getQueueByName("websphere");
checkQueue(
"websphere",
"active",
"was",
"third Queue",
"key word3",
q);

q = qDAO.getQueueByName("tivoli");
checkQueue(
"tivoli",
"active",
"tiv",
"Fourth Queue",
"key word3",
q);

} catch (Exception e) {
fail("An unexpected exception has occured --> " + e.getMessage());
}
}

private void checkQueue(
String name,
String status,
String tName,
String desc,
String kWord,
Queue q) {

assertEquals(name, q.getQueueName());
assertEquals(status, q.getQueueStatus());
assertEquals(tName, q.getQueueTeamName());
assertEquals(desc, q.getQueueDescription());
assertEquals(kWord, q.getQueueKeywords());
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 9:52 am 
Beginner
Beginner

Joined: Wed Jul 21, 2004 8:12 pm
Posts: 35
HI:

Thanks for your reply, It actually worked. I was under the impression that a call to session.clear() at the end of the transaction would clear all the session not that I have to call session.clear() after each look up by ID.

I also managed to get around the problem by providing a thrid method that runs all the test methods within a transaction. It is only this method that commits the transaction and closes the session (This should work only for tests that does only lookups). For those who may want to consider it here is the code:

public void testmethods(){

try {
HibernateUtil util = HibernateUtil.getInstance();
getQueueById();
getQueueByName();
util.commitTransaction();
util.closeSession();

}catch(Exception e){

}
}

private void getQueueById() {

try {
HibernateUtil util = HibernateUtil.getInstance();


Queue q = qDAO.getQueueById(q1.getId());
checkQueue(
"system",
"active",
"iam",
"First Queue",
"key word1",
q1);

q = qDAO.getQueueById(q2.getId());
checkQueue(
"lotus",
"inactive",
"lot",
"Second Queue",
"key word2",
q);

q = qDAO.getQueueById(q3.getId());
checkQueue(
"websphere",
"active",
"was",
"third Queue",
"key word3",
q);

q = qDAO.getQueueById(q4.getId());
checkQueue(
"tivoli",
"active",
"tiv",
"Fourth Queue",
"key word3",
q);

} catch (Exception e) {
fail("An unexpected exception has occured --> " + e.getMessage());
}
}

private void getQueueByName() {
try {

Queue q = qDAO.getQueueByName(q1.getQueueName());

checkQueue(
"system",
"active",
"iam",
"First Queue",
"key word1",
q1);

q = qDAO.getQueueByName("lotus");
checkQueue(
"lotus",
"inactive",
"lot",
"Second Queue",
"key word2",
q);

q = qDAO.getQueueByName("websphere");
checkQueue(
"websphere",
"active",
"was",
"third Queue",
"key word3",
q);

q = qDAO.getQueueByName("tivoli");
checkQueue(
"tivoli",
"active",
"tiv",
"Fourth Queue",
"key word3",
q);

} catch (Exception e) {
fail("An unexpected exception has occured --> " + e.getMessage());
}
}

private void checkQueue(
String name,
String status,
String tName,
String desc,
String kWord,
Queue q) {

assertEquals(name, q.getQueueName());
assertEquals(status, q.getQueueStatus());
assertEquals(tName, q.getQueueTeamName());
assertEquals(desc, q.getQueueDescription());
assertEquals(kWord, q.getQueueKeywords());
}[/quote][/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 11, 2004 9:52 am 
Beginner
Beginner

Joined: Wed Jul 21, 2004 8:12 pm
Posts: 35
HI:

Thanks for your reply, It actually worked. I was under the impression that a call to session.clear() at the end of the transaction would clear all the session not that I have to call session.clear() after each look up by ID.

I also managed to get around the problem by providing a thrid method that runs all the test methods within a transaction. It is only this method that commits the transaction and closes the session (This should work only for tests that does only lookups). For those who may want to consider it here is the code:

public void testmethods(){

try {
HibernateUtil util = HibernateUtil.getInstance();
getQueueById();
getQueueByName();
util.commitTransaction();
util.closeSession();

}catch(Exception e){

}
}

private void getQueueById() {

try {
HibernateUtil util = HibernateUtil.getInstance();


Queue q = qDAO.getQueueById(q1.getId());
checkQueue(
"system",
"active",
"iam",
"First Queue",
"key word1",
q1);

q = qDAO.getQueueById(q2.getId());
checkQueue(
"lotus",
"inactive",
"lot",
"Second Queue",
"key word2",
q);

q = qDAO.getQueueById(q3.getId());
checkQueue(
"websphere",
"active",
"was",
"third Queue",
"key word3",
q);

q = qDAO.getQueueById(q4.getId());
checkQueue(
"tivoli",
"active",
"tiv",
"Fourth Queue",
"key word3",
q);

} catch (Exception e) {
fail("An unexpected exception has occured --> " + e.getMessage());
}
}

private void getQueueByName() {
try {

Queue q = qDAO.getQueueByName(q1.getQueueName());

checkQueue(
"system",
"active",
"iam",
"First Queue",
"key word1",
q1);

q = qDAO.getQueueByName("lotus");
checkQueue(
"lotus",
"inactive",
"lot",
"Second Queue",
"key word2",
q);

q = qDAO.getQueueByName("websphere");
checkQueue(
"websphere",
"active",
"was",
"third Queue",
"key word3",
q);

q = qDAO.getQueueByName("tivoli");
checkQueue(
"tivoli",
"active",
"tiv",
"Fourth Queue",
"key word3",
q);

} catch (Exception e) {
fail("An unexpected exception has occured --> " + e.getMessage());
}
}

private void checkQueue(
String name,
String status,
String tName,
String desc,
String kWord,
Queue q) {

assertEquals(name, q.getQueueName());
assertEquals(status, q.getQueueStatus());
assertEquals(tName, q.getQueueTeamName());
assertEquals(desc, q.getQueueDescription());
assertEquals(kWord, q.getQueueKeywords());
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.