-->
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.  [ 3 posts ] 
Author Message
 Post subject: CGLIB loading objects incorrectly
PostPosted: Thu Aug 09, 2007 4:33 am 
Newbie

Joined: Sat Jul 14, 2007 11:50 am
Posts: 12
Location: the Burgh
This issue is driving me crazy… There are two relevant classes Page, TextInstance, and TextAttributes. Pages have TextInstances and one TextAttribute. Every TextInstance has a TextAttribute. They are structured as follows:

Code:

<hibernate-mapping>
   <class name="beans.Page" table="PAGE">
   
      <id name="id" type="long" column="PAGE_ID">
         <meta attribute="scope-set">protected</meta>
         <generator class="foreign">
            <param name="property">ta</param>
         </generator>
      </id>

      <list name="imageInstances" table="PAGE_IMAGE_INSTANCE" cascade="all-delete-orphan">
         <key column="PAGE_ID"/>
         <list-index column="IMG_INS_INDEX"/>
         <one-to-many class="beans.ImageInstance"/>
      </list>

    <one-to-one name="ta" class="beans.TextAttributes" constrained="true" cascade="all"/>

   </class>   
</hibernate-mapping>

<hibernate-mapping>
   <class name="beans.TextInstance" table="TEXT_INSTANCE">
      
      <id name="id" type="long" column="TEXT_INSTANCE_ID">
         <meta attribute="scope-set">protected</meta>
      <generator class="foreign">
        <param name="property">ta</param>
      </generator>
      </id>
      
      <property name="body" type="text" not-null="true"/>

      <one-to-one name="ta" class="beans.TextAttributes" constrained="true" lazy="false" cascade="all"/>
   
      <many-to-one name="page" class="beans.Page" not-null="true"/>
   </class>   
</hibernate-mapping>

<hibernate-mapping>
   <class name="beans.TextAttributes" table="TEXT_ATTRIBUTES">
   
      <id name="id" type="long" column="TEXT_ATTRIBUTES_ID">
         <meta attribute="scope-set">protected</meta>
         <generator class="native"/>
      </id>
      
   </class>   
</hibernate-mapping>



The problem I'm having stems from the lazy initialization of the Page's TextAttribute. Immediately after creating a Page and a TextAttribute the Variable explorer in Eclipse shows this:

Name, Value

page > ta, TextAttributes (id=134)
page > ta > id, 1
page > textInstances > list > elementData > 0 > ta > id, 2

After I've logged out and back in... once I've loaded the page through my DAO or Hibernate.get or Hibernate.load (I've tried them all) the Eclipse Variable explorer shows this:

Name, Value

page > ta, TextAttributes$$EnhancerByCGLIB$$d6d48c34 (id=170)
page > ta > id, 0
page > ta > CGLIB$CALLBACK_0 > target > id, 1

The problem with this is when accessing the next Text Attribute the "enhancer" loads the one mentioned above!

page > textInstances > list > elementData > 0 > ta, TextAttributes$$EnhancerByCGLIB$$d6d48c34 (id=170)

it uses the exact same text attribute as before despite the fact that it's id was different!... so all the data inside that ta, if I look down the CALLBACK is the same as the Page's ta. I have no idea how to solve this. I dont think this should be happening.

Any suggestions would be amazing... I hope I've been comprehensive enough in my explanation. I'm using the most recent version of Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 09, 2007 7:39 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I've built a test case using your mapping docs and don't see the same behaviour. In fact it all works fine.

Your bi-directional page mapping is slightly wrong because you don't specify which end is the controlling end (inverse="true") so you end up with 2 columns in TextInstance containing the page id: page_id and page. However, I ran the test with your mapping as-is and it doesn't seem to affect things.

Also, the page mapping refers to "beans.ImageInstance" which I presume is just a typo and should be beans.TextInstance ??

I'm using session.load to retrieve the Page but I see different results in eclipse. Immediately after the load, Page is cglib enhanced but
page > textAttributes, null
page > textInstances, null

Perhaps its the way/order your objects are constructed or something in your java code?

hibernate version: 3.2.5ga

Here's my test code:
Code:
package test.onetoone;

import java.util.List;

public class Page {
   private long id;
   private List<TextInstance> textInstances;
   private TextAttributes textAttributes;
   public long getId() {
      return id;
   }
   public void setId(long id) {
      this.id = id;
   }
   public List<TextInstance> getTextInstances() {
      return textInstances;
   }
   public void setTextInstances(List<TextInstance> textInstances) {
      this.textInstances = textInstances;
   }
   public TextAttributes getTextAttributes() {
      return textAttributes;
   }
   public void setTextAttributes(TextAttributes textAttributes) {
      this.textAttributes = textAttributes;
   }
   
}



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

<hibernate-mapping package="test.onetoone">
   <class name="Page" table="onetoone_page">
 
     <id name="id">
     <meta attribute="scope-set">protected</meta>
      <generator class="foreign">
        <param name="property">textAttributes</param>
      </generator>
      </id>
       
      <list name="textInstances" cascade="all-delete-orphan">
         <key column="page_id"/>
         <list-index column="index_col"/>
         <one-to-many class="TextInstance"/>
      </list>

       
      <one-to-one name="textAttributes" class="TextAttributes" constrained="true" lazy="false" cascade="all"/>

   </class>
</hibernate-mapping>



package test.onetoone;

public class TextInstance {

   private long id;
   private String body;
   private TextAttributes textAttributes;
   private Page page;
   public long getId() {
      return id;
   }
   public void setId(long id) {
      this.id = id;
   }
   public String getBody() {
      return body;
   }
   public void setBody(String body) {
      this.body = body;
   }
   public TextAttributes getTextAttributes() {
      return textAttributes;
   }
   public void setTextAttributes(TextAttributes textAttributes) {
      this.textAttributes = textAttributes;
   }
   public Page getPage() {
      return page;
   }
   public void setPage(Page page) {
      this.page = page;
   }

   
}



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

<hibernate-mapping package="test.onetoone">
   <class name="TextInstance" table="onetoone_text_instance">
 
     <id name="id">
          <meta attribute="scope-set">protected</meta>
      <generator class="foreign">
        <param name="property">textAttributes</param>
      </generator>
      </id>
       
      <property name="body" type="text" not-null="true"/>

      <one-to-one name="textAttributes" class="TextAttributes" constrained="true" lazy="false" cascade="all"/>
   
      <many-to-one name="page" class="Page" not-null="true"/>

   </class>
</hibernate-mapping>



package test.onetoone;

public class TextAttributes {
   private long id;

   public long getId() {
      return id;
   }

   public void setId(long id) {
      this.id = id;
   }
   
}



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

<hibernate-mapping package="test.onetoone">
   <class name="TextAttributes" table="onetoone_text_attributes">
 
      <id name="id">
           <meta attribute="scope-set">protected</meta>
         <generator class="native" />
      </id>

   </class>
</hibernate-mapping>



package test.onetoone;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.hibernate.Session;

public class Test extends TestCase {

   public void testIt() {
      createObjects();
      Session s = HibernateUtil.getSession();
      s.beginTransaction();
      
      Page page = (Page)s.load(Page.class, new Long(1));
      
      TextAttributes pageTa = page.getTextAttributes();
      assertEquals(1, pageTa.getId());
      
      TextInstance textInstance = (TextInstance)page.getTextInstances().get(0);
      assertEquals(2, textInstance.getId());
      
      TextAttributes textInstanceTa = textInstance.getTextAttributes();
      assertEquals(2, textInstanceTa.getId());

      s.getTransaction().commit();
      s.close();
   }

   private void createObjects() {
      Session s = HibernateUtil.getSession();
      s.beginTransaction();
      TextAttributes ta1 = new TextAttributes();
      TextAttributes ta2 = new TextAttributes();
      TextInstance ti = new TextInstance();
      Page page = new Page();
      page.setTextAttributes(ta1);
      ti.setBody("body");
      ti.setPage(page);
      ti.setTextAttributes(ta2);
      List<TextInstance> textInstances = new ArrayList<TextInstance>();
      textInstances.add(ti);
      page.setTextInstances(textInstances);
      s.save(page);
      s.getTransaction().commit();
      s.close();
   }
}




package test.onetoone;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static SessionFactory factory;

    static {
       Configuration config = new Configuration()
          .addClass(Page.class)
          .addClass(TextInstance.class)
          .addClass(TextAttributes.class)
          
          .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect")
         .setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver")
         .setProperty("hibernate.connection.url", "jdbc:mysql://localhost/test")
         .setProperty("hibernate.connection.username", "root")
         .setProperty("hibernate.connection.password", "password")
         .setProperty("hibernate.hbm2ddl.auto", "create-drop")
                  
         .setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider")
         .setProperty("hibernate.show_sql", "true");
        HibernateUtil.setSessionFactory(config.buildSessionFactory());
    }

    public static synchronized Session getSession() {
        if (factory == null) {
            factory = new Configuration().configure().buildSessionFactory();
        }
        return factory.openSession();
    }

    public static void setSessionFactory(SessionFactory factory) {
        HibernateUtil.factory = factory;
    }
}


generated sql
Code:
Hibernate: insert into onetoone_text_attributes values ( )
Hibernate: insert into onetoone_page (id) values (?)
Hibernate: insert into onetoone_text_attributes values ( )
Hibernate: insert into onetoone_text_instance (body, page, id) values (?, ?, ?)
Hibernate: update onetoone_text_instance set page_id=?, index_col=? where id=?
Hibernate: select page0_.id as id0_0_ from onetoone_page page0_ where page0_.id=?
Hibernate: select textattrib0_.id as id2_0_ from onetoone_text_attributes textattrib0_ where textattrib0_.id=?
Hibernate: select textinstan0_.page_id as page4_1_, textinstan0_.id as id1_, textinstan0_.index_col as index5_1_, textinstan0_.id as id1_0_, textinstan0_.body as body1_0_, textinstan0_.page as page1_0_ from onetoone_text_instance textinstan0_ where textinstan0_.page_id=?
Hibernate: select textattrib0_.id as id2_0_ from onetoone_text_attributes textattrib0_ where textattrib0_.id=?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 10, 2007 2:43 pm 
Newbie

Joined: Sat Jul 14, 2007 11:50 am
Posts: 12
Location: the Burgh
I'm just posting this in the case that someone runs into this problem in the future. I created a test case similar to the one outlined above and was unable to replicate the error. So the problem was most likely in my code... but I certainly couldn't find it. I changed the mappings of the objects involved into <many-to-one>'s that are unique... and that seemed to fix the problem. Maybe there's still a bug in my code or something, but it's working properly now.


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