-->
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: @PrePersist
PostPosted: Wed Mar 28, 2007 11:34 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
I am using compound primary keys, which have to be manually assigned before the POJO is persisted.
One way I thought of doing this was by using the @PrePersist annotation and then assigning it there.

However, when I do this, I get the exception:

detailMessage= "org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():Person"
count= 150
hash= 0
offset= 0
value= char[150] (id=3377)

I wrote a interceptor and tried to do the assign in the onSave() method but I notice that the same happens there as well. Hibernate seems to check that the Entity has an Id before invoking the PrePersist or onSave methods.

Any ideas how to get around this? I would have thought the preSave(0 method should get called before any save implementation code kicks in.

Regards[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 12:03 pm 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Hi,
Just some further info to above, the issue happens irrespective of whether a transaction is active or not, and irrespective of whether the @PrePersist is put in the POJO or in a separate Entity Listener class.

It would appear that Hibernate / JPA stipulates that you must have an ID for a POJO entity before it goes near the EntityManager.

Comments appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 11:38 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Please use the standard question template, you're missing a lot of info

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 6:04 am 
Senior
Senior

Joined: Mon Jul 24, 2006 8:43 am
Posts: 160
Apologies, the question is:

I am using compound primary keys for my Entity POJOs.
As per the JPA spec compound primary keys have to be manually assigned.
I was going to try to automate this by using the PrePersist Lifecycle event.
For example, the POJO would look like:

Code:

@Entity
public class Person {
      private PrimaryKey key;
     
      @PrePersist
      public void assginKey() {
           // Use the factory to assign the key.
           key = PrimaryKeyFactory.createPrimaryKey();
      }
}



However, when persist() is called for instances of Person, Hibernate checks to see if the primary key is assigned, before firing the PrePersist event. Because I assign my key in the PrePersist even handling, i.e. the assignKey() method, the key is null when hibernate checks, hibernate throws the following exception:


detailMessage= "org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():Person"


I did some investigations and I found that hibernate will chuck this exception regardless of whether a transaction has started or not.
Hibernate will also chuck this exception if you instead of using the POJO lifecycle framework you use the Interceptor framework.

For example, if you were to configure the following interceptor:

Code:

public class TestInterceptor extends EmptyInterceptor {

public boolean onSave(Object entity, Serializable id, Object [] state,
            String [] propertyNames, Type [] types) throws CallbackException {
         if (entity instanceof Person) {
             // code to set thhe key for person.
             .
             .
             //
         }
        return true;
    }


Hibernate again, does the check to see if the Entity has the primary key assigned before invoking the onSave(). In my case this means the same exception will be thrown.

Are my right in saying that:
1. The primary key for any POJO entity must be set before em.persist() is invoked for it and there are no exceptions to this rule.
2. There are no other events or lifecycle events that can help me out here, i.e. that are are fired before the hibernate does the check to see if the primary key is assigned.

Kind regards


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.