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: