Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Hibernate create: null error

Thread view: 
Jacqui - 11 Oct 2005 17:40 GMT
Hi,

I have been puzzling over this for the last couple of days and was
wondering if any of you could help me.

I get the following error when I try to load a jsp in my webapp:

Error looking up property "town" in object type
"com.moveassist.reloassist.model.Address$$EnhancerByCGLIB$$48a8613c".
Cause: null

I know that the EnhancerByCGLIB part means that it has not initialised
the proxy but I am unsure why it would not do this and I also do not
know what the Cause: null might mean.

I have the following in Address.hbm.xml:

   <class
       name="com.moveassist.reloassist.model.Address"
       lazy="true"
   >

and in Company.hbm.xml (which has a many-to-one relationship with the
address table and is the object the Address is a property of in the
exception above) I have:

       <many-to-one
           name="address"
           class="com.moveassist.reloassist.model.Address"
           cascade="none"
           outer-join="true"
           update="true"
           insert="true"
           column="address"
       />

I have read in the excellent book 'Hibernate In Action' that setting
lazy="true" and outer-join="true" usually makes the proxy initialised,
but not in this occasion!  Could someone please give me pointer in the
right direction!!  If you need further info, please let me know.

Thanks in advance for your help and time.

Jacqueline
Ross Bamford - 11 Oct 2005 18:23 GMT
> Hi,
>
[quoted text clipped - 40 lines]
>
> Jacqueline

I have seen similar errors from Hibernate when the reflection optimizer  
couldn't find a class - it turns out it's hardcoded to the system  
classloader for a number of things, so do you have any custom  
classloading? I've found that 'lazy' usually does the trick to ensure  
initialization is done properly, but that you have to take care to specify  
'inverse' appropriately with bidi collections. I also like to specify the  
collection lazy, rather than the class, but I guess it depends on what  
you're trying to achieve.

Apart from that, make sure that you have a default constructor of at least  
package private visibility, and ensure that your classes, getters/setters  
and fields (obv) aren't final.

You might also add the following to your hibernate.properties:

    hibernate.cglib.use_reflection_optimizer=false

And seeing what that does. You might find a (much) more descriptive error  
message hiding there. I've had to use this before when there have been  
problems with certain classes, though I can't remember the exact causes...

If you're still stuck, it would be helpful to see at least Address.hbm.xml  
and hibernate.cfg.xml in full.

Signature

Ross Bamford (rosco@roscopeco.remove.co.uk)

Jacqui - 12 Oct 2005 09:38 GMT
Thanks for your quick response!

I have checked both the Company and Address classes for final
fields/methods and there are none there.

I have also followed your suggestion and put the line in the
hibernate.properties file and this has not added any further info to
the error messages.

We are using Spring to initialize the beans, and the following is the
config in the spring.xml file:

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="mappingDirectoryLocations">
       <list>
           <value>classpath:/com/moveassist/reloassist/model</value>
        </list>
    </property>
    <property name="hibernateProperties">
       <props>
           <prop
key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        </props>
   </property>
    <property name="dataSource">
       <ref bean="dataSource"/>
    </property>
</bean>

This setup works fine for other classes/relationships, we are just
having problems with this one.

As requested, Address.hbm.xml in full:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping

   <class
       name="com.moveassist.reloassist.model.Address"
       lazy="true"
   >

       <id
           name="id"
           column="id"
           type="java.lang.Integer"
       >
           <generator class="native">
             <!--
                 To add non XDoclet generator parameters, create a
file named
                 hibernate-generator-params-Address.xml
                 containing the additional parameters and place it in
your merge dir.
             -->
           </generator>
       </id>

       <property
           name="address"
           type="java.lang.String"
           update="true"
           insert="true"
           column="address"
       />

       <property
           name="street"
           type="java.lang.String"
           update="true"
           insert="true"
           column="street"
       />

       <property
           name="town"
           type="java.lang.String"
           update="true"
           insert="true"
           column="town"
       />

       <property
           name="county"
           type="java.lang.String"
           update="true"
           insert="true"
           column="county"
       />

       <property
           name="postalCode"
           type="java.lang.String"
           update="true"
           insert="true"
           column="postalCode"
       />

       <many-to-one
           name="country"
           class="com.moveassist.reloassist.model.Country"
           cascade="none"
           outer-join="true"
           update="true"
           insert="true"
           column="country"
       />

       <!--
           To add non XDoclet property mappings, create a file named
               hibernate-properties-Address.xml
           containing the additional properties and place it in your
merge dir.
       -->

   </class>

</hibernate-mapping>

Company.hbm.xml in full:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping

   <class
       name="com.moveassist.reloassist.model.Company"
   >

       <id
           name="code"
           column="code"
           type="java.lang.String"
       >
           <generator class="assigned">
             <!--
                 To add non XDoclet generator parameters, create a
file named
                 hibernate-generator-params-Company.xml
                 containing the additional parameters and place it in
your merge dir.
             -->
           </generator>
       </id>

       <property
           name="name"
           type="java.lang.String"
           update="true"
           insert="true"
           column="name"
       />

       <many-to-one
           name="address"
           class="com.moveassist.reloassist.model.Address"
           cascade="none"
           outer-join="true"
           update="true"
           insert="true"
           column="address"
       />

       <property
           name="accountsCode"
           type="java.lang.String"
           update="true"
           insert="true"
           column="accountsCode"
       />

       <many-to-one
           name="companyHead"
           class="com.moveassist.reloassist.model.Employee"
           cascade="none"
           outer-join="auto"
           update="true"
           insert="true"
           column="companyHead"
       />

       <set
           name="offices"
           lazy="true"
           cascade="all"
           sort="unsorted"
       >

           <key
               column="company"
           >
           </key>

           <one-to-many
                 class="com.moveassist.reloassist.model.Office"
           />

       </set>

       <!--
           To add non XDoclet property mappings, create a file named
               hibernate-properties-Company.xml
           containing the additional properties and place it in your
merge dir.
       -->

   </class>

</hibernate-mapping>

If it helps, we also have another class 'CorporateAccount' which has an
address, and this initializes properly.

Thanks for the help so far, I look forward to hearing from you again!

Jacqueline
Ross Bamford - 12 Oct 2005 11:02 GMT
> Thanks for your quick response!
>
[quoted text clipped - 4 lines]
> hibernate.properties file and this has not added any further info to
> the error messages.

Thanks for sending over your mappings! I have a few suggestions:

1) Check that the database column 'town' really does exist.

2) Remove 'lazy=true' from the class definition. I think you can only  
switch _off_ laziness for a whole class.

3) Remove the 'outer-join=true' from the many-to-one entries. IIRC it's  
not supported there (any more?).

4) Remove the 'insert' and 'update' entries, because they default true  
anyway.

If none of that helps, next:

5) Disable laziness explicitly with 'lazy=false' on the many-to-one

5) Look into your cascades - why aren't you cascading events on that  
class? Is there some particular reason for that? If so, are you making  
sure you persist and update everything as required by the Company class?  
If not, set cascade 'all' for now.

6) Comment out the 'town' property in Address.hbm.xml, to see if you still  
get the problem. If so, it's likely the problem is actually a symptom of a  
more specific problem loading that column from the database.

6) Set the property 'hibernate.show_sql' in either the config or your  
hibernate properties. This will make hibernate output the SQL statements  
it's about to execute to System.out. Look for the commands it executes  
leading upto the problem. If nothing immediately seems wrong with it, fire  
up the mysql console and try those same commands with some test data.

Unfortunately in my experience theres usually some trial and error  
involved with fixing any reasonably complex hibernate model, and it's  
often the performance optimizations that cause problems. IMHO there are  
too many conditional features in hibernate, in that you can do this, but  
only if you're doing this, and this, and not doing something else, so the  
approach I usually use is to build the model incrementally, starting with  
the very basics, and then gradually building in optimizations and stuff,  
always checking each change still works.

> If it helps, we also have another class 'CorporateAccount' which has an
> address, and this initializes properly.

Good. The Address mapping itself looks okay (apart from the redundancy  
mentioned above). Try comparing the way you've mapped the address from  
there to see if anything's done differently.

Hopefully this will at least give a more descriptive error to work on ;)

Signature

Ross Bamford - rosco@roscopeco.remove.co.uk



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.