-->
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: Component mapping - possible bug?
PostPosted: Mon Feb 07, 2005 10:24 am 
Newbie

Joined: Mon Feb 07, 2005 10:05 am
Posts: 3
Hibernate version: Hibernate3 beta3


I decided to take the plunge and update to H3 but have run into a problem. I have the following component mapping:

<component name="mapIdentifier" class="com.ppd.emap.ejb.model.emap.MapIdentifier" lazy="false">
<set name="lawsonValues" lazy="false" table="LawsonNumbers" access="field">
<key column="emap_id"/>
<composite-element class="com.ppd.emap.ejb.model.emap.LawsonValue">
<property name="value" column="number" not-null="true"/>
</composite-element>
</set>
<set name="protocolValues" lazy="false" table="ProtocolNumbers" access="field">
<key column="emap_id"/>
<composite-element class="com.ppd.emap.ejb.model.emap.ProtocolValue">
<property name="value" column="number" not-null="true"/>
</composite-element>
</set>
<set name="bcValues" lazy="false" table="BcNumbers" access="field">
<key column="emap_id"/>
<composite-element class="com.ppd.emap.ejb.model.emap.BcValue">
<property name="value" column="number" not-null="true"/>
</composite-element>
</set>
</component>


This worked fine under 2.1.7 but now Hibernate always sets the MapIdentifier to NULL. I've checked the sql output and unless I'm missing something, there is no query executed that relates to the component, only it's parent entity. I also checked the data because I know Hibernate will use a NULL value if all of the fields are NULL. Is there something new in H3 that I'm missing or might this be a bug?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 12:39 pm 
Newbie

Joined: Thu Oct 07, 2004 9:41 pm
Posts: 5
I've seen the same thing.

Something else that seems funnny is that if all your columns (not collections) in the component is null, the component comes back as null. If those columns are not null, it loads everything fine, including the collections.

I am currently trying to write something that will give a reproducable case every time, and as soon as I can will post it here and add it to JIRA


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 6:36 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
I've checked the sql output and unless I'm missing something, there is no query executed that relates to the component, only it's parent entity.


This is correct.

Quote:
I also checked the data because I know Hibernate will use a NULL value if all of the fields are NULL.


Correct, as per the documentation.

Quote:
Is there something new in H3


No, components work the same as ever.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 8:26 pm 
Newbie

Joined: Thu Oct 07, 2004 9:41 pm
Posts: 5
Ok, I finally got a chance to throw a quick example together.

It seems that hibernate3 will save the information to the database just fine.

The problem is when it reads the information back from the DB.

If the component has only collections, the component will allways be null.
or
If the component contains non-null collections, but the columns in the parent table is null , the component is allways null.

This works fine in Hibernate 2.1.x. I have code that was ported to Hibernate3, but I had to port it back to 2.1.8 due to this issue.

This will replicate the issue:

Hibernate Properties file

hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://localhost/testdb
hibernate.connection.username =
hibernate.connection.password =
hibernate.dialect = org.hibernate.dialect.MySQLInnoDBDialect

SQL table creation
Code:
SET FOREIGN_KEY_CHECKS=0;

USE `testdb`;

#
# Structure for the `_compitems` table :
#

CREATE TABLE `_compitems` (
  `_parentId` int(11) NOT NULL default '0',
  `_test` varchar(255) default NULL,
  `_number` int(11) default NULL,
  `_indexName` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`_parentId`,`_indexName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

#
# Structure for the `_hibparent` table :
#

CREATE TABLE `_hibparent` (
  `_id` int(11) NOT NULL auto_increment,
  `_parentName` varchar(255) default NULL,
  `_someValue` int(11) NOT NULL default '0',
  `_compName` varchar(255) default NULL,
  PRIMARY KEY  (`_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;




Mapping

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="test.hibernate.Parent" table="_hibParent">
      <id name="id" column="_id" type="int" unsaved-value="0">
         <generator class="native"/>
      </id>
      <property name="parentName" column="_parentName" type="string" length="255" access="property" insert="true" update="true" not-null="false"/>
      <property name="someValue" column="_someValue" type="int" access="property" insert="true" update="true" not-null="true"/>
      <component name="component" class="test.hibernate.Component" lazy="false" insert="true" update="true">
         <property name="compName" column="_compName" type="string" length="255" access="property" insert="true" update="true" not-null="false"/>
         <map cascade="all" inverse="false" lazy="false" name="mapItems" sort="unsorted" table="_compItems" access="property"  outer-join="true">
            <key column="_parentId"/>
            <index column="_indexName" type="string"/>
            <composite-element class="test.hibernate.MapItem">
               <property access="property" column="_test" insert="true" name="text" not-null="false" type="string" unique="false" update="true"/>
               <property access="property" column="_number" insert="true" name="number" not-null="false" type="int" unique="false" update="true"/>
            </composite-element>
         </map>
      </component>
   </class>
</hibernate-mapping>


H3MapTest.java
Code:
package test.hibernate;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.MySQLInnoDBDialect;

public class H3MapTest
{
 
  public H3MapTest()
  {
    super();
  }

  public static void main(String[] args) throws Exception
  {
    Configuration cfg = new Configuration()
      .addClass(Parent.class);
   
   
    SessionFactory factory = cfg.buildSessionFactory();
   
    Session sess = factory.openSession();
   
    Transaction tr = sess.beginTransaction();
   
   
    Parent workingParent = createWorking();
    Parent problemParent = createComponentProblem1();
   
    sess.save(workingParent);
    sess.save(problemParent);

    tr.commit();
   
    sess.close();
   
   
    sess = factory.openSession();
    tr = sess.beginTransaction();

    Query aQuery = sess.createQuery("from Parent");
    List aList = aQuery.list();

    for (Iterator iter = aList.iterator(); iter.hasNext();)
    {
      Parent element = (Parent) iter.next();
      sess.delete(element);
     
    }
    tr.commit();
    sess.close();
  }

  public static Parent createWorking()
  {
    Parent aParent = new Parent();
    aParent.setParentName("Working");
    aParent.setSomeValue(50);

    Component aComp = new Component();
    //Because this is null, the component map will not be loaded back out
    aComp.setCompName("aComponent");
    Map aCompMap = new HashMap(2);
    aCompMap.put(aParent.getParentName() + "compIndex1", new MapItem(10, aParent.getParentName() + "compIndex1 text"));
    aCompMap.put(aParent.getParentName() + "compIndex2", new MapItem(20, aParent.getParentName() + "compIndex2 text"));
    aComp.setMapItems(aCompMap);
   
    aParent.setComponent(aComp);

    return aParent;
  }
 
  //Demonstrates the error caused when a component property included in the main class table is null
  public static Parent createComponentProblem1()
  {
    Parent aParent = new Parent();
    aParent.setParentName("ParentCompProb1");
    aParent.setSomeValue(50);

    Component aComp = new Component();
    //Because this is null, the component map will not be loaded back out
    aComp.setCompName(null);
    Map aCompMap = new HashMap(2);
    aCompMap.put(aParent.getParentName() + "compIndex1", new MapItem(10, aParent.getParentName() + "compIndex1 text"));
    aCompMap.put(aParent.getParentName() + "compIndex2", new MapItem(20, aParent.getParentName() + "compIndex2 text"));
    aComp.setMapItems(aCompMap);
   
    aParent.setComponent(aComp);

    return aParent;
  }
 
}



Parent.java

Code:

package test.hibernate;

import java.util.Map;

public class Parent
{
  private String parentName = "";
  private int someValue = 0;
  private int id = 0;
  private Component component = null;

  public Parent()
  {
    super();
  }

  public String getParentName()
  {
    return parentName;
  }

  public void setParentName(String parentName)
  {
    this.parentName = parentName;
  }

  public int getSomeValue()
  {
    return someValue;
  }

  public void setSomeValue(int someValue)
  {
    this.someValue = someValue;
  }

  public int getId()
  {
    return id;
  }
  public void setId(int id)
  {
    this.id = id;
  }

  public Component getComponent()
  {
    return component;
  }
  public void setComponent(Component component)
  {
    this.component = component;
  }

}



Component.java
Code:
package test.hibernate;

import java.util.Map;

public class Component
{
  private String compName = "";
  private Map mapItems = null;
 
  public Component()
  {
    super();
  }

  public Map getMapItems()
  {
    return mapItems;
  }
  public void setMapItems(Map mapItems)
  {
    this.mapItems = mapItems;
  }
 
  /**
   * If this is set to null, hibernate3 can not retrieve this class from the db
   */
  public String getCompName()
  {
    return compName;
  }

  public void setCompName(String name)
  {
    this.compName = name;
  }
}


MapItem.java
Code:
package test.hibernate;

public class MapItem
{
  private int number;
  private String text;

  public MapItem()
  {
    super();
  }

  public MapItem(int number, String text)
  {
    super();
    this.number = number;
    this.text = text;
  }

  public int getNumber()
  {
    return number;
  }

  public void setNumber(int number)
  {
    this.number = number;
  }

  public String getText()
  {
    return text;
  }

  public void setText(String text)
  {
    this.text = text;
  }
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 8:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
ok, that's possible, I'll go have a look at the code now...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 8:41 pm 
Newbie

Joined: Thu Oct 07, 2004 9:41 pm
Posts: 5
Oops, forgot the logs:

As you can see, it is only loading out a collection for one of the two parents retrieved, although it should be loading it for both.

Code:
16:25:23,156 DEBUG SessionImpl:980 - find: from Parent
16:25:23,156 DEBUG QueryParameters:213 - named parameters: {}
16:25:23,187 DEBUG QueryTranslatorImpl:170 - compiling query
16:25:23,250 DEBUG QueryTranslatorImpl:231 - HQL: from test.hibernate.Parent
16:25:23,250 DEBUG QueryTranslatorImpl:232 - SQL: select parent0_._id as _id, parent0_._parentName as _parentN2_0_, parent0_._someValue as _someValue0_, parent0_._compName as _compName0_ from _hibParent parent0_
16:25:23,250 DEBUG AbstractBatcher:259 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
16:25:23,250 DEBUG SQL:297 - select parent0_._id as _id, parent0_._parentName as _parentN2_0_, parent0_._someValue as _someValue0_, parent0_._compName as _compName0_ from _hibParent parent0_
16:25:23,250 DEBUG AbstractBatcher:348 - preparing statement
16:25:23,265 DEBUG AbstractBatcher:275 - about to open ResultSet (open ResultSets: 0, globally: 0)
16:25:23,265 DEBUG Loader:1135 - Using naked result set
16:25:23,265 DEBUG Loader:363 - processing result set
16:25:23,281 DEBUG Loader:660 - result row: EntityKey[test.hibernate.Parent#1]
16:25:23,281 DEBUG Loader:800 - Initializing object from ResultSet: EntityKey[test.hibernate.Parent#1]
16:25:23,281 DEBUG BasicEntityPersister:1514 - Hydrating entity: [test.hibernate.Parent#1]
16:25:23,296 DEBUG Loader:660 - result row: EntityKey[test.hibernate.Parent#2]
16:25:23,296 DEBUG Loader:800 - Initializing object from ResultSet: EntityKey[test.hibernate.Parent#2]
16:25:23,296 DEBUG BasicEntityPersister:1514 - Hydrating entity: [test.hibernate.Parent#2]
16:25:23,296 DEBUG Loader:383 - done processing result set (2 rows)
16:25:23,296 DEBUG AbstractBatcher:282 - about to close ResultSet (open ResultSets: 1, globally: 1)
16:25:23,312 DEBUG AbstractBatcher:267 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
16:25:23,312 DEBUG AbstractBatcher:368 - closing statement
16:25:23,312 DEBUG Loader:426 - total objects hydrated: 2
16:25:23,312 DEBUG TwoPhaseLoad:94 - resolving associations for [test.hibernate.Parent#1]
16:25:23,328 DEBUG TwoPhaseLoad:165 - done materializing entity [test.hibernate.Parent#1]
16:25:23,328 DEBUG TwoPhaseLoad:94 - resolving associations for [test.hibernate.Parent#2]
16:25:23,328 DEBUG TwoPhaseLoad:165 - done materializing entity [test.hibernate.Parent#2]
16:25:23,343 DEBUG PersistenceContext:679 - initializing non-lazy collections
16:25:23,343 DEBUG DefaultInitializeCollectionEventListener:39 - initializing collection [test.hibernate.Parent.test.hibernate.Parent.component.mapItems#1]
16:25:23,343 DEBUG DefaultInitializeCollectionEventListener:43 - checking second-level cache
16:25:23,343 DEBUG DefaultInitializeCollectionEventListener:55 - collection not cached
16:25:23,359 DEBUG AbstractBatcher:259 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
16:25:23,359 DEBUG SQL:297 - select mapitems0_._parentId as _parentId__, mapitems0_._test as _test__, mapitems0_._number as _number__, mapitems0_._indexName as _indexName__ from _compItems mapitems0_ where mapitems0_._parentId=?
16:25:23,359 DEBUG AbstractBatcher:348 - preparing statement
16:25:23,375 DEBUG AbstractBatcher:275 - about to open ResultSet (open ResultSets: 0, globally: 0)
16:25:23,390 DEBUG Loader:1135 - Using naked result set
16:25:23,390 DEBUG Loader:557 - result set contains (possibly empty) collection: [test.hibernate.Parent.test.hibernate.Parent.component.mapItems#1]
16:25:23,390 DEBUG PersistenceContext:638 - uninitialized collection: initializing
16:25:23,406 DEBUG Loader:363 - processing result set
16:25:23,406 DEBUG Loader:660 - result row:
16:25:23,421 DEBUG Loader:498 - found row of collection: [test.hibernate.Parent.test.hibernate.Parent.component.mapItems#1]
16:25:23,421 DEBUG PersistenceContext:661 - reading row
16:25:23,421 DEBUG Loader:660 - result row:
16:25:23,437 DEBUG Loader:498 - found row of collection: [test.hibernate.Parent.test.hibernate.Parent.component.mapItems#1]
16:25:23,437 DEBUG PersistenceContext:661 - reading row
16:25:23,437 DEBUG Loader:383 - done processing result set (2 rows)
16:25:23,437 DEBUG AbstractBatcher:282 - about to close ResultSet (open ResultSets: 1, globally: 1)
16:25:23,453 DEBUG AbstractBatcher:267 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
16:25:23,453 DEBUG AbstractBatcher:368 - closing statement
16:25:23,453 DEBUG PersistenceContext:721 - 1 collections were found in result set
16:25:23,468 DEBUG PersistenceContext:751 - collection fully initialized: [test.hibernate.Parent.test.hibernate.Parent.component.mapItems#1]
16:25:23,468 DEBUG PersistenceContext:729 - 1 collections initialized
16:25:23,468 DEBUG DefaultInitializeCollectionEventListener:57 - collection initialized



Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 8:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is now fixed in CVS.


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.