-->
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.  [ 8 posts ] 
Author Message
 Post subject: Avoiding Duplicate IDs
PostPosted: Tue Aug 31, 2004 8:57 am 
Newbie

Joined: Tue Jul 13, 2004 5:38 pm
Posts: 10
Good day,

I am using Hibernate from within Tomcat as a service, and outside of Tomcat in a loose standing application. These two clients are accessing the database concurrently, so this causes duplicate ID's to be generated by Hibernate.

Any ideas on a solution to the problem will be greatly appreciated.

Kind regards,
Sitrac


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 8:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This should be impossible, if you are using the built-in id generation strategies correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 9:19 am 
Newbie

Joined: Tue Jul 13, 2004 5:38 pm
Posts: 10
Does Hibernate check the latest ID in the database before assigning a new one, or does it keep these entries in cache? In other words, if I were to add a database record manually without restarting a Hibernate session, will the Hibernate id generator create the correct id for a new entry?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 9:40 am 
Regular
Regular

Joined: Mon Feb 23, 2004 10:42 pm
Posts: 102
Location: Washington DC
Post your code and mapping files.

_________________
Matt Veitas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 10:36 am 
Newbie

Joined: Tue Jul 13, 2004 5:38 pm
Posts: 10
Below is the mapping for the class, a hibernate utility class and a shortened piece of source showing how the class is saved.

PostgreSQL responds with the following error:

org.postgresql.util.PSQLException: ERROR: duplicate key violates unique constraint "note_pkey"

After restarting Tomcat, the system works fine.

******************* NOTE MAPPING ***********************

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class
name="com.vhr.util.Note"
table="note"
dynamic-update="false"
dynamic-insert="false"
>

<id
name="id"
column="note_id"
type="java.lang.Long"
>
<generator class="increment">
</generator>
</id>

<property
name="text"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="text"
/>

<property
name="timestamp"
type="java.util.Date"
update="true"
insert="true"
access="property"
column="timestamp"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Note.xml
containing the additional properties and place it in your merge dir.
-->

</class>

</hibernate-mapping>

******************* HIBERNATE UTILITY CLASS *****************

package com.vhr.hibernate;

import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.cfg.Configuration;
import net.*;
/**
* To-do: import javax.servlet.Servlet;
*/
public class HibernateUtil {

/**
* Todo: implements Servlet{
*/
private static final SessionFactory sessionFactory;
public static final ThreadLocal session = new ThreadLocal();

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Configuration problem: " + ex.getMessage(), ex);
}
}

/**
* @return net.sf.hibernate.Session
* @throws net.sf.hibernate.HibernateException
*/
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

/**
* @throws net.sf.hibernate.HibernateException
*/
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}

******************* SAVING A NOTE ************************

net.sf.hibernate.Session sess = com.vhr.hibernate.HibernateUtil.currentSession();
net.sf.hibernate.Transaction tran = sess.beginTransaction();

com.vhr.Vehicle vehicleUpdate = (com.vhr.Vehicle) sess.load(com.vhr.Vehicle.class, new Long(request.getParameter("update")));

vehicleUpdate.getTheTrackingDevice().getStatusHistory().addNote(new com.vhr.util.Note("Test Status: "+request.getParameter("lmTestStatus")));

sess.save(vehicleUpdate);
tran.commit();
com.vhr.hibernate.HibernateUtil.closeSession();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 31, 2004 10:54 am 
Regular
Regular

Joined: Mon Feb 23, 2004 10:42 pm
Posts: 102
Location: Washington DC
Maybe try saveOrUpdate instead of save?

http://www.hibernate.org/hib_docs/refer ... g-detached

_________________
Matt Veitas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 01, 2004 5:38 pm 
Newbie

Joined: Fri Aug 29, 2003 4:36 am
Posts: 16
Location: Belgium
Quote:
<generator class="increment">
</generator>


It is not possible to use the increment generator when multiple applications access the same table. It only fetches a maximum at startup and does an internal increment from then on. Look at
http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-id-generator
and choose a different strategy.

Bart


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 01, 2004 5:53 pm 
Newbie

Joined: Tue Jul 13, 2004 5:38 pm
Posts: 10
Thank you Bart


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