-->
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: How to create object with FK that is a self ref many-to-one
PostPosted: Wed Mar 29, 2006 8:34 pm 
Newbie

Joined: Wed Mar 29, 2006 7:48 pm
Posts: 3
I have a class Asset (ID: sid) which has a many-to-one association (FK: derivedFromAssetId ) to another Asset.

When a new Asset is created, I want to default the value of the derivedFromAssetId to the id generated for the asset. I do not want to commit the asset until this value is assigned.

How can I do this? The following code is causing IllegalArgumentException exception to be thrown.

[java] 15:37:35,669 ERROR BasicPropertyAccessor:167 - IllegalArgumentException in class: com.wmg.gcdm.model.Asset, getter method of property: sid

full strack trace and mappings follow the code.


Code:
      private void createAndStoreAsset(String name, String type,
      String identifier) {

      Session session = HibernateUtil.getSessionFactory().getCurrentSession();

      session.beginTransaction();


      // Make a new Asset
      Asset theAsset = new Asset();

      // Set some attributes
      theAsset.setType(type);
      theAsset.setIdentifier(identifier);
      theAsset.setIdentifierType("ISRC");
      theAsset.setName(name);
      theAsset.setDescription(name);
      
      // this attribute will be updated with the ID assigned to the asset upon save
      theAsset.setDerivedFromAssetId(null);

      // Save the asset
      // Need the generated Id as default value for FK "derivedFromAssetId"
      Long generatedId = (Long) session.save(theAsset); [color=red]// this worked![/color]
      System.out.println("Generated Asset Id: " + generatedId);

      // Now that asset id has been assigned,
      // Update theAsset.derivedFromAssetId
      System.out.println(  "Asset Id from new persistent Asset: " + theAsset.getSid() );


      theAsset.setDerivedFromAssetId(  theAsset.getSid()  );
      session.saveOrUpdate(theAsset); // this gave exception

      // Tried reloading the asset before doing the update and got the same exception

      //theAsset = (Asset) session.load(Asset.class, generatedId);
      //theAsset.setDerivedFromAssetId(generatedId);



      session.saveOrUpdate(theAsset);

      session.getTransaction().commit();
    }




Hibernate version:

version 3.1.2, Jan 27, 2006

Mapping documents:

<hibernate-mapping >
<class name="Asset" table="asset">
<id name="sid" column="sid" type="long">
<generator class="hilo">
<param name="table">asset_hibernate_hi_lo</param>
<param name="column">next_hi</param>
<param name="max_lo">100</param>
</generator>
</id>

<version name="sysVersion" column="sys_version" type="integer"/>

<property name="type" type="string" column="type"/>
<property name="identifier" type="string" column="identifier"/>
<property name="identifierType" type="string" column="identifier_type"/>
<property name="name" type="string" column="name"/>
<property name="description" type="string" column="description"/>


<!-- Allow nulls - app to default the current asset as the derived from asset; user may override to another asset -->
<many-to-one name="derivedFromAssetId" column="derived_from_asset_sid" not-null="false " class="Asset" fetch="select"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Code:
   [java] Generated Asset Id: 808
    [java] Asset Id from new persistent Asset: 808
    [java] 15:37:35,669 ERROR BasicPropertyAccessor:167 - IllegalArgumentException in class: com.wmg.gcdm.model.Asset, getter method of property: sid
    [java] org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.wmg.gcdm.model.Asset.sid
    [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:172)
    [java]     at org.apache.tools.ant.taskdefs.Java.run(Java.java:705)
    [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:177)
    [java]     at org.apache.tools.ant.taskdefs.Java.execute(Java.java:83)
    [java]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
    [java]     at org.apache.tools.ant.Task.perform(Task.java:364)
    [java]     at org.apache.tools.ant.Target.execute(Target.java:341)
    [java]     at org.apache.tools.ant.Target.performTasks(Target.java:369)
    [java]     at org.apache.tools.ant.Project.executeTarget(Project.java:1214)
    [java]     at org.apache.tools.ant.Project.executeTargets(Project.java:1062)
    [java]     at org.apache.tools.ant.Main.runBuild(Main.java:673)
    [java]     at org.apache.tools.ant.Main.startAnt(Main.java:188)
    [java]     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:196)
    [java]     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:55)
    [java] Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.wmg.gcdm.model.Asset.sid
    [java]     at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
    [java]     at org.hibernate.tuple.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:176)
    [java]     at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3257)
    [java]     at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:2983)
    [java]     at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
    [java]     at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:215)
    [java]     at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
    [java]     at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:221)
    [java]     at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:476)
    [java]     at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:2803)
    [java]     at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:467)
    [java]     at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:190)
    [java]     at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)
    [java]     at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
    [java]     at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
    [java]     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
    [java]     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
    [java]     at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)
    [java]     at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    [java]     at com.wmg.gcdm.model.AssetManager.createAndStoreAsset(Unknown Source)
    [java]     at com.wmg.gcdm.model.AssetManager.main(Unknown Source)
    [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    [java]     at java.lang.reflect.Method.invoke(Method.java:324)
    [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:193)
    [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:130)
    [java]     ... 13 more
    [java] Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    [java]     at java.lang.reflect.Method.invoke(Method.java:324)
    [java]     at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
    [java]     ... 39 more
    [java] --- Nested Exception ---
    [java] org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.wmg.gcdm.model.Asset.sid
    [java]     at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
    [java]     at org.hibernate.tuple.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:176)
    [java]     at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3257)
    [java]     at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:2983)
    [java]     at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
    [java]     at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:215)
    [java]     at org.hibernate.type.EntityType.getIdentifier(EntityType.java:108)
    [java]     at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:221)
    [java]     at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:476)
    [java]     at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:2803)
    [java]     at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:467)
    [java]     at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:190)
    [java]     at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:113)
    [java]     at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
    [java]     at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
    [java]     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
    [java]     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1009)
    [java]     at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:356)
    [java]     at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    [java]     at com.wmg.gcdm.model.AssetManager.createAndStoreAsset(Unknown Source)
    [java]     at com.wmg.gcdm.model.AssetManager.main(Unknown Source)
    [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    [java]     at java.lang.reflect.Method.invoke(Method.java:324)
    [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:193)
    [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:130)
    [java]     at org.apache.tools.ant.taskdefs.Java.run(Java.java:705)
    [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:177)
    [java]     at org.apache.tools.ant.taskdefs.Java.execute(Java.java:83)
    [java]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
    [java]     at org.apache.tools.ant.Task.perform(Task.java:364)
    [java]     at org.apache.tools.ant.Target.execute(Target.java:341)
    [java]     at org.apache.tools.ant.Target.performTasks(Target.java:369)
    [java]     at org.apache.tools.ant.Project.executeTarget(Project.java:1214)
    [java]     at org.apache.tools.ant.Project.executeTargets(Project.java:1062)
    [java]     at org.apache.tools.ant.Main.runBuild(Main.java:673)
    [java]     at org.apache.tools.ant.Main.startAnt(Main.java:188)
    [java]     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:196)
    [java]     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:55)
    [java] Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
    [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    [java]     at java.lang.reflect.Method.invoke(Method.java:324)
    [java]     at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
    [java]     ... 39 more

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 10:11 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
How are you calling " theAsset.setDerivedFromAssetId( theAsset.getSid() ); " when DerivedFromAssetId is an Asset but Sid is a long? That's what's causing the current exception. Try " theAsset.setDerivedFromAssetId( theAsset); " instead.

You'll always have to save twice for a new Asset, because of referential integrity. Even if you changed your id generator to "assigned", allowing you to fill in all the details in advance, it wouldn't work because the id you put into the derivedFrom... column wouldn't yet exist in the sid column. Your current solution works, once you put an Asset object into the derivedFrom... property, instead of using its id.


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Wed Mar 29, 2006 10:30 pm 
Newbie

Joined: Wed Mar 29, 2006 7:48 pm
Posts: 3
Got it! Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 29, 2006 10:36 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Cool. Don't forget to rate! (Only one more level to go! I wanna know what's after "Expert" ;)


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.