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.
|