-->
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.  [ 2 posts ] 
Author Message
 Post subject: many-to-one entity with foreign key saving problem
PostPosted: Thu Jan 19, 2006 2:33 pm 
Newbie

Joined: Wed Jan 18, 2006 2:19 pm
Posts: 14
Hi, I have 2 classes:

public class A implements Serializable(){
private Integer id;
private Integer foreignId;
A(){}
public void setId(Integer id){this.id=id;}
public Integer getId(){return this.id;}
public void setForeignId(Integer id){this.foreignId=id;}
public Integer getForeignId(){return this.foreignId;}
}

public class B implements Serializable(){
private Integer id;
private Integer something;
B(){}
public void setId(Integer id){this.id=id;}
public Integer getId(){return this.id;}
public void setSomething(Integer s){this.something=s;}
public Integer getSomething(){return this.something;}
}

and mappings

<hibernate-mapping>
<class name="A" table="a">
<id name="id">
<generator class="sequence"/>
</id>
<many-to-one name="foreignId" insert="true" class="B" />
</hibernate-mapping>

<hibernate-mapping>
<class name="B" table="b">
<id name="id">
<generator class="sequence"/>
</id>
<property name="something"/>
</hibernate-mapping>

in table b, i have the raw |1|2|
and then

tx=session.beginTransaction();
A a=new A();
a.setForeignId(new Integer(1)); //from table b
session.save(a);
tx.commit();

after commit, the exception is thrown

Exception in thread "main" java.lang.RuntimeException: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of B.id
at newB(MyUtil.java:350)
at Main.main(Main.java:45)
Caused by: org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of B.id
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
at org.hibernate.tuple.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:176)
at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3257)
at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:2983)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:137)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:69)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:47)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:263)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:114)
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:557)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:545)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:541)
at cz.muni.fi.xsubcik.lis.session.Librarian.newItem(Librarian.java:345)
... 1 more
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
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:585)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
... 20 more
Java Result: 1

what i am doing wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 2:47 pm 
Newbie

Joined: Thu Jul 14, 2005 5:39 pm
Posts: 12
Location: Ann Arbor, MI
(Just in case you're not watching the other thread anymore: http://forum.hibernate.org/viewtopic.php?p=2286935#2286935)

-- Jim


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