-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate 3.6.4: NonUniqueObjectException when inserting
PostPosted: Tue Jan 31, 2012 9:18 am 
Newbie

Joined: Wed Aug 27, 2003 3:47 am
Posts: 4
Location: Frankfurt, Germany
Hi all,

I have a spring roo webapp which uses Hibernate (3.6.2) as JPA. The entities primary keys are annotated with @Id and @GeneratedValue(strategy = GenerationType.AUTO). The webapp is running fine and I may insert/update/delete records in any table at will. I have a data interface outside the webapp implemented in Pentaho Kettle 4.2.1 aka PDI which periodically inserts and updates records in the database and of course increases the primary keys (strategy there is max(id)+1).

When does hibernate get a new primary key if the strategy is set to AUTO? If have tried the GenerationType.TABLE with no luck. I may do not use GenerationType.SEQUENCE.

May I refresh the internal hibernate PK counters? Do I have to restart the persistanceUnit in some sort? If so how would i do that?

Cheers
A Noob


Top
 Profile  
 
 Post subject: Re: Hibernate 3.6.4: NonUniqueObjectException when inserting
PostPosted: Tue Jan 31, 2012 11:13 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi,

According documentation GenerationType.AUTO always does a fallback to either
Identity-, Sequence- or Hilo-generator
depending on the capabilities of the underlying database.

Following your comments I guess in your case it falls back to GenerationType.TABLE.

If true then on your database there must exist a table named hibernate_sequences with
following 2 columns:
sequence_name (varchar](255) NULL) and
sequence_next_hi_value (int NULL)

This table holds a hilo value for each table.
Quote:
When does hibernate get a new primary key ?

Hibernate gets the new primary key when you persist a new entity object.

Quote:
May I refresh the internal hibernate PK counters?


As I know this is not possible, unless you implement and use a own custom generator.


Quote:
Do I have to restart the persistanceUnit in some sort?


Yes. If in meantime you have altered the content of table hibernate_sequences outside hibernate
(for example within PDI) then you must close and restart the entitymanager factory
in order that the changed values in table hibernate_sequences are considered by hibernate

Code:
_entityManagerFactory.close();
_entityManagerFactory= javax.persistence.Persistence.createEntityManagerFactory(...);



An alternative approach would consist in using identity generator type which is supported by databases like DB2,MySQL,MS-SQL , Sybase ,HSQLDB etc. making the DB is responsible to assign the primary keys for new inserted objects.

Advantages of this approach:
-no matter about duplicate assigns or wrong set sequence because the db takes the full responsibility
-insertions of new records also feasible also outside hibernate application.

Disadvantages:
-only applicable with table per class-hierarchy mapping (aka flat-mapping).
-no deferring (batching) of jdbc statement possible on insertions because hibernate needs to know the assigned id immediately
(= extra jdbc round trip on makePersistent)
-You need to bootstrap your database again when switching between table and identity generator, because the schema changes.


Top
 Profile  
 
 Post subject: Re: Hibernate 3.6.4: NonUniqueObjectException when inserting
PostPosted: Tue Jan 31, 2012 11:45 am 
Newbie

Joined: Wed Aug 27, 2003 3:47 am
Posts: 4
Location: Frankfurt, Germany
I will first play with the hibernate_sequence table and restarting the EMF and if that fails revert to the SEQUENCE strategy.

You are great!

Thanks


Top
 Profile  
 
 Post subject: Re: Hibernate 3.6.4: NonUniqueObjectException when inserting
PostPosted: Thu Feb 02, 2012 4:00 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi,

an alternative to restart the emf would be following:
in some way you tell the hibernate-application how many primary keys where assigned outside hibernate within PDI.
Then in hibernate you blow away exaclty the same number of primary keys without ever propagating them to the database.
In this way you gain again up to the true next free number.

Code:
void wastePrimaryKeys(int usedByPDI) {
session.beginTransaction();
for (int=0;i < usedByPDI; i++) {
    MyPersistentClass a = new MyPersistentClass ();
    session.persist(arg0); // this increments the primary keys but does not do the actual insert, unless you use identity generator
}
session.getTransaction().rollback(); // so finally the inserts are never made
}


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