Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

many to one unidirectional propertyaccessexception

 
Hariharansr Sethuramanhh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Foo class
---------------
public class foo {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Bar class
---------
public class bar {
private Long id;
private String name;
private Long fooid;
public Long getFooid() {
return fooid;
}
public void setFooid(Long fooid) {
this.fooid = fooid;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}


foobar.hbm.xml

<hibernate-mapping>

<class name="foobar.foo" table="FOO">
<id name="id" column="FOOID">
<generator class="increment" />
</id>
<property name="name" column="FOONAME" />
</class>

<class name="foobar.bar" table="BAR">
<id name="id" column="BARID">
<generator class="increment" />
</id>
<property name="name" column="BARNAME" />
<many-to-one name="fooid" column="FOOID" not-null="true" class="foobar.foo"/>
</class>

</hibernate-mapping>


Exception trace

[java] Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of foobar.foo.id
[java] at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:171)
[java] at org.hibernate.tuple.entity.AbstractEntityTuplizer.getIdentifier(AbstractEntityTuplizer.java:183)
[java] at org.hibernate.persister.entity.AbstractEntityPersister.getIdentifier(AbstractEntityPersister.java:3591)
[java] at org.hibernate.persister.entity.AbstractEntityPersister.isTransient(AbstractEntityPersister.java:3307)
[java] at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
[java] at org.hibernate.engine.ForeignKeys$Nullifier.isNullifiable(ForeignKeys.java:137)
[java] at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:69)
[java] at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:47)
[java] at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
[java] at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
[java] at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
[java] at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
[java] at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
[java] at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
[java] at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
[java] at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
[java] at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
[java] at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
[java] at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
[java] at foobar.create.main(Unknown Source)
[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(Unknown Source)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
[java] at java.lang.reflect.Method.invoke(Unknown Source)
[java] at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:145)
[java] ... 19 more
[java] Java Result: 1


Help me to get rid :roll:
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a type mismatch.

Basically in your Java object you have an attribute of type Long for the FK id. It should be of the type of object that it is referring to and not a Long, if you are going to use the Many-To-One.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic