[hibernate-commits] Hibernate SVN: r17845 - in core/trunk/entitymanager/src: test/java/org/hibernate/ejb/criteria and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Oct 26 14:35:30 EDT 2009


Author: steve.ebersole at jboss.com
Date: 2009-10-26 14:35:29 -0400 (Mon, 26 Oct 2009)
New Revision: 17845

Added:
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/CriteriaCompilingTest.java
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Customer.java
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Item.java
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Order.java
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Product.java
Modified:
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CollectionJoinImpl.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/JoinImpl.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/ListJoinImpl.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/MapJoinImpl.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/SetJoinImpl.java
Log:
HHH-4203 - Implement JPA 2.0 criteria apis (compiling)


Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CollectionJoinImpl.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CollectionJoinImpl.java	2009-10-26 17:08:09 UTC (rev 17844)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/CollectionJoinImpl.java	2009-10-26 18:35:29 UTC (rev 17845)
@@ -26,6 +26,8 @@
 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 @@
     }
 
 	@Override
+	protected ManagedType<E> getManagedType() {
+		return ( ManagedType<E> ) getAttribute().getElementType();
+	}
+
+	@Override
+	@SuppressWarnings({ "unchecked" })
 	public CollectionJoinImplementor<O, E> correlateTo(CriteriaSubqueryImpl subquery) {
 		CollectionJoinImpl<O,E> correlation = new CollectionJoinImpl<O,E>(
 				queryBuilder(),

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/JoinImpl.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/JoinImpl.java	2009-10-26 17:08:09 UTC (rev 17844)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/JoinImpl.java	2009-10-26 18:35:29 UTC (rev 17845)
@@ -56,12 +56,17 @@
 				javaType,
 				lhs,
 				joinProperty,
-				(ManagedType<X>) criteriaBuilder.getEntityManagerFactory().getMetamodel().managedType( javaType )
+				criteriaBuilder.getEntityManagerFactory().getMetamodel().managedType( javaType )
 		);
-		this.managedType = (ManagedType<X>) getModel();
+		this.managedType = getManagedType();
 		this.joinType = joinType;
 	}
 
+	@SuppressWarnings({ "unchecked" })
+	protected ManagedType<X> getManagedType() {
+		return (ManagedType<X>) getModel();
+	}
+
 	/**
 	 * {@inheritDoc}
 	 */

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/ListJoinImpl.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/ListJoinImpl.java	2009-10-26 17:08:09 UTC (rev 17844)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/ListJoinImpl.java	2009-10-26 18:35:29 UTC (rev 17845)
@@ -27,6 +27,8 @@
 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,9 +55,14 @@
 
 	@Override
 	public ListAttribute<? super O, E> getModel() {
-        return (ListAttribute<? super O, E>) getAttribute();
+        return getAttribute();
 	}
 
+	@Override
+	protected ManagedType<E> getManagedType() {
+		return ( ManagedType<E> ) getAttribute().getElementType();
+	}
+
 	/**
 	 * {@inheritDoc}
 	 */
@@ -64,6 +71,7 @@
 	}
 
 	@Override
+	@SuppressWarnings({ "unchecked" })
 	public ListJoinImplementor<O, E> correlateTo(CriteriaSubqueryImpl subquery) {
 		ListJoinImpl<O,E> correlation = new ListJoinImpl<O,E>(
 				queryBuilder(),

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/MapJoinImpl.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/MapJoinImpl.java	2009-10-26 17:08:09 UTC (rev 17844)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/MapJoinImpl.java	2009-10-26 18:35:29 UTC (rev 17845)
@@ -31,6 +31,7 @@
 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 @@
 		return getAttribute();
 	}
 
+	@Override
+	protected ManagedType<V> getManagedType() {
+		return ( ManagedType<V> ) getAttribute().getElementType();
+	}
+
 	/**
 	 * {@inheritDoc}
 	 */

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java	2009-10-26 17:08:09 UTC (rev 17844)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryStructure.java	2009-10-26 18:35:29 UTC (rev 17845)
@@ -214,11 +214,10 @@
 		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 " );

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/SetJoinImpl.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/SetJoinImpl.java	2009-10-26 17:08:09 UTC (rev 17844)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/SetJoinImpl.java	2009-10-26 18:35:29 UTC (rev 17845)
@@ -26,6 +26,8 @@
 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 @@
 	}
 
 	@Override
+	protected ManagedType<E> getManagedType() {
+		return ( ManagedType<E> ) getAttribute().getElementType();
+	}
+
+	@Override
+	@SuppressWarnings({ "unchecked" })
 	public SetJoinImplementor<O, E> correlateTo(CriteriaSubqueryImpl subquery) {
 		SetJoinImpl<O,E> correlation = new SetJoinImpl<O,E>(
 				queryBuilder(),

Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/CriteriaCompilingTest.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/CriteriaCompilingTest.java	                        (rev 0)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/CriteriaCompilingTest.java	2009-10-26 18:35:29 UTC (rev 17845)
@@ -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<Customer> criteria = em.getCriteriaBuilder().createQuery( Customer.class );
+		criteria.from( Customer.class );
+		em.createQuery( criteria ).getResultList();
+
+		// Now with...
+		criteria = em.getCriteriaBuilder().createQuery( Customer.class );
+		Root<Customer> 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<Order> criteria = em.getCriteriaBuilder().createQuery( Order.class );
+		Root<Order> 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<Order> criteria = em.getCriteriaBuilder().createQuery( Order.class );
+		Root<Order> root = criteria.from( Order.class );
+		root.fetch( "lineItems" );
+		criteria.select( root );
+		em.createQuery( criteria ).getResultList();
+
+		em.getTransaction().commit();
+		em.close();
+	}
+
+}

Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Customer.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Customer.java	                        (rev 0)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Customer.java	2009-10-26 18:35:29 UTC (rev 17845)
@@ -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
+ */
+ at Entity
+public class Customer {
+	private Long id;
+	private String name;
+	private int status;
+	private Set<Order> orders = new HashSet<Order>();
+
+	@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<Order> getOrders() {
+		return orders;
+	}
+
+	void setOrders(Set<Order> orders) {
+		this.orders = orders;
+	}
+}

Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Item.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Item.java	                        (rev 0)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Item.java	2009-10-26 18:35:29 UTC (rev 17845)
@@ -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
+ */
+ at 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;
+	}
+}

Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Order.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Order.java	                        (rev 0)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Order.java	2009-10-26 18:35:29 UTC (rev 17845)
@@ -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
+ */
+ at Entity
+ at Table( name="t_order" )
+public class Order {
+	private Long id;
+	private Customer customer;
+	private Date date;
+	private Set<Item> lineItems = new HashSet<Item>();
+
+	@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<Item> getLineItems() {
+		return lineItems;
+	}
+
+	void setLineItems(Set<Item> lineItems) {
+		this.lineItems = lineItems;
+	}
+}

Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Product.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Product.java	                        (rev 0)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/criteria/Product.java	2009-10-26 18:35:29 UTC (rev 17845)
@@ -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
+ */
+ at 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;
+	}
+}



More information about the hibernate-commits mailing list