I'm having some trouble using an @Loader annotation to load a collection from a Native SQL Query. The parent entity is also loaded by a Native SQL Query. The only error I receive is a null pointer exception. For some reason the underlying collection (PersistentBag for an ArrayList and PersistentSet for a HashSet; I've tried both) is never instantiated. I've run through the debugger, but I still can't seem to find out what configuration I am missing. Any help given will be appreciated.
Hibernate version: 3.2.1
Mapping annotations:
@Entity
@SqlResultSetMapping
(
name = "OrdersAndAuditee",
entities =
{
@EntityResult
(
entityClass = model.OrdersAndAuditee.class,
fields =
{
@FieldResult(name = "orderId", column = "OrderID"),
@FieldResult(name = "shipDate", column = "ShipDate"),
@FieldResult(name = "shipState", column = "ShipState"),
@FieldResult(name = "shipZip", column = "ShipZip"),
@FieldResult(name = "orderTotal", column = "OrderTotal"),
@FieldResult(name = "processedFlag", column = "ProcessedFlag"),
@FieldResult(name = "shippedFlag", column = "ShippedFlag"),
@FieldResult(name = "notes", column = "Notes"),
@FieldResult(name = "auditDate", column = "CreationDate"),
@FieldResult(name = "auditUser", column = "Creator"),
@FieldResult(name = "auditAction", column = "AuditAction")
}
)
}
)
@NamedNativeQueries
({
@NamedNativeQuery
( name = "orders.creator",
query = "select ord.OrderID, ord.ShipState, " +
"(select AuditUser from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') Creator, " +
"(select AuditDate from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') CreationDate, " +
"ord.ShipZip, ord.AccountNo, ord.OrderTotal, ord.ShipDate, ord.ProcessedFlag, ord.ShippedFlag, ord.Notes, " +
"'C' AS AuditAction " +
"from Orders ord " +
"where ord.OrderID = ? ",
resultSetMapping = "OrdersAndAuditee"
),
@NamedNativeQuery
( name = "orders.creator.all",
query = "select ord.OrderID, ord.ShipState, " +
"(select AuditUser from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') Creator, " +
"(select AuditDate from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') CreationDate, " +
"ord.ShipZip, ord.AccountNo, ord.OrderTotal, ord.ShipDate, ord.ProcessedFlag, ord.ShippedFlag, ord.Notes, " +
"'C' AS AuditAction " +
"from Orders ord ",
resultSetMapping = "OrdersAndAuditee"
)
})
@Loader(namedQuery = "orders.creator")
public class OrdersAndAuditee implements Serializable
{
private List<ItemAndAuditee> itemAndAuditees = new ArrayList<ItemAndAuditee>();
...
@OneToMany(mappedBy="ordersAndAuditee", fetch=FetchType.LAZY)
@Loader(namedQuery = "item.creator.orderid")
public List<ItemAndAuditee> getItemAndAuditees() {return itemAndAuditees;}
public void setItemAndAuditees(List<ItemAndAuditee> itemAndAuditees) {this.itemAndAuditees = itemAndAuditees;}
}
@Entity
@SqlResultSetMapping
(
name = "ItemAndAuditee",
entities =
{
@EntityResult
(
entityClass = model.ItemAndAuditee.class,
fields =
{
@FieldResult(name = "itemId", column = "ItemID"),
@FieldResult(name = "itemAmount", column = "ItemAmount"),
@FieldResult(name = "shipRate", column = "ShipRate"),
@FieldResult(name = "ordersAndAuditee", column = "OrderID"),
@FieldResult(name = "notes", column = "Notes"),
@FieldResult(name = "auditDate", column = "CreationDate"),
@FieldResult(name = "auditUser", column = "Creator"),
@FieldResult(name = "auditAction", column = "AuditAction")
}
)
}
)
@NamedNativeQueries
({
@NamedNativeQuery
( name = "item.creator.orderid",
query = "select i.ItemID, i.ItemAmount, " +
"(select AuditUser from Item_History where i.ItemID = ItemID and AuditAction = 'C') Creator, " +
"(select AuditDate from Item_History where i.ItemID = ItemID and AuditAction = 'C') CreationDate, " +
"i.ShipRate, i.OrderID, i.ItemTypeID, i.Notes, " +
"'C' AS AuditAction " +
"from Item i " +
"where i.OrderID = ? ",
resultSetMapping = "ItemAndAuditee"
),
@NamedNativeQuery
( name = "item.creator",
query = "select i.ItemID, i.ItemAmount, " +
"(select AuditUser from Item_History where i.ItemID = ItemID and AuditAction = 'C') Creator, " +
"(select AuditDate from Item_History where i.ItemID = ItemID and AuditAction = 'C') CreationDate, " +
"i.ShipRate, i.OrderID, i.ItemTypeID, i.Notes, " +
"'C' AS AuditAction " +
"from Item i " +
"where i.ItemID = ? ",
resultSetMapping = "ItemAndAuditee"
)
})
@Loader(namedQuery = "item.creator")
public class ItemAndAuditee implements Serializable
{
private OrdersAndAuditee ordersAndAuditee;
...
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="OrderID")
public OrdersAndAuditee getOrdersAndAuditee() {return ordersAndAuditee;}
public void setOrdersAndAuditee(OrdersAndAuditee ordersAndAuditee) {this.ordersAndAuditee = ordersAndAuditee;}
}
Code between sessionFactory.openSession() and session.close():
OrdersAndAuditeeDAO oaaDAO = daoFactory.getOrdersAndAuditeeDAO();
List<OrdersAndAuditee> ordCreators = oaaDAO.findAll();
logger.debug("Number Orders and Creators: " + ordCreators.size());
for(OrdersAndAuditee oc:ordCreators)
{
logger.debug(oc.getItemAndAuditees().size());
}
Note: The findAll method listed above has been overridden as follows:
public List<OrdersAndAuditee> findAll()
{
return getSession().getNamedQuery("orders.creator.all").list();
}
Full stack trace of any exception that occurs:
java.lang.NullPointerException
at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
at test.Test.main(Test.java:151)
Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException
at test.Test.main(Test.java:184)
Caused by: java.lang.NullPointerException
at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
at test.Test.main(Test.java:151)
Name and version of the database you are using: MySQL 5 and SQlServer 2000 (error occurs on both)
The generated SQL (show_sql=true): SQL query: select i.ItemID, i.ItemAmount, (select AuditUser from Item_History where i.ItemID = ItemID and AuditAction = 'C') Creator, (select AuditDate from Item_History where i.ItemID = ItemID and AuditAction = 'C') CreationDate, i.ShipRate, i.OrderID, i.ItemTypeID, i.Notes, 'C' AS AuditAction from Item i where i.OrderID = ?
Debug level Hibernate log excerpt:
DEBUG [main] (DAOFactory.java:15) - Creating concrete DAO factory: class hibernate.HibernateDAOFactory
DEBUG [main] (ThreadLocalSessionContext.java:290) - allowing method [beginTransaction] in non-transacted context
DEBUG [main] (ThreadLocalSessionContext.java:300) - allowing proxied method [beginTransaction] to proceed to real session
DEBUG [main] (JDBCTransaction.java:54) - begin
DEBUG [main] (ConnectionManager.java:415) - opening JDBC connection
DEBUG [main] (JDBCTransaction.java:59) - current autocommit status: true
DEBUG [main] (JDBCTransaction.java:62) - disabling autocommit
DEBUG [main] (JDBCContext.java:210) - after transaction begin
DEBUG [main] (Test.java:52) - !!! Logger
DEBUG [main] (HibernateDAOFactory.java:27) - Instantiating DAO: class hibernate.HibernateDAOFactory$AccountDAOHibernate
DEBUG [main] (HibernateDAOFactory.java:27) - Instantiating DAO: class hibernate.HibernateDAOFactory$CategoryDAOHibernate
DEBUG [main] (HibernateDAOFactory.java:27) - Instantiating DAO: class hibernate.HibernateDAOFactory$OrdersHistoryDAOHibernate
DEBUG [main] (HibernateDAOFactory.java:27) - Instantiating DAO: class hibernate.HibernateDAOFactory$ItemHistoryDAOHibernate
DEBUG [main] (HibernateDAOFactory.java:27) - Instantiating DAO: class hibernate.HibernateDAOFactory$OrdersDAOHibernate
DEBUG [main] (HibernateDAOFactory.java:27) - Instantiating DAO: class hibernate.HibernateDAOFactory$ItemDAOHibernate
DEBUG [main] (HibernateDAOFactory.java:27) - Instantiating DAO: class hibernate.OrdersAndAuditeeDAOHibernate
DEBUG [main] (SessionFactoryObjectFactory.java:69) - JNDI lookup: PolicyAdjSF
DEBUG [main] (SessionFactoryObjectFactory.java:145) - lookup: uid=2c9048e5105ff38a01105ff38e2c0000
DEBUG [main] (ThreadLocalSessionContext.java:300) - allowing proxied method [getNamedQuery] to proceed to real session
DEBUG [main] (SessionImpl.java:1289) - setting flush mode to: AUTO
DEBUG [main] (SessionImpl.java:1308) - setting cache mode to: NORMAL
DEBUG [main] (QueryPlanCache.java:118) - located native-sql query plan in cache (select ord.OrderID, ord.ShipState, (select AuditUser from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') Creator, (select AuditDate from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') CreationDate, ord.ShipZip, ord.AccountNo, ord.OrderTotal, ord.ShipDate, ord.ProcessedFlag, ord.ShippedFlag, ord.Notes, 'C' AS AuditAction from Orders ord)
DEBUG [main] (SessionImpl.java:1685) - SQL query: select ord.OrderID, ord.ShipState, (select AuditUser from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') Creator, (select AuditDate from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') CreationDate, ord.ShipZip, ord.AccountNo, ord.OrderTotal, ord.ShipDate, ord.ProcessedFlag, ord.ShippedFlag, ord.Notes, 'C' AS AuditAction from Orders ord
DEBUG [main] (AbstractBatcher.java:358) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [main] (AbstractBatcher.java:393) - select ord.OrderID, ord.ShipState, (select AuditUser from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') Creator, (select AuditDate from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') CreationDate, ord.ShipZip, ord.AccountNo, ord.OrderTotal, ord.ShipDate, ord.ProcessedFlag, ord.ShippedFlag, ord.Notes, 'C' AS AuditAction from Orders ord
Hibernate: select ord.OrderID, ord.ShipState, (select AuditUser from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') Creator, (select AuditDate from Orders_History where ord.OrderID = OrderID and AuditAction = 'C') CreationDate, ord.ShipZip, ord.AccountNo, ord.OrderTotal, ord.ShipDate, ord.ProcessedFlag, ord.ShippedFlag, ord.Notes, 'C' AS AuditAction from Orders ord
DEBUG [main] (AbstractBatcher.java:476) - preparing statement
DEBUG [main] (AbstractBatcher.java:374) - about to open ResultSet (open ResultSets: 0, globally: 0)
DEBUG [main] (Loader.java:682) - processing result set
DEBUG [main] (Loader.java:687) - result set row: 0
DEBUG [main] (NullableType.java:122) - returning '1' as column: OrderID
DEBUG [main] (Loader.java:1164) - result row: EntityKey[model.OrdersAndAuditee#1]
DEBUG [main] (Loader.java:1346) - Initializing object from ResultSet: [model.OrdersAndAuditee#1]
DEBUG [main] (AbstractEntityPersister.java:1981) - Hydrating entity: [model.OrdersAndAuditee#1]
DEBUG [main] (NullableType.java:122) - returning 'MI' as column: ShipState
DEBUG [main] (NullableType.java:122) - returning '48910' as column: ShipZip
DEBUG [main] (NullableType.java:122) - returning '1488.95' as column: OrderTotal
DEBUG [main] (NullableType.java:122) - returning '15 January 2007' as column: ShipDate
DEBUG [main] (NullableType.java:122) - returning 'true' as column: ProcessedFlag
DEBUG [main] (NullableType.java:122) - returning 'false' as column: ShippedFlag
DEBUG [main] (NullableType.java:116) - returning null as column: Notes
DEBUG [main] (NullableType.java:122) - returning 'User1' as column: Creator
DEBUG [main] (NullableType.java:122) - returning 'C' as column: AuditAction
DEBUG [main] (NullableType.java:122) - returning '2007-01-15 00:00:00' as column: CreationDate
DEBUG [main] (Loader.java:687) - result set row: 1
DEBUG [main] (NullableType.java:122) - returning '2' as column: OrderID
DEBUG [main] (Loader.java:1164) - result row: EntityKey[model.OrdersAndAuditee#2]
DEBUG [main] (Loader.java:1346) - Initializing object from ResultSet: [model.OrdersAndAuditee#2]
DEBUG [main] (AbstractEntityPersister.java:1981) - Hydrating entity: [model.OrdersAndAuditee#2]
DEBUG [main] (NullableType.java:122) - returning 'CA' as column: ShipState
DEBUG [main] (NullableType.java:122) - returning '90210' as column: ShipZip
DEBUG [main] (NullableType.java:122) - returning '37890.1' as column: OrderTotal
DEBUG [main] (NullableType.java:122) - returning '15 January 2007' as column: ShipDate
DEBUG [main] (NullableType.java:122) - returning 'true' as column: ProcessedFlag
DEBUG [main] (NullableType.java:122) - returning 'true' as column: ShippedFlag
DEBUG [main] (NullableType.java:116) - returning null as column: Notes
DEBUG [main] (NullableType.java:122) - returning 'User2' as column: Creator
DEBUG [main] (NullableType.java:122) - returning 'C' as column: AuditAction
DEBUG [main] (NullableType.java:122) - returning '2007-01-14 18:44:48' as column: CreationDate
DEBUG [main] (Loader.java:709) - done processing result set (2 rows)
DEBUG [main] (AbstractBatcher.java:381) - about to close ResultSet (open ResultSets: 1, globally: 1)
DEBUG [main] (AbstractBatcher.java:366) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [main] (AbstractBatcher.java:525) - closing statement
DEBUG [main] (Loader.java:839) - total objects hydrated: 2
DEBUG [main] (TwoPhaseLoad.java:107) - resolving associations for [model.OrdersAndAuditee#1]
DEBUG [main] (CollectionLoadContext.java:141) - creating collection wrapper:[model.OrdersAndAuditee.itemAndAuditees#1]
DEBUG [main] (TwoPhaseLoad.java:206) - done materializing entity [model.OrdersAndAuditee#1]
DEBUG [main] (TwoPhaseLoad.java:107) - resolving associations for [model.OrdersAndAuditee#2]
DEBUG [main] (CollectionLoadContext.java:141) - creating collection wrapper:[model.OrdersAndAuditee.itemAndAuditees#2]
DEBUG [main] (TwoPhaseLoad.java:206) - done materializing entity [model.OrdersAndAuditee#2]
DEBUG [main] (StatefulPersistenceContext.java:748) - initializing non-lazy collections
DEBUG [main] (SessionImpl.java:1289) - setting flush mode to: AUTO
DEBUG [main] (SessionImpl.java:1308) - setting cache mode to: NORMAL
DEBUG [main] (Test.java:148) - Number Orders and Creators: 2
DEBUG [main] (DefaultInitializeCollectionEventListener.java:41) - initializing collection [model.OrdersAndAuditee.itemAndAuditees#1]
DEBUG [main] (DefaultInitializeCollectionEventListener.java:47) - checking second-level cache
DEBUG [main] (DefaultInitializeCollectionEventListener.java:59) - collection not cached
DEBUG [main] (NamedQueryCollectionInitializer.java:34) - initializing collection: model.OrdersAndAuditee.itemAndAuditees using named query: item.creator.orderid
DEBUG [main] (SessionImpl.java:1289) - setting flush mode to: MANUAL
DEBUG [main] (SessionImpl.java:1308) - setting cache mode to: NORMAL
DEBUG [main] (QueryPlanCache.java:118) - located native-sql query plan in cache (select i.ItemID, i.ItemAmount, (select AuditUser from Item_History where i.ItemID = ItemID and AuditAction = 'C') Creator, (select AuditDate from Item_History where i.ItemID = ItemID and AuditAction = 'C') CreationDate, i.ShipRate, i.OrderID, i.ItemTypeID, i.Notes, 'C' AS AuditAction from Item i where i.OrderID = ?)
DEBUG [main] (SessionImpl.java:1685) - SQL query: select i.ItemID, i.ItemAmount, (select AuditUser from Item_History where i.ItemID = ItemID and AuditAction = 'C') Creator, (select AuditDate from Item_History where i.ItemID = ItemID and AuditAction = 'C') CreationDate, i.ShipRate, i.OrderID, i.ItemTypeID, i.Notes, 'C' AS AuditAction from Item i where i.OrderID = ?
DEBUG [main] (AbstractBatcher.java:358) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [main] (AbstractBatcher.java:393) - select i.ItemID, i.ItemAmount, (select AuditUser from Item_History where i.ItemID = ItemID and AuditAction = 'C') Creator, (select AuditDate from Item_History where i.ItemID = ItemID and AuditAction = 'C') CreationDate, i.ShipRate, i.OrderID, i.ItemTypeID, i.Notes, 'C' AS AuditAction from Item i where i.OrderID = ?
Hibernate: select i.ItemID, i.ItemAmount, (select AuditUser from Item_History where i.ItemID = ItemID and AuditAction = 'C') Creator, (select AuditDate from Item_History where i.ItemID = ItemID and AuditAction = 'C') CreationDate, i.ShipRate, i.OrderID, i.ItemTypeID, i.Notes, 'C' AS AuditAction from Item i where i.OrderID = ?
DEBUG [main] (AbstractBatcher.java:476) - preparing statement
DEBUG [main] (NullableType.java:80) - binding '1' to parameter: 1
DEBUG [main] (AbstractBatcher.java:374) - about to open ResultSet (open ResultSets: 0, globally: 0)
DEBUG [main] (Loader.java:682) - processing result set
DEBUG [main] (Loader.java:687) - result set row: 0
DEBUG [main] (NullableType.java:122) - returning '1' as column: ItemID
DEBUG [main] (Loader.java:1164) - result row: EntityKey[model.ItemAndAuditee#1]
DEBUG [main] (Loader.java:1346) - Initializing object from ResultSet: [model.ItemAndAuditee#1]
DEBUG [main] (AbstractEntityPersister.java:1981) - Hydrating entity: [model.ItemAndAuditee#1]
DEBUG [main] (NullableType.java:116) - returning null as column: Notes
DEBUG [main] (NullableType.java:122) - returning '100.0' as column: ItemAmount
DEBUG [main] (NullableType.java:122) - returning '12.12' as column: ShipRate
DEBUG [main] (NullableType.java:122) - returning 'User1' as column: Creator
DEBUG [main] (NullableType.java:122) - returning 'C' as column: AuditAction
DEBUG [main] (NullableType.java:122) - returning '2007-01-15 12:00:01' as column: CreationDate
DEBUG [main] (NullableType.java:122) - returning '1' as column: OrderID
DEBUG [main] (Loader.java:687) - result set row: 1
DEBUG [main] (NullableType.java:122) - returning '2' as column: ItemID
DEBUG [main] (Loader.java:1164) - result row: EntityKey[model.ItemAndAuditee#2]
DEBUG [main] (Loader.java:1346) - Initializing object from ResultSet: [model.ItemAndAuditee#2]
DEBUG [main] (AbstractEntityPersister.java:1981) - Hydrating entity: [model.ItemAndAuditee#2]
DEBUG [main] (NullableType.java:116) - returning null as column: Notes
DEBUG [main] (NullableType.java:122) - returning '487.0' as column: ItemAmount
DEBUG [main] (NullableType.java:122) - returning '0.0' as column: ShipRate
DEBUG [main] (NullableType.java:122) - returning 'User1' as column: Creator
DEBUG [main] (NullableType.java:122) - returning 'C' as column: AuditAction
DEBUG [main] (NullableType.java:122) - returning '2007-01-15 12:00:02' as column: CreationDate
DEBUG [main] (NullableType.java:122) - returning '1' as column: OrderID
DEBUG [main] (Loader.java:709) - done processing result set (2 rows)
DEBUG [main] (AbstractBatcher.java:381) - about to close ResultSet (open ResultSets: 1, globally: 1)
DEBUG [main] (AbstractBatcher.java:366) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [main] (AbstractBatcher.java:525) - closing statement
DEBUG [main] (Loader.java:839) - total objects hydrated: 2
DEBUG [main] (TwoPhaseLoad.java:107) - resolving associations for [model.ItemAndAuditee#1]
DEBUG [main] (DefaultLoadEventListener.java:171) - loading entity: [model.OrdersAndAuditee#1]
DEBUG [main] (DefaultLoadEventListener.java:244) - entity found in session cache
DEBUG [main] (TwoPhaseLoad.java:206) - done materializing entity [model.ItemAndAuditee#1]
DEBUG [main] (TwoPhaseLoad.java:107) - resolving associations for [model.ItemAndAuditee#2]
DEBUG [main] (DefaultLoadEventListener.java:171) - loading entity: [model.OrdersAndAuditee#1]
DEBUG [main] (DefaultLoadEventListener.java:244) - entity found in session cache
DEBUG [main] (TwoPhaseLoad.java:206) - done materializing entity [model.ItemAndAuditee#2]
DEBUG [main] (StatefulPersistenceContext.java:748) - initializing non-lazy collections
DEBUG [main] (SessionImpl.java:1289) - setting flush mode to: AUTO
DEBUG [main] (SessionImpl.java:1308) - setting cache mode to: NORMAL
DEBUG [main] (DefaultInitializeCollectionEventListener.java:61) - collection initialized
DEBUG [main] (ThreadLocalSessionContext.java:300) - allowing proxied method [getTransaction] to proceed to real session
DEBUG [main] (Test.java:175) - Trying to rollback database transaction after exception
DEBUG [main] (ThreadLocalSessionContext.java:300) - allowing proxied method [getTransaction] to proceed to real session
DEBUG [main] (JDBCTransaction.java:152) - rollback
DEBUG [main] (JDBCTransaction.java:193) - re-enabling autocommit
DEBUG [main] (JDBCTransaction.java:163) - rolled back JDBC Connection
DEBUG [main] (JDBCContext.java:215) - after transaction completion
DEBUG [main] (ConnectionManager.java:398) - aggressively releasing JDBC connection
DEBUG [main] (ConnectionManager.java:435) - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG [main] (SessionImpl.java:422) - after transaction completion
DEBUG [main] (SessionImpl.java:353) - automatically closing session
DEBUG [main] (SessionImpl.java:273) - closing session
DEBUG [main] (ConnectionManager.java:369) - connection already null in cleanup : no action
DEBUG [main] (ThreadLocalSessionContext.java:300) - allowing proxied method [isOpen] to proceed to real session
java.lang.NullPointerException
at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
at test.Test.main(Test.java:151)
Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException
at test.Test.main(Test.java:184)
Caused by: java.lang.NullPointerException
at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
at test.Test.main(Test.java:151)
|