-->
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.  [ 7 posts ] 
Author Message
 Post subject: exception setting property value with CGLIB
PostPosted: Wed Mar 10, 2010 4:53 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I get an error which I don't understand.
My mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="data.City" table="cities">
      <id name="id" column="id" length="20">
         <generator class="assigned" />
      </id>

      <property name="country">
         <column name="country" not-null="true" />
      </property>

      <many-to-one name="Country" class="data.Country" column="country"
         insert="false" update="false" />
   </class>
</hibernate-mapping>


The java class:
Code:
package data;

import hibernate.IdCounter;

public class City
{
   private String id, country;
   
   public City(String country)
   {
      setId(id = IdCounter.getInstance().getCityId());
      this.country = country;
   }
   
   public City()
   {
      
   }
   
   public String getId()
   {
      return id;
   }
   
   public String getCountry()
   {
      return country;
   }
   
   private void setId(String id)
   {
      this.id = id;
   }
   
   public void setCountry(String country)
   {
      this.country = country;
   }
}


And the error:
Code:
Exception in thread "main" org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of data.City.setCountry
   at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:197)
   at org.hibernate.tuple.PojoTuplizer.setPropertyValues(PojoTuplizer.java:167)
   at org.hibernate.persister.entity.BasicEntityPersister.setPropertyValues(BasicEntityPersister.java:2879)
   at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
   at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:452)
   at org.hibernate.loader.Loader.doQuery(Loader.java:406)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:203)
   at org.hibernate.loader.Loader.loadEntity(Loader.java:1255)
   at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:139)
   at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:124)
   at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2453)
   at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:387)
   at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:368)
   at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:166)
   at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:140)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:119)
   at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:571)
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:59)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:80)
   at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:133)
   at data.City$$EnhancerByCGLIB$$915e1df.getCountry(<generated>)
   at main.HibernateMain.main(HibernateMain.java:26)
Caused by: net.sf.cglib.beans.BulkBeanException: data.Country$$EnhancerByCGLIB$$e5b5572
   at data.City$$BulkBeanByCGLIB$$28f44616.setPropertyValues(<generated>)
   at org.hibernate.tuple.PojoTuplizer.setPropertyValuesWithOptimizer(PojoTuplizer.java:194)
   ... 21 more
Caused by: java.lang.ClassCastException: data.Country$$EnhancerByCGLIB$$e5b5572
   ... 23 more


I've set hibernate.cglib.use_reflection_optimizer=false but the error didn't change.
The setter should be fine so I quess something is wrong in the mapping.

And I'm new to hibernate.


Top
 Profile  
 
 Post subject: Re: exception setting property value with CGLIB
PostPosted: Wed Mar 10, 2010 5:43 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
Hope this link can help you
http://www.coderanch.com/t/215814/Objec ... g-property


Top
 Profile  
 
 Post subject: Re: exception setting property value with CGLIB
PostPosted: Wed Mar 10, 2010 6:20 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The problem is that you don't have a setter/getter for the <many-to-one> association and that there is a name clash with the <property name="country">.

As an example, change the <many-to-one> to:

Code:
<many-to-one name="countryObject" class="data.Country" column="country"
         insert="false" update="false" />


and then add setter/getter methods to the City class:

Code:
private Country countryObject;
public Country getCountryObject()
{
  return countryObject;
}
public void setCountryObject(Country countryObject)
{
  this.countryObject = countryObject;
}


You may also want to make sure the object and string representation of the country remains synchronized when one of the setCountry() or setCountryObject() methods are called.


Top
 Profile  
 
 Post subject: Re: exception setting property value with CGLIB
PostPosted: Wed Mar 10, 2010 7:10 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
nordborg wrote:
Code:
private Country countryObject;
public Country getCountryObject()
{
  return countryObject;
}
public void setCountryObject(Country countryObject)
{
  this.countryObject = countryObject;
}


You may also want to make sure the object and string representation of the country remains synchronized when one of the setCountry() or setCountryObject() methods are called.


In your example, is countryObject a new object a side from id and country?
I quess this is used to make the relation to the Country class.
I used country as Foreign Key, will countryObject replace this and make a FK in de db, or is it just for the relation in java.

Edit:
I gave city a countryFK (Country) variable with getter and setter and the error was gone.
Do I still need the country (String) variable or can hibernate handle this and make the FK?

Edit 2:
I tested it in one class and it works.
Now I have the following. If anyone sees something I can improve, please tell me?

My mapping is now:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="data.City" table="cities">
      <id name="id" column="id" length="20">
         <generator class="assigned" />
      </id>

      <many-to-one name="country" class="data.Country" column="country"
         insert="false" update="false" />
   </class>
</hibernate-mapping>


And the java class:
Code:
package data;

import hibernate.IdCounter;

public class City
{
   private String id;
   private Country country;
   
   public City(String country)
   {
      setId(id = IdCounter.getInstance().getCityId());
   }
   
   public City()
   {
      
   }
   
   public String getId()
   {
      return id;
   }
   
   public Country getCountry()
   {
      return country;
   }
   
   private void setId(String id)
   {
      this.id = id;
   }
   
   public void setCountry(Country country)
   {
      this.country = country;
   }
}


Top
 Profile  
 
 Post subject: Re: exception setting property value with CGLIB
PostPosted: Wed Mar 10, 2010 7:38 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Since you had mapped the "country" column to both a <property> and a <many-to-one> I assumed that you for some reason wanted to have it like this. The usual case is to just have the <many-to-one> and as you have discovered yourself it works. You need to remove the insert="false" and update="false" from the mapping though, otherwise you will not get any values in the country column.


Top
 Profile  
 
 Post subject: Re: exception setting property value with CGLIB
PostPosted: Wed Mar 10, 2010 8:01 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
nordborg wrote:
Since you had mapped the "country" column to both a <property> and a <many-to-one> I assumed that you for some reason wanted to have it like this. The usual case is to just have the <many-to-one> and as you have discovered yourself it works. You need to remove the insert="false" and update="false" from the mapping though, otherwise you will not get any values in the country column.


Thanks for the reply. Like I said I'm new to Hibernate, but I think I got it working now.


Top
 Profile  
 
 Post subject: Re: exception setting property value with CGLIB
PostPosted: Wed Mar 10, 2010 8:21 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
Edit: solved it myself.


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