From 5c195024b9cbb57aacd14a216e6688bfd6631a91 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 26 Oct 2009 18:35:29 +0000 Subject: [PATCH] HHH-4203 - Implement JPA 2.0 criteria apis (compiling) git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17845 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../ejb/criteria/CollectionJoinImpl.java | 8 ++ .../org/hibernate/ejb/criteria/JoinImpl.java | 9 +- .../hibernate/ejb/criteria/ListJoinImpl.java | 10 +- .../hibernate/ejb/criteria/MapJoinImpl.java | 6 ++ .../ejb/criteria/QueryStructure.java | 5 +- .../hibernate/ejb/criteria/SetJoinImpl.java | 8 ++ .../ejb/criteria/CriteriaCompilingTest.java | 97 +++++++++++++++++++ .../org/hibernate/ejb/criteria/Customer.java | 79 +++++++++++++++ .../java/org/hibernate/ejb/criteria/Item.java | 78 +++++++++++++++ .../org/hibernate/ejb/criteria/Order.java | 84 ++++++++++++++++ .../org/hibernate/ejb/criteria/Product.java | 80 +++++++++++++++ 11 files changed, 458 insertions(+), 6 deletions(-) create mode 100644 entitymanager/src/test/java/org/hibernate/ejb/criteria/CriteriaCompilingTest.java create mode 100644 entitymanager/src/test/java/org/hibernate/ejb/criteria/Customer.java create mode 100644 entitymanager/src/test/java/org/hibernate/ejb/criteria/Item.java create mode 100644 entitymanager/src/test/java/org/hibernate/ejb/criteria/Order.java create mode 100644 entitymanager/src/test/java/org/hibernate/ejb/criteria/Product.java diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/CollectionJoinImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/CollectionJoinImpl.java index 8417437485..85378c1f34 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/CollectionJoinImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/CollectionJoinImpl.java @@ -26,6 +26,8 @@ package org.hibernate.ejb.criteria; import javax.persistence.criteria.JoinType; import javax.persistence.criteria.From; import javax.persistence.metamodel.CollectionAttribute; +import javax.persistence.metamodel.ManagedType; + import org.hibernate.ejb.criteria.JoinImplementors.CollectionJoinImplementor; /** @@ -58,6 +60,12 @@ public class CollectionJoinImpl } @Override + protected ManagedType getManagedType() { + return ( ManagedType ) getAttribute().getElementType(); + } + + @Override + @SuppressWarnings({ "unchecked" }) public CollectionJoinImplementor correlateTo(CriteriaSubqueryImpl subquery) { CollectionJoinImpl correlation = new CollectionJoinImpl( queryBuilder(), diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/JoinImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/JoinImpl.java index f55364b83f..e1a7907c13 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/JoinImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/JoinImpl.java @@ -56,12 +56,17 @@ public class JoinImpl extends FromImpl implements JoinImplementors.J javaType, lhs, joinProperty, - (ManagedType) criteriaBuilder.getEntityManagerFactory().getMetamodel().managedType( javaType ) + criteriaBuilder.getEntityManagerFactory().getMetamodel().managedType( javaType ) ); - this.managedType = (ManagedType) getModel(); + this.managedType = getManagedType(); this.joinType = joinType; } + @SuppressWarnings({ "unchecked" }) + protected ManagedType getManagedType() { + return (ManagedType) getModel(); + } + /** * {@inheritDoc} */ diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/ListJoinImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/ListJoinImpl.java index a76b0e556e..51c0244340 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/ListJoinImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/ListJoinImpl.java @@ -27,6 +27,8 @@ import javax.persistence.criteria.Expression; import javax.persistence.criteria.JoinType; import javax.persistence.criteria.From; import javax.persistence.metamodel.ListAttribute; +import javax.persistence.metamodel.ManagedType; + import org.hibernate.ejb.criteria.JoinImplementors.ListJoinImplementor; import org.hibernate.ejb.criteria.expression.ListIndexExpression; @@ -53,7 +55,12 @@ public class ListJoinImpl extends JoinImpl implements JoinImplementors @Override public ListAttribute getModel() { - return (ListAttribute) getAttribute(); + return getAttribute(); + } + + @Override + protected ManagedType getManagedType() { + return ( ManagedType ) getAttribute().getElementType(); } /** @@ -64,6 +71,7 @@ public class ListJoinImpl extends JoinImpl implements JoinImplementors } @Override + @SuppressWarnings({ "unchecked" }) public ListJoinImplementor correlateTo(CriteriaSubqueryImpl subquery) { ListJoinImpl correlation = new ListJoinImpl( queryBuilder(), diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/MapJoinImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/MapJoinImpl.java index e30bd35e80..d3d8c98eb9 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/MapJoinImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/MapJoinImpl.java @@ -31,6 +31,7 @@ import javax.persistence.criteria.JoinType; import javax.persistence.criteria.Path; import javax.persistence.criteria.From; import javax.persistence.metamodel.MapAttribute; +import javax.persistence.metamodel.ManagedType; import javax.persistence.metamodel.Type.PersistenceType; import org.hibernate.ejb.criteria.JoinImplementors.MapJoinImplementor; @@ -63,6 +64,11 @@ public class MapJoinImpl return getAttribute(); } + @Override + protected ManagedType getManagedType() { + return ( ManagedType ) getAttribute().getElementType(); + } + /** * {@inheritDoc} */ diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java index 54577ddb8a..467f536e0f 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java @@ -214,11 +214,10 @@ public class QueryStructure { if ( getSelection() == null ) { // we should have only a single root (query validation should have checked this...) final Root root = getRoots().iterator().next(); - ( (TableExpressionMapper) root ).prepareAlias( renderingContext ); - jpaqlQuery.append( root.getAlias() ); + jpaqlQuery.append( ( (ExpressionImplementor) root ).renderProjection( renderingContext) ); } else { - ( ( ExpressionImplementor ) getSelection() ).renderProjection( renderingContext ); + jpaqlQuery.append( ( (ExpressionImplementor) getSelection() ).renderProjection( renderingContext ) ); } jpaqlQuery.append( " from " ); diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/SetJoinImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/SetJoinImpl.java index 4e4239c8d7..1acae02abb 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/SetJoinImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/SetJoinImpl.java @@ -26,6 +26,8 @@ package org.hibernate.ejb.criteria; import javax.persistence.criteria.JoinType; import javax.persistence.criteria.From; import javax.persistence.metamodel.SetAttribute; +import javax.persistence.metamodel.ManagedType; + import org.hibernate.ejb.criteria.JoinImplementors.SetJoinImplementor; /** @@ -58,6 +60,12 @@ public class SetJoinImpl } @Override + protected ManagedType getManagedType() { + return ( ManagedType ) getAttribute().getElementType(); + } + + @Override + @SuppressWarnings({ "unchecked" }) public SetJoinImplementor correlateTo(CriteriaSubqueryImpl subquery) { SetJoinImpl correlation = new SetJoinImpl( queryBuilder(), diff --git a/entitymanager/src/test/java/org/hibernate/ejb/criteria/CriteriaCompilingTest.java b/entitymanager/src/test/java/org/hibernate/ejb/criteria/CriteriaCompilingTest.java new file mode 100644 index 0000000000..b7efb35ba5 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/criteria/CriteriaCompilingTest.java @@ -0,0 +1,97 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.ejb.criteria; + +import javax.persistence.EntityManager; +import javax.persistence.criteria.Root; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Join; + +import org.hibernate.ejb.test.TestCase; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +public class CriteriaCompilingTest extends TestCase { + public Class[] getAnnotatedClasses() { + return new Class[] { + Customer.class, + Item.class, + Order.class, + Product.class + }; + } + + public void testJustSimpleRootCriteria() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + // First w/o explicit selection... + CriteriaQuery criteria = em.getCriteriaBuilder().createQuery( Customer.class ); + criteria.from( Customer.class ); + em.createQuery( criteria ).getResultList(); + + // Now with... + criteria = em.getCriteriaBuilder().createQuery( Customer.class ); + Root root = criteria.from( Customer.class ); + criteria.select( root ); + em.createQuery( criteria ).getResultList(); + + em.getTransaction().commit(); + em.close(); + } + + public void testSimpleJoinCriteria() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + // String based... + CriteriaQuery criteria = em.getCriteriaBuilder().createQuery( Order.class ); + Root root = criteria.from( Order.class ); + root.join( "lineItems" ); + criteria.select( root ); + em.createQuery( criteria ).getResultList(); + + em.getTransaction().commit(); + em.close(); + } + + public void testSimpleFetchCriteria() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + // String based... + CriteriaQuery criteria = em.getCriteriaBuilder().createQuery( Order.class ); + Root root = criteria.from( Order.class ); + root.fetch( "lineItems" ); + criteria.select( root ); + em.createQuery( criteria ).getResultList(); + + em.getTransaction().commit(); + em.close(); + } + +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/criteria/Customer.java b/entitymanager/src/test/java/org/hibernate/ejb/criteria/Customer.java new file mode 100644 index 0000000000..bb0b357e18 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/criteria/Customer.java @@ -0,0 +1,79 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.ejb.criteria; + +import java.util.Set; +import java.util.HashSet; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; +import javax.persistence.OneToMany; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +public class Customer { + private Long id; + private String name; + private int status; + private Set orders = new HashSet(); + + @Id + @GeneratedValue + public Long getId() { + return id; + } + + void setId(Long id) { + this.id = id; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @OneToMany( mappedBy = "customer" ) + public Set getOrders() { + return orders; + } + + void setOrders(Set orders) { + this.orders = orders; + } +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/criteria/Item.java b/entitymanager/src/test/java/org/hibernate/ejb/criteria/Item.java new file mode 100644 index 0000000000..734adbb77b --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/criteria/Item.java @@ -0,0 +1,78 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.ejb.criteria; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; +import javax.persistence.ManyToOne; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +public class Item { + private Long id; + private Order order; + private Product product; + private long quantity; + + @Id + @GeneratedValue + Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @ManyToOne + public Order getOrder() { + return order; + } + + public void setOrder(Order order) { + this.order = order; + } + + @ManyToOne + public Product getProduct() { + return product; + } + + public void setProduct(Product product) { + this.product = product; + } + + public long getQuantity() { + return quantity; + } + + public void setQuantity(long quantity) { + this.quantity = quantity; + } +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/criteria/Order.java b/entitymanager/src/test/java/org/hibernate/ejb/criteria/Order.java new file mode 100644 index 0000000000..8f1c6a6ae2 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/criteria/Order.java @@ -0,0 +1,84 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.ejb.criteria; + +import java.util.Date; +import java.util.Set; +import java.util.HashSet; +import javax.persistence.Id; +import javax.persistence.GeneratedValue; +import javax.persistence.OneToMany; +import javax.persistence.Entity; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +@Table( name="t_order" ) +public class Order { + private Long id; + private Customer customer; + private Date date; + private Set lineItems = new HashSet(); + + @Id + @GeneratedValue + Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @ManyToOne + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + @OneToMany( mappedBy = "order" ) + public Set getLineItems() { + return lineItems; + } + + void setLineItems(Set lineItems) { + this.lineItems = lineItems; + } +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/criteria/Product.java b/entitymanager/src/test/java/org/hibernate/ejb/criteria/Product.java new file mode 100644 index 0000000000..e75e13bed3 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/criteria/Product.java @@ -0,0 +1,80 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by + * third-party contributors as indicated by either @author tags or express + * copyright attribution statements applied by the authors. All + * third-party contributions are distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.ejb.criteria; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@javax.persistence.Entity +public class Product { + private Long id; + private String name; + private String manufacturer; + private String model; + private String productType; + + @javax.persistence.Id + @javax.persistence.GeneratedValue + public Long getId() { + return id; + } + + void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getManufacturer() { + return manufacturer; + } + + public void setManufacturer(String manufacturer) { + this.manufacturer = manufacturer; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getProductType() { + return productType; + } + + public void setProductType(String productType) { + this.productType = productType; + } +}