-->
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.  [ 2 posts ] 
Author Message
 Post subject: Simple iteration generates inserts that fail (duplicate key)
PostPosted: Mon Mar 07, 2005 9:33 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 9:15 am
Posts: 26
Hii,

I am inserting new objects in the DB, closing a transaction and then
attemtping to read the objects again in a diff. transaction. This seems to work
until the last commit (after iterating through the listed objects). I get an
exception due to a violated constraint. It seems that hHibernate is attemtpting to insert
the object again.

Can anyone point out my (most probably stupid) mistake? Here is the
code I use:
SessionFactory factory;
try {
factory = new Configuration()
// Loaded as a resource, cannot
// seem to use a generic directory
//.configure(base_dir + HiberConfFile)
.configure("/pt/inescporto/"+HiberConfFile)
.buildSessionFactory();
Session s;
s = factory.openSession();
Transaction tx;
tx = s.beginTransaction();

log.info( "Begin transaction to add Product objects" );
Product pd1 = new Product();
pd1.setName("Product-1");
pd1.setDescription("Product-1: <30");
s.save( pd1 );
<snip...>
log.info( "End transaction to add Product objects" );
tx.commit();
s.close();

log.info( "Begin transaction to lists Products objects" );

s = factory.openSession();
tx = s.beginTransaction();

List prods = s.find("from Product");
Iterator i = prods.iterator();
while (i.hasNext())
{
Product p = (Product)i.next();
//log.info("Found: " + p.toString() );
}

log.info( "End transaction to list Product objects" );
tx.commit(); <------ exception occurs here. Invalid constraint.
s.close();
factory.close();

}

I have not posted mappings, logs etc. because I think this is such a simple exercise
that I must be making a very obvious mistake.

I am using
- Hibernate version: 1.2.8
- PostgreSQL 7.4.5
- executing the code showing SQL (log4j set to debug)

BTW: I am using version 1.2.8 but the jar file still shows version 1.2.7 in its manifest.

TIA,
Hugo Ferreira.


Top
 Profile  
 
 Post subject: Simple iteration generates inserts that fail (duplica
PostPosted: Mon Mar 07, 2005 10:40 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 9:15 am
Posts: 26
Hi,

I have some additional information and am suspiicious that something is wrong
with the session's "find" method. If I use the session find but do not attribute
the returned object to a Product variable then all goes well:

List prods = s.find("from Product");
Iterator i = prods.iterator();
while (i.hasNext())
{
i.next();
}

If I use the equivalent createQuery method, then it works correctly:
List prods = s.createQuery("from Product")
.setMaxResults(100)
.list();
Iterator i = prods.iterator();
while (i.hasNext())
{
Product p = (Product)i.next();
log.info("Found: " + p.toString() );
}

If I read the manuall correclty then find and createQuery should work
the same way. If so, then the find method is not functioning correclty.

In case it is needed, I can make available a test case.

Regards,
Hugo Ferreira.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.