An Order contains some OrderItem's. Each OrderItem contains a Product and a quantity. To retrieve the Orders, The following HQL works:
Code:
from Order o left join fetch o.items i join fetch i.product
However, if I specify the select clause:
Code:
select o from Order o left join fetch o.items i join fetch i.product
Then Hibernate will return an error:
Code:
Exception in thread "main" org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=Product,tableAlias=product2_,origin=items items1_,colums={items1_.p_id ,className=lab3.Product}}] [select o from lab3.Order o left join fetch o.items i join fetch i.product]
at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:217)
at org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:727)
at org.hibernate.hql.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:551)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:645)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)
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:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
at $Proxy0.createQuery(Unknown Source)
at lab3.OnlineStoreApp.run(OnlineStoreApp.java:32)
at lab3.OnlineStoreApp.main(OnlineStoreApp.java:14)
The test code is shown below:
Code:
package lab3;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class OnlineStoreApp {
private SessionFactory factory;
private Session session;
public static void main(String[] args) {
new OnlineStoreApp().run();
}
public OnlineStoreApp() {
Configuration cfg = new Configuration();
cfg.configure();
factory = cfg.buildSessionFactory();
}
@SuppressWarnings("unchecked")
private void run() {
session = factory.getCurrentSession();
session.beginTransaction();
Order o = new Order();
o.getItems().add(new OrderItem(new Product("p1"), 10));
o.getItems().add(new OrderItem(new Product("p2"), 20));
session.save(o);
List<Order> orders = session
.createQuery(
"select o from Order o left join fetch o.items i join fetch i.product")
.list();
System.out.println(orders.size());
session.getTransaction().commit();
}
}
package lab3;
import java.util.ArrayList;
import java.util.List;
public class Order {
private Long internalId;
private List<OrderItem> items;
public Order() {
items = new ArrayList<OrderItem>();
}
public Long getInternalId() {
return internalId;
}
public void setInternalId(Long internalId) {
this.internalId = internalId;
}
public List<OrderItem> getItems() {
return items;
}
public void setItems(List<OrderItem> items) {
this.items = items;
}
}
package lab3;
public class OrderItem {
private Product product;
private int qty;
public OrderItem() {
}
public OrderItem(Product product, int qty) {
this();
this.product = product;
this.qty = qty;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
}
package lab3;
public class Product {
private Long internalId;
private String name;
public Product() {
}
public Product(String name) {
this();
this.name = name;
}
public Long getInternalId() {
return internalId;
}
public void setInternalId(Long internalId) {
this.internalId = internalId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:h2:tcp://localhost/~/test</property>
<property name="hibernate.connection.driver_class">org.h2.Driver</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<mapping resource="Product.hbm.xml"/>
<mapping resource="Order.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="lab3.Order" table="orders">
<id name="internalId">
<generator class="sequence"></generator>
</id>
<list name="items" cascade="save-update">
<key column="o_id"></key>
<list-index column="idx"></list-index>
<composite-element class="lab3.OrderItem">
<many-to-one name="product" column="p_id" cascade="save-update"></many-to-one>
<property name="qty"></property>
</composite-element>
</list>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="lab3.Product">
<id name="internalId">
<generator class="sequence"></generator>
</id>
<property name="name"></property>
</class>
</hibernate-mapping>