Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Persisting entity with @NaturalId problem (MySQL)
PostPosted: Sun Apr 08, 2012 4:07 am 
Newbie

Joined: Thu May 20, 2010 1:39 am
Posts: 6
It seems like a problem with @NaturalId. I have MySQL database and User entity with natural id:
Code:
@Entity
@Table(name="user")
public class User {
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Integer id;
...
   @NotNull
   @NaturalId
   @Pattern(regexp = "[a-z\\d]+", message = "invalid.login.nickname")
   private String name;
...
}

When I insert a new entity with JPA api NaturalIdResolutionCache throws NPE. Stacktrace:
Code:
SEVERE: Servlet.service() for servlet springServlet threw exception
java.lang.NullPointerException
   at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:768)
   at org.hibernate.engine.internal.NaturalIdXrefDelegate$NaturalIdResolutionCache.cache(NaturalIdXrefDelegate.java:454)
   at org.hibernate.engine.internal.NaturalIdXrefDelegate.cacheNaturalIdCrossReference(NaturalIdXrefDelegate.java:92)
   at org.hibernate.engine.internal.StatefulPersistenceContext$1.manageLocalNaturalIdCrossReference(StatefulPersistenceContext.java:1769)
   at org.hibernate.action.internal.AbstractEntityInsertAction.handleNaturalIdPreSaveNotifications(AbstractEntityInsertAction.java:184)
   at org.hibernate.action.internal.AbstractEntityInsertAction.<init>(AbstractEntityInsertAction.java:75)
   at org.hibernate.action.internal.EntityIdentityInsertAction.<init>(EntityIdentityInsertAction.java:55)
   at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:317)
   at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)
   at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
   at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)
   at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)
   at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)
   at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)
   at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
   at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
   at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:786)
   at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:790)
   at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:859)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
   at $Proxy52.persist(Unknown Source)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
   at $Proxy52.persist(Unknown Source)
...

The reason of exception is that MySQL generates id value AFTER query is executed.

Please let me know if you need any additional info.

Regards,
Maksim Chemerisuk


Top
 Profile  
 
 Post subject: Re: Persisting entity with @NaturalId problem (MySQL)
PostPosted: Sun Apr 08, 2012 9:30 am 
Newbie

Joined: Sun Apr 08, 2012 9:29 am
Posts: 1
It has no problem when I use 4.1.0.Final, but when i switch 4.1.2.Final, I have the same problem.

My Hibernate config:
Code:
cfg.setProperty(DIALECT, "org.hibernate.dialect.MySQL5InnoDBDialect")
     .setProperty(HBM2DDL_AUTO, "update")
     .setProperty(USE_SECOND_LEVEL_CACHE, "false")
     .setProperty(ISOLATION, "2")


Code:
class Company {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    var id : Long = _

    @Version
    @Column(name = "version")
    var version : Int = _

    @NaturalId(mutable = true)
    var shortCode: String = _
    @Column(unique = true, nullable = false)
    var name:String = _
}


I did not do any query before save.


Top
 Profile  
 
 Post subject: Re: Persisting entity with @NaturalId problem (MySQL)
PostPosted: Tue Apr 10, 2012 3:24 pm 
Newbie

Joined: Thu May 20, 2010 1:39 am
Posts: 6
Checked on the 4.1.1.Final - the same scenario works properly. Pretty sure It's a 4.1.2 version bug.


Top
 Profile  
 
 Post subject: Re: Persisting entity with @NaturalId problem (MySQL)
PostPosted: Wed Apr 18, 2012 2:14 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 988
This is fixed with HHH-7253 (version 4.1.3)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.