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
This commit is contained in:
Steve Ebersole 2009-10-26 18:35:29 +00:00
parent d74037756e
commit 5c195024b9
11 changed files with 458 additions and 6 deletions

View File

@ -26,6 +26,8 @@ package org.hibernate.ejb.criteria;
import javax.persistence.criteria.JoinType; import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.From; import javax.persistence.criteria.From;
import javax.persistence.metamodel.CollectionAttribute; import javax.persistence.metamodel.CollectionAttribute;
import javax.persistence.metamodel.ManagedType;
import org.hibernate.ejb.criteria.JoinImplementors.CollectionJoinImplementor; import org.hibernate.ejb.criteria.JoinImplementors.CollectionJoinImplementor;
/** /**
@ -58,6 +60,12 @@ public class CollectionJoinImpl<O,E>
} }
@Override @Override
protected ManagedType<E> getManagedType() {
return ( ManagedType<E> ) getAttribute().getElementType();
}
@Override
@SuppressWarnings({ "unchecked" })
public CollectionJoinImplementor<O, E> correlateTo(CriteriaSubqueryImpl subquery) { public CollectionJoinImplementor<O, E> correlateTo(CriteriaSubqueryImpl subquery) {
CollectionJoinImpl<O,E> correlation = new CollectionJoinImpl<O,E>( CollectionJoinImpl<O,E> correlation = new CollectionJoinImpl<O,E>(
queryBuilder(), queryBuilder(),

View File

@ -56,12 +56,17 @@ public class JoinImpl<Z, X> extends FromImpl<Z, X> implements JoinImplementors.J
javaType, javaType,
lhs, lhs,
joinProperty, joinProperty,
(ManagedType<X>) criteriaBuilder.getEntityManagerFactory().getMetamodel().managedType( javaType ) criteriaBuilder.getEntityManagerFactory().getMetamodel().managedType( javaType )
); );
this.managedType = (ManagedType<X>) getModel(); this.managedType = getManagedType();
this.joinType = joinType; this.joinType = joinType;
} }
@SuppressWarnings({ "unchecked" })
protected ManagedType<X> getManagedType() {
return (ManagedType<X>) getModel();
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -27,6 +27,8 @@ import javax.persistence.criteria.Expression;
import javax.persistence.criteria.JoinType; import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.From; import javax.persistence.criteria.From;
import javax.persistence.metamodel.ListAttribute; import javax.persistence.metamodel.ListAttribute;
import javax.persistence.metamodel.ManagedType;
import org.hibernate.ejb.criteria.JoinImplementors.ListJoinImplementor; import org.hibernate.ejb.criteria.JoinImplementors.ListJoinImplementor;
import org.hibernate.ejb.criteria.expression.ListIndexExpression; import org.hibernate.ejb.criteria.expression.ListIndexExpression;
@ -53,7 +55,12 @@ public class ListJoinImpl<O,E> extends JoinImpl<O,E> implements JoinImplementors
@Override @Override
public ListAttribute<? super O, E> getModel() { public ListAttribute<? super O, E> getModel() {
return (ListAttribute<? super O, E>) getAttribute(); return getAttribute();
}
@Override
protected ManagedType<E> getManagedType() {
return ( ManagedType<E> ) getAttribute().getElementType();
} }
/** /**
@ -64,6 +71,7 @@ public class ListJoinImpl<O,E> extends JoinImpl<O,E> implements JoinImplementors
} }
@Override @Override
@SuppressWarnings({ "unchecked" })
public ListJoinImplementor<O, E> correlateTo(CriteriaSubqueryImpl subquery) { public ListJoinImplementor<O, E> correlateTo(CriteriaSubqueryImpl subquery) {
ListJoinImpl<O,E> correlation = new ListJoinImpl<O,E>( ListJoinImpl<O,E> correlation = new ListJoinImpl<O,E>(
queryBuilder(), queryBuilder(),

View File

@ -31,6 +31,7 @@ import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Path; import javax.persistence.criteria.Path;
import javax.persistence.criteria.From; import javax.persistence.criteria.From;
import javax.persistence.metamodel.MapAttribute; import javax.persistence.metamodel.MapAttribute;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.Type.PersistenceType; import javax.persistence.metamodel.Type.PersistenceType;
import org.hibernate.ejb.criteria.JoinImplementors.MapJoinImplementor; import org.hibernate.ejb.criteria.JoinImplementors.MapJoinImplementor;
@ -63,6 +64,11 @@ public class MapJoinImpl<O,K,V>
return getAttribute(); return getAttribute();
} }
@Override
protected ManagedType<V> getManagedType() {
return ( ManagedType<V> ) getAttribute().getElementType();
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -214,11 +214,10 @@ public class QueryStructure<T> {
if ( getSelection() == null ) { if ( getSelection() == null ) {
// we should have only a single root (query validation should have checked this...) // we should have only a single root (query validation should have checked this...)
final Root root = getRoots().iterator().next(); final Root root = getRoots().iterator().next();
( (TableExpressionMapper) root ).prepareAlias( renderingContext ); jpaqlQuery.append( ( (ExpressionImplementor) root ).renderProjection( renderingContext) );
jpaqlQuery.append( root.getAlias() );
} }
else { else {
( ( ExpressionImplementor ) getSelection() ).renderProjection( renderingContext ); jpaqlQuery.append( ( (ExpressionImplementor) getSelection() ).renderProjection( renderingContext ) );
} }
jpaqlQuery.append( " from " ); jpaqlQuery.append( " from " );

View File

@ -26,6 +26,8 @@ package org.hibernate.ejb.criteria;
import javax.persistence.criteria.JoinType; import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.From; import javax.persistence.criteria.From;
import javax.persistence.metamodel.SetAttribute; import javax.persistence.metamodel.SetAttribute;
import javax.persistence.metamodel.ManagedType;
import org.hibernate.ejb.criteria.JoinImplementors.SetJoinImplementor; import org.hibernate.ejb.criteria.JoinImplementors.SetJoinImplementor;
/** /**
@ -58,6 +60,12 @@ public class SetJoinImpl<O,E>
} }
@Override @Override
protected ManagedType<E> getManagedType() {
return ( ManagedType<E> ) getAttribute().getElementType();
}
@Override
@SuppressWarnings({ "unchecked" })
public SetJoinImplementor<O, E> correlateTo(CriteriaSubqueryImpl subquery) { public SetJoinImplementor<O, E> correlateTo(CriteriaSubqueryImpl subquery) {
SetJoinImpl<O,E> correlation = new SetJoinImpl<O,E>( SetJoinImpl<O,E> correlation = new SetJoinImpl<O,E>(
queryBuilder(), queryBuilder(),

View File

@ -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();
}
}

View File

@ -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<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;
}
}

View File

@ -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;
}
}

View File

@ -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<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;
}
}

View File

@ -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;
}
}