Hi all,
I am getting a NullPointerException when trying to save a Hibernate-managed entity modeled as a joined subclass (stacktrace at bottom of the post). The subclass previously did not have an additional table, so this problem was introduced when I introduced the joined-subclass mapping.
The mentions of the stacktrace I have found recommend double-checking the mappings, which can cause this confusing error. My mappings seem right, as far as I can tell.
The base class is AvatarEntity, which is versioned using optimistic locking and an OBJECT_VERSION column on the table. The joined subclass is RomeoAvatarEntity.
The exception gets thrown when a RomeoAvatarEntity is initially saved. One of the setters does a calculation that requires looking up some other entities in the process. When this is done, the session is autoflushed. The EntityEntry corresponding to the original RomeoAvatarEntity is found (even though the status is "SAVING") and is scheduled for update. It tries to increment the entry's version, which is null.
I have stepped through the Hibernate code, and it is unclear to me what the root cause is. Any help is greatly appreciated. Below are the relevant mappings. Note that AvatarEntity has a separate version field for application use, in addition to the objectVersion.
Paul
Code:
<hibernate-mapping>
<class
name="com.myco.common.avatar.AvatarEntity"
table="AVA_AVATAR"
dynamic-update="true"
optimistic-lock="version"
>
<id
name="id"
column="ID"
type="java.lang.Integer"
length="11"
>
<generator class="org.hibernate.id.MultipleHiLoPerTableGenerator">
<param name="max_lo">100</param>
</generator>
</id>
<version
name="objectVersion"
column="OBJECT_VERSION"
type="long"
access="field"
unsaved-value="negative"
/>
<property
name="version"
type="int"
update="true"
insert="true"
column="VERSION"
/>
<property
name="accountId"
type="java.lang.Integer"
update="true"
insert="true"
column="ACCOUNT_ID"
/>
...
<set
name="rawInventory"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
>
<key column="AVATAR_ID"></key>
<one-to-many class="com.myco.common.asset.InventoryEntity"/>
</set>
<map name="avatarProperties" cascade="all-delete-orphan" access="field" lazy="true" inverse="true" fetch="subselect">
<!-- <cache usage="transactional"/> -->
<key column="AVATAR_ID" not-null="true"/>
<map-key formula="PROPERTY_NAME" type="string"/>
<one-to-many class="com.dchoc.common.avatar.property.PropertyEntity"/>
</map>
<joined-subclass
name="com.dchoc.app.romeo.RomeoAvatarEntity"
table="RM_AVATAR"
dynamic-update="true"
>
<key column="ID"/>
<property
name="gender"
type="java.lang.String"
update="true"
insert="true"
column="GENDER"
/>
...
</joined-subclass>
</class>
</hibernate-mapping>
Code:
org.hibernate.PropertyAccessException: Exception occurred inside setter of com.myco.app.romeo.RomeoAvatarEntity.location
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:65)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.setPropertyValues(AbstractEntityTuplizer.java:337)
at org.hibernate.tuple.entity.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:200)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3514)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
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:324)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.save(Unknown Source)
at com.myco.common.persistence.Entity.create(Entity.java:174)
at com.myco.common.avatar.AvatarTestUtil.createAvatar(AvatarTestUtil.java:67)
at com.myco.app.romeo.RomeoAvatarTestUtil.createAvatar(RomeoAvatarTestUtil.java:77)
at com.myco.app.romeo.TestRomeoAvatarEntity.setUp(TestRomeoAvatarEntity.java:39)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.lang.reflect.InvocationTargetException
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:324)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:42)
... 37 more
Caused by: java.lang.NullPointerException
at org.hibernate.type.LongType.next(LongType.java:56)
at org.hibernate.engine.Versioning.increment(Versioning.java:25)
at org.hibernate.event.def.DefaultFlushEntityEventListener.getNextVersion(DefaultFlushEntityEventListener.java:358)
at org.hibernate.event.def.DefaultFlushEntityEventListener.scheduleUpdate(DefaultFlushEntityEventListener.java:250)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:121)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:35)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at com.myco.common.asset.AssetRatingEntity.findAllByTagsAndLocation(AssetRatingEntity.java:46)
at com.myco.app.romeo.util.LooksCalculator.calculateLooks(LooksCalculator.java:169)
at com.myco.app.romeo.RomeoAvatarEntity.calculateLooks(RomeoAvatarEntity.java:634)
at com.myco.app.romeo.RomeoAvatarEntity.setLocation(RomeoAvatarEntity.java:166)
... 42 more