Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Entity association list padded with null in first slot.
PostPosted: Sat Jun 16, 2012 6:02 pm 
Contributor
Contributor

Joined: Fri Apr 03, 2009 11:07 am
Posts: 11
Hi,
I am seeing in my application an association list is padded with a null. It is the first item in the list. The application is deployed as an EJB as part of a enterprise application.

My association is a bi-directional One To Many association with a compound primary key. There is a primary key class too. CustomerInventoryPK

Customer---<-CustomerInventory

I notice the index variable in PersistentList.readFrom(ResultSet, CollectionPersister, CollectionAliases, Object) is set with a value of 1. Rather than 0 which means the following for loop in the method pads the list with a null item.
This causes my application to throw a NullPointerException.

Details of the entities in my application are below. The getters and setters have been excluded.

Code:
@Entity
@Table (name="TEST_CUSTOMER")
public class Customer implements Serializable   
{
   @Id
   @GeneratedValue
   @Column(name="C_ID")
   private int id;
   
   @OneToMany(mappedBy="customer", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
   private List<CustomerInventory> customerInventories;
   
   public List<CustomerInventory> getInventories()
   {
      return customerInventories;
   }


Code:
@Entity
@Table(name="TEST_CUSTINVENTORY")
@IdClass(CustomerInventoryPK.class)
public class CustomerInventory implements Serializable
{
   @Id
   @GeneratedValue
   @Column(name="CI_ID")
   private Long id;
   
   @Id
   @Column(name = "CI_CUSTOMERID", insertable = false, updatable = false)
   private int custId;
   
   @ManyToOne(cascade=CascadeType.MERGE)
   @JoinColumn(name="CI_CUSTOMERID")
   private Customer        customer;


Code:
public class CustomerInventoryPK implements Serializable {
    private Long id;
    private int custId;


Code:
<hibernate-mapping>
   
    <class name="org.jboss.orm.test.entity.Customer" mutable="true" table="TEST_CUSTOMER">
        <cache usage="transactional" include="all"/>
        <id name="id" type="java.lang.Integer" column="C_ID" access="field">
            <generator class="assigned"/>
        </id>
        <list name="customerInventories" access="field" table="TEST_CUSTINVENTORY" lazy="false" cascade="all">
            <cache usage="transactional" include="all"/>
            <key>
                <column name="CI_CUSTOMERID" not-null="true"/>
            </key>
            <list-index column="CI_ID"/>
            <one-to-many class="org.jboss.orm.test.entity.CustomerInventory"/>
        </list>
    </class>
   
</hibernate-mapping>

Code:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
   version="2.0">
   <persistence-unit name="associationTest">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
      <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
      <class>org.jboss.orm.test.entity.Customer</class>
      <class>org.jboss.orm.test.entity.CustomerInventory</class>
      <class>org.jboss.orm.test.entity.CustomerInventoryPK</class>
      <mapping-file>META-INF/hbm_order.xml</mapping-file>
      <properties>
         <property name="hibernate.hbm2ddl.auto" value="create-drop" />
         <property name="hibernate.show_sql" value="false" />
         <property name="hibernate.id.optimizer.pooled.prefer_lo" value="true" />
         <property name="hibernate.id.new_generator_mappings" value="true" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
          <property name="hibernate.order_inserts" value="true"/>
          <property name="hibernate.default_batch_fetch_size" value="200"/>
          <property name="hibernate.jdbc.fetch_size" value="200"/>
          <property name="hibernate.jdbc.batch_size" value="16"/>
                         <property name="hibernate.cache.use_second_level_cache" value="true"/>
                         <property name="hibernate.archive.autodetection" value="true" />
      </properties>
   </persistence-unit>
</persistence>


This is the contents of the list. Should this be happening ?

[null, org.jboss.orm.test.entity.CustomerInventory@60bfb9a5, org.jboss.orm.test.entity.CustomerInventory@3f31a783, org.jboss.orm.test.entity.CustomerInventory@13b3a21f, org.jboss.orm.test.entity.CustomerInventory@388a91e1, org.jboss.orm.test.entity.CustomerInventory@12d67edd]

I am using 4.1.3.Final version of Hibernate. This error occurs both with PostgreSQL and MySQL.

Regards,
Jeremy


Top
 Profile  
 
 Post subject: Re: Entity association list padded with null in first slot.
PostPosted: Sat Jun 16, 2012 6:37 pm 
Contributor
Contributor

Joined: Fri Apr 03, 2009 11:07 am
Posts: 11
I found these two JIRAs that are related to this posting.

https://hibernate.onjira.com/browse/HHH-1782
https://hibernate.onjira.com/browse/HHH-3643

Jeremy


Top
 Profile  
 
 Post subject: Re: Entity association list padded with null in first slot.
PostPosted: Mon Jun 18, 2012 1:58 pm 
Contributor
Contributor

Joined: Fri Apr 03, 2009 11:07 am
Posts: 11
Hi,
It turns out to be a mistake configuring the entity in the hbm file.

Rather than define the customerInventories as a list type it should be a bag.

Code:
<hibernate-mapping>
   
    <class name="org.jboss.orm.test.entity.Customer" mutable="true" table="TEST_CUSTOMER">
        <cache usage="transactional" include="all"/>
        <id name="id" type="java.lang.Integer" column="C_ID" access="field">
            <generator class="assigned"/>
        </id>
       <bag name="customerInventories" access="field" table="TEST_CUSTINVENTORY" lazy="false" cascade="all">
            <cache usage="transactional" include="all"/>
            <key>
                <column name="CI_CUSTOMERID" not-null="true"/>
            </key>
            <one-to-many class="org.jboss.orm.test.entity.CustomerInventory"/>
        </bag>
    </class>
   
</hibernate-mapping>




Regards,
Jeremy


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.