Join query
jmacneedshelp Aug 10, 2009 12:13 AMI have the following query:
"select o from Orders o join o.orderitems as item where item.vendor=:vendor and o.status In('Vendors Assigned')"
I'm using the resulting list in an dataTable to list the Orders and OrderItems; However I only want the OrderItems for which the given Vendor is assigned.
After some reading I discovered 'fetch' so I'm using it like this:
"select o from Orders o join fetch o.orderitems as item where item.vendor=:vendor and o.status In('Vendors Assigned')"
The pages loads fine the first time, but when I reload the page I get the following exception:
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.xeosuite.xeoadmin.entity.Vendor at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219) at org.hibernate.type.EntityType.getIdentifier(EntityType.java:407) at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:87) at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:38) at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:491) at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563) at org.hibernate.loader.Loader.doQuery(Loader.java:673) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236) at org.hibernate.loader.Loader.doList(Loader.java:2220) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104) at org.hibernate.loader.Loader.list(Loader.java:2099) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378) at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338) at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121) at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79) at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:65)
Here's the component from which I'm executng the query:
@Stateful @Name("VendorOrder") @Scope(ScopeType.SESSION) public class VendorOrderAction implements VendorOrder { @In private EntityManager entityManager; @In @Out private Vendor vendor; public Vendor getVendor() { return vendor; } public void setVendor(Vendor vendor) { this.vendor = vendor; } public List<Orders> getOrderList() { return entityManager.createQuery( "select o from Orders o join fetch o.orderitems as item where item.vendor=:vendor and o.status In('Vendors Assigned')" ).setParameter("vendor", vendor).getResultList(); }
Does anyone have any idea why I get this exception on second request to the page, but no the first?
Thanks.