-->
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.  [ 4 posts ] 
Author Message
 Post subject: OneToOne mapping causes NullPointerException on load
PostPosted: Thu Jul 07, 2005 2:41 pm 
Newbie

Joined: Fri Mar 25, 2005 1:16 pm
Posts: 13
Hibernate version:
v31alpha1 tag, Annotations v31beta3

Mapping documents:
This is what I have now, minus extra junk and setters. TankLineItem can have 0 or 1 FuelLineItem.
Code:
@Entity
public class OrderUpdate extends BaseAuditedExternalIdItem {

    private Set<TankLineItem> _tankLineItems = new HashSet<TankLineItem>();

    @OneToMany(cascade={CascadeType.ALL}, mappedBy="orderUpdate")
    public Set<TankLineItem> getTankLineItems() {
        return _tankLineItems;
    }

    //...
}

@Entity
public class TankLineItem extends AbstractTankLineItem {

    private FuelLineItem _fuelLineItem;

    private OrderUpdate _orderUpdate;

    @ManyToOne (optional=false)
    @JoinColumn(name="orderUpdate_id")
    public OrderUpdate getOrderUpdate() {
        return _orderUpdate;
    }

    @OneToOne (cascade={CascadeType.ALL})
    public FuelLineItem getFuelLineItem() {
        return _fuelLineItem;
    }

    //...
}

@Entity
public class FuelLineItem extends AbstractFuelLineItem {
    //...
}


Code between sessionFactory.openSession() and session.close():
Code:
OrderUpdate ou1 = new OrderUpdate();

TankLineItem tli1 = new TankLineItem();
ou1.addTankLineItem(tli1);

TankLineItem tli2 = new TankLineItem();
ou1.addTankLineItem(tli2);

FuelLineItem fli1 = new FuelLineItem();
tli2.setFuelLineItem(fli1);

dao.saveOrUpdate(ou1);

//new session
Order order = (Order) _maxDao.findByExternalId(Order.class, "123");
assertNotNull("order", order);
assertEquals("update count", 1, order.getOrderUpdates().size());
OrderUpdate ou1 = order.getOrderUpdates().get(0);
Set<TankLineItem> tlis = ou1.getTankLineItems();
assertEquals("tank line item size", 2, tlis.size()); //!!exception here on load with second mapping//


This all works fine. However, I wanted to try to map the relationship so that the FuelLineItem table had a foreign key to TankLineItem. This would give me flexibility in the future to have TankLineItem have many FuelLineItems instead of one without changing the database. However, when I map it like below, I get an exception. I tried OneToOne and ManyToOne in FuelLineItem and both have the same effect. It seems like the mapping is correct because the sql generated is what I expect, and the save works correctly. The error occurs when loading the TankLineItem collection on the line indicated. Does the mapping look okay? I am using pre-release versions so I know that could be an issue (but something else I needed was fixed in the newer versions - attribute override, I think).

Code:
@Entity
public class TankLineItem extends AbstractTankLineItem {

    private FuelLineItem _fuelLineItem;

    private OrderUpdate _orderUpdate;

    @ManyToOne (optional=false)
    @JoinColumn(name="orderUpdate_id")
    public OrderUpdate getOrderUpdate() {
        return _orderUpdate;
    }

    @OneToOne (cascade={CascadeType.ALL}, mappedBy="tankLineItem")
    public FuelLineItem getFuelLineItem() {
        return _fuelLineItem;
    }

    //...
}

@Entity
public class FuelLineItem extends AbstractFuelLineItem {
    private TankLineItem _tankLineItem;

    //@ManyToOne(optional=false)
    @OneToOne(optional=false)
    @JoinColumn(name="tankLineItem_id")
    public TankLineItem getTankLineItem() {
        return _tankLineItem;
    }

    //...
}


Full stack trace of any exception that occurs:
Code:

java.lang.NullPointerException
   at org.hibernate.engine.AssociationKey.hashCode(AssociationKey.java:39)
   at java.util.HashMap.hash(HashMap.java:264)
   at java.util.HashMap.put(HashMap.java:382)
   at java.util.HashSet.add(HashSet.java:194)
   at org.hibernate.engine.StatefulPersistenceContext.addNullProperty(StatefulPersistenceContext.java:1048)
   at org.hibernate.loader.Loader.registerNonExists(Loader.java:878)
   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:548)
   at org.hibernate.loader.Loader.doQuery(Loader.java:665)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:221)
   at org.hibernate.loader.Loader.loadCollection(Loader.java:1699)
   at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:71)
   at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:490)
   at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
   at com.connectria.kenan.dao.hibernate.AllEventListener.onInitializeCollection(AllEventListener.java:203)
   at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1438)
   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:219)
   at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:81)
   at org.hibernate.collection.PersistentBag.size(PersistentBag.java:222)
   at com.connectria.kenan.model.NewModelDbTest$10.run(NewModelDbTest.java:379)
   at com.connectria.kenan.model.NewModelDbTest.runInSession(NewModelDbTest.java:92)
   at com.connectria.kenan.model.NewModelDbTest.testOrder(NewModelDbTest.java:371)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at junit.framework.TestCase.runTest(TestCase.java:154)

Name and version of the database you are using: MSSQL 2000

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 10, 2005 12:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This might be a bug.
If you provides me a simple runnable testcase reproducing the problem in JIRA, I will be able to look at it.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Verified
PostPosted: Tue Jul 12, 2005 1:21 pm 
Newbie

Joined: Tue Jul 12, 2005 1:17 pm
Posts: 7
I've seen this as well. We made a temporary patch on AssociationKey in order to get past it.

Code:
package org.hibernate.engine;

import java.io.Serializable;

final class AssociationKey implements Serializable {
    private EntityKey ownerKey;
    private String propertyName;

    AssociationKey(EntityKey ownerKey, String propertyName) {
        this.ownerKey = ownerKey;
        this.propertyName = propertyName;
    }
    public boolean equals(Object that) {
        AssociationKey key = (AssociationKey) that;
        if (key == null || propertyName == null || ownerKey == null || key.propertyName == null || key.ownerKey == null) {
            System.out.println("This happens when you have a One-to-One with no matching One.");
            return false;
        }

        return (propertyName == key.propertyName || (propertyName != null && propertyName.equals(key.propertyName))) &&
                (ownerKey == key.ownerKey || (ownerKey != null && ownerKey.equals(key.ownerKey)));
    }
    public int hashCode() {
        return (ownerKey != null ? ownerKey.hashCode() : 0) + (propertyName != null ? propertyName.hashCode() : 0);
    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 13, 2005 1:55 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This should be solve in CVS, can you spend some time and test this on both Hibernate3 and Hibernate annotations CVS head. It would be great.

_________________
Emmanuel


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