diff --git a/core/src/main/java/org/hibernate/engine/query/NamedParameterDescriptor.java b/core/src/main/java/org/hibernate/engine/query/NamedParameterDescriptor.java index be7fb3cd18..132410b275 100644 --- a/core/src/main/java/org/hibernate/engine/query/NamedParameterDescriptor.java +++ b/core/src/main/java/org/hibernate/engine/query/NamedParameterDescriptor.java @@ -35,7 +35,7 @@ import java.io.Serializable; */ public class NamedParameterDescriptor implements Serializable { private final String name; - private final Type expectedType; + private Type expectedType; private final int[] sourceLocations; private final boolean jpaStyle; @@ -61,4 +61,8 @@ public class NamedParameterDescriptor implements Serializable { public boolean isJpaStyle() { return jpaStyle; } + + public void resetExpectedType(Type type) { + this.expectedType = type; + } } diff --git a/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java index d139f30c0d..3b3bb25e3a 100755 --- a/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/AbstractEntityManagerImpl.java @@ -27,8 +27,8 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; +import java.util.List; import java.util.Map; -import java.util.Set; import javax.persistence.EntityNotFoundException; import javax.persistence.EntityTransaction; import javax.persistence.FlushModeType; @@ -59,6 +59,7 @@ import org.slf4j.LoggerFactory; import org.hibernate.*; import org.hibernate.cfg.Environment; +import org.hibernate.ejb.criteria.ValueConverter; import org.hibernate.ejb.transaction.JoinableCMTTransaction; import org.hibernate.ejb.util.ConfigurationHelper; import org.hibernate.ejb.criteria.CriteriaQueryCompiler; @@ -66,6 +67,7 @@ import org.hibernate.engine.SessionFactoryImplementor; import org.hibernate.engine.SessionImplementor; import org.hibernate.proxy.HibernateProxy; import org.hibernate.transaction.TransactionFactory; +import org.hibernate.transform.BasicTransformerAdapter; import org.hibernate.util.CollectionHelper; import org.hibernate.util.JTAHelper; @@ -140,6 +142,45 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage } } + public TypedQuery createQuery( + String jpaqlString, + Class resultClass, + Options options) { + try { + org.hibernate.Query hqlQuery = getSession().createQuery( jpaqlString ); + if ( options.getConversions() != null ) { + hqlQuery.setResultTransformer( new ValueConversionResultTransformer( options.getConversions() ) ); + } + else { + options.getResultMetadataValidator().validate( hqlQuery.getReturnTypes() ); + } + return new QueryImpl( hqlQuery, this, options.getNamedParameterExplicitTypes() ); + } + catch ( HibernateException he ) { + throw convert( he ); + } + } + + private static class ValueConversionResultTransformer extends BasicTransformerAdapter { + private List conversions; + + private ValueConversionResultTransformer(List conversions) { + this.conversions = conversions; + } + + @Override + public Object transformTuple(Object[] tuple, String[] aliases) { + Object[] result = new Object[ tuple.length ]; + for ( int i = 0; i < tuple.length; i++ ) { + ValueConverter.Conversion conversion = conversions.get( i ); + result[i] = conversion == null + ? tuple[i] + : conversion.apply( tuple[i] ); + } + return result.length == 1 ? result[0] : result; + } + } + private CriteriaQueryCompiler criteriaQueryCompiler; public TypedQuery createQuery(CriteriaQuery criteriaQuery) { diff --git a/entitymanager/src/main/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java b/entitymanager/src/main/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java index 0cf7407451..c82b548352 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/HibernateEntityManagerImplementor.java @@ -25,11 +25,16 @@ package org.hibernate.ejb; import javax.persistence.PersistenceException; import javax.persistence.LockModeType; +import javax.persistence.TypedQuery; import org.hibernate.HibernateException; import org.hibernate.StaleStateException; import org.hibernate.LockOptions; +import org.hibernate.ejb.criteria.ValueConverter; +import org.hibernate.transform.ResultTransformer; +import org.hibernate.type.Type; +import java.util.List; import java.util.Map; /** @@ -108,4 +113,38 @@ public interface HibernateEntityManagerImplementor extends HibernateEntityManage * @return the LockOptions */ public LockOptions getLockRequest(LockModeType lockModeType, Map properties); + + public static interface Options { + public static interface ResultMetadataValidator { + public void validate(Type[] returnTypes); + } + + /** + * Get the conversions for the individual tuples in the query results. + * + * @return Value conversions to be applied to the JPA QL results + */ + public List getConversions(); + + /** + * Get the explicit parameter types. Generally speaking these would apply to implicit named + * parameters. + * + * @return The + */ + public Map getNamedParameterExplicitTypes(); + + public ResultMetadataValidator getResultMetadataValidator(); + } + + /** + * Used during "compiling" a JPA criteria query. + * + * @param jpaqlString The criteria query rendered as a JPA QL string + * @param resultClass The result type (the type expected in the result list) + * @param options The options to use to build the query. + * @param The query type + * @return The typed query + */ + public TypedQuery createQuery(String jpaqlString, Class resultClass, Options options); } diff --git a/entitymanager/src/main/java/org/hibernate/ejb/QueryImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/QueryImpl.java index 78355d923d..fd6d9ff794 100755 --- a/entitymanager/src/main/java/org/hibernate/ejb/QueryImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/QueryImpl.java @@ -25,9 +25,11 @@ package org.hibernate.ejb; import java.util.Calendar; import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import javax.persistence.NoResultException; @@ -56,6 +58,7 @@ import org.hibernate.engine.query.NamedParameterDescriptor; import org.hibernate.engine.query.OrdinalParameterDescriptor; import org.hibernate.hql.QueryExecutionRequestException; import org.hibernate.impl.AbstractQueryImpl; +import org.hibernate.type.TypeFactory; /** * Hibernate implementation of both the {@link Query} and {@link TypedQuery} contracts. @@ -72,13 +75,20 @@ public class QueryImpl extends org.hibernate.ejb.AbstractQueryImpl impleme private Set> parameters; public QueryImpl(org.hibernate.Query query, AbstractEntityManagerImpl em) { + this( query, em, Collections.emptyMap() ); + } + + public QueryImpl( + org.hibernate.Query query, + AbstractEntityManagerImpl em, + Map namedParameterTypeRedefinitions) { super( em ); this.query = query; - extractParameterInfo(); + extractParameterInfo( namedParameterTypeRedefinitions ); } @SuppressWarnings({ "unchecked", "RedundantCast" }) - private void extractParameterInfo() { + private void extractParameterInfo(Map namedParameterTypeRedefinition) { if ( ! AbstractQueryImpl.class.isInstance( query ) ) { throw new IllegalStateException( "Unknown query type for parameter extraction" ); } @@ -90,12 +100,16 @@ public class QueryImpl extends org.hibernate.ejb.AbstractQueryImpl impleme for ( String name : (Set) queryImpl.getParameterMetadata().getNamedParameterNames() ) { final NamedParameterDescriptor descriptor = queryImpl.getParameterMetadata().getNamedParameterDescriptor( name ); - final ParameterImpl parameter = new ParameterImpl( - name, - descriptor.getExpectedType() == null - ? null - : descriptor.getExpectedType().getReturnedClass() - ); + Class javaType = namedParameterTypeRedefinition.get( name ); + if ( javaType != null ) { + descriptor.resetExpectedType( + TypeFactory.heuristicType( javaType.getName() ) + ); + } + else if ( descriptor.getExpectedType() != null ) { + javaType = descriptor.getExpectedType().getReturnedClass(); + } + final ParameterImpl parameter = new ParameterImpl( name, javaType ); parameters.add( parameter ); if ( descriptor.isJpaStyle() ) { if ( jpaPositionalIndices == null ) { diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaBuilderImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaBuilderImpl.java index 9fc50a2711..c75bbd35b7 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaBuilderImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaBuilderImpl.java @@ -48,6 +48,7 @@ import org.hibernate.ejb.criteria.expression.CoalesceExpression; import org.hibernate.ejb.criteria.expression.CollectionExpression; import org.hibernate.ejb.criteria.expression.CompoundSelectionImpl; import org.hibernate.ejb.criteria.expression.ConcatExpression; +import org.hibernate.ejb.criteria.ExpressionImplementor; import org.hibernate.ejb.criteria.expression.ParameterExpressionImpl; import org.hibernate.ejb.criteria.expression.LiteralExpression; import org.hibernate.ejb.criteria.expression.NullifExpression; @@ -1062,50 +1063,50 @@ public class CriteriaBuilderImpl implements CriteriaBuilder, Serializable { /** * {@inheritDoc} */ - public Expression toLong(Expression expression) { - return expression.as( Long.class ); + public ExpressionImplementor toLong(Expression expression) { + return ( (ExpressionImplementor) expression ).asLong(); } /** * {@inheritDoc} */ - public Expression toInteger(Expression expression) { - return expression.as( Integer.class ); + public ExpressionImplementor toInteger(Expression expression) { + return ( (ExpressionImplementor) expression ).asInteger(); } /** * {@inheritDoc} */ - public Expression toFloat(Expression expression) { - return expression.as( Float.class ); + public ExpressionImplementor toFloat(Expression expression) { + return ( (ExpressionImplementor) expression ).asFloat(); } /** * {@inheritDoc} */ - public Expression toDouble(Expression expression) { - return expression.as( Double.class ); + public ExpressionImplementor toDouble(Expression expression) { + return ( (ExpressionImplementor) expression ).asDouble(); } /** * {@inheritDoc} */ - public Expression toBigDecimal(Expression expression) { - return expression.as( BigDecimal.class ); + public ExpressionImplementor toBigDecimal(Expression expression) { + return ( (ExpressionImplementor) expression ).asBigDecimal(); } /** * {@inheritDoc} */ - public Expression toBigInteger(Expression expression) { - return expression.as( BigInteger.class ); + public ExpressionImplementor toBigInteger(Expression expression) { + return ( (ExpressionImplementor) expression ).asBigInteger(); } /** * {@inheritDoc} */ - public Expression toString(Expression characterExpression) { - return characterExpression.as( String.class ); + public ExpressionImplementor toString(Expression characterExpression) { + return ( (ExpressionImplementor) characterExpression ).asString(); } diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaQueryCompiler.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaQueryCompiler.java index bd8f4bd0a4..bf459697e3 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaQueryCompiler.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaQueryCompiler.java @@ -55,6 +55,8 @@ import org.hibernate.util.StringHelper; */ public class CriteriaQueryCompiler { public static interface ImplicitParameterBinding { + public String getParameterName(); + public Class getJavaType(); public void bind(TypedQuery typedQuery); } @@ -70,6 +72,8 @@ public class CriteriaQueryCompiler { public static interface RenderedCriteriaQuery { public String getQueryString(); + public List getValueConversions(); + public HibernateEntityManagerImplementor.Options.ResultMetadataValidator getResultMetadataValidator(); } private final HibernateEntityManagerImplementor entityManager; @@ -85,6 +89,7 @@ public class CriteriaQueryCompiler { final Map,String> explicitParameterMapping = new HashMap,String>(); final Map> explicitParameterNameMapping = new HashMap>(); final List implicitParameterBindings = new ArrayList(); + final Map implicitParameterTypes = new HashMap(); RenderingContext renderingContext = new RenderingContext() { private int aliasCount = 0; @@ -110,6 +115,10 @@ public class CriteriaQueryCompiler { public void registerImplicitParameterBinding(ImplicitParameterBinding binding) { implicitParameterBindings.add( binding ); + implicitParameterTypes.put( + binding.getParameterName(), + binding.getJavaType() + ); } public String getCastType(Class javaType) { @@ -132,12 +141,26 @@ public class CriteriaQueryCompiler { } }; - RenderedCriteriaQuery renderedCriteriaQuery = criteriaQueryImpl.render( renderingContext ); + final RenderedCriteriaQuery renderedCriteriaQuery = criteriaQueryImpl.render( renderingContext ); TypedQuery jpaqlQuery = entityManager.createQuery( renderedCriteriaQuery.getQueryString(), - criteriaQuery.getResultType() + criteriaQuery.getResultType(), + new HibernateEntityManagerImplementor.Options() { + public List getConversions() { + return renderedCriteriaQuery.getValueConversions(); + } + + public Map getNamedParameterExplicitTypes() { + return implicitParameterTypes; + } + + public ResultMetadataValidator getResultMetadataValidator() { + return renderedCriteriaQuery.getResultMetadataValidator(); + } + } ); + for ( ImplicitParameterBinding implicitParameterBinding : implicitParameterBindings ) { implicitParameterBinding.bind( jpaqlQuery ); } diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaQueryImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaQueryImpl.java index 613579c794..151372b348 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaQueryImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/CriteriaQueryImpl.java @@ -38,6 +38,9 @@ import javax.persistence.Tuple; import javax.persistence.criteria.Subquery; import javax.persistence.metamodel.EntityType; +import org.hibernate.ejb.HibernateEntityManagerImplementor; +import org.hibernate.type.Type; + /** * The Hibernate implementation of the JPA {@link CriteriaQuery} contract. Mostly a set of delegation to its * internal {@link QueryStructure}. @@ -350,6 +353,41 @@ public class CriteriaQueryImpl extends AbstractNode implements CriteriaQuery< public String getQueryString() { return jpaqlQuery.toString(); } + + @SuppressWarnings({ "unchecked" }) + public List getValueConversions() { + SelectionImplementor selection = (SelectionImplementor) queryStructure.getSelection(); + return selection == null + ? null + : selection.getConversions(); + } + + public HibernateEntityManagerImplementor.Options.ResultMetadataValidator getResultMetadataValidator() { + return new HibernateEntityManagerImplementor.Options.ResultMetadataValidator() { + public void validate(Type[] returnTypes) { + SelectionImplementor selection = (SelectionImplementor) queryStructure.getSelection(); + if ( selection != null ) { + if ( selection.isCompoundSelection() ) { + if ( returnTypes.length != selection.getCompoundSelectionItems().size() ) { + throw new IllegalStateException( + "Number of return values [" + returnTypes.length + + "] did not match expected [" + + selection.getCompoundSelectionItems().size() + "]" + ); + } + } + else { + if ( returnTypes.length > 1 ) { + throw new IllegalStateException( + "Number of return values [" + returnTypes.length + + "] did not match expected [1]" + ); + } + } + } + } + }; + } }; } } diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/ExpressionImplementor.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/ExpressionImplementor.java new file mode 100644 index 0000000000..60253c6000 --- /dev/null +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/ExpressionImplementor.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.math.BigDecimal; +import java.math.BigInteger; +import javax.persistence.criteria.Expression; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +public interface ExpressionImplementor extends SelectionImplementor, Expression, Renderable { + /** + * See {@link javax.persistence.criteria.CriteriaBuilder#toLong} + * + * @return this but as a long + */ + public ExpressionImplementor asLong(); + + /** + * See {@link javax.persistence.criteria.CriteriaBuilder#toInteger} + * + * @return this but as an integer + */ + public ExpressionImplementor asInteger(); + + /** + * See {@link javax.persistence.criteria.CriteriaBuilder#toFloat} + * + * @return this but as a float + */ + public ExpressionImplementor asFloat(); + + /** + * See {@link javax.persistence.criteria.CriteriaBuilder#toDouble} + * + * @return this but as a double + */ + public ExpressionImplementor asDouble(); + + /** + * See {@link javax.persistence.criteria.CriteriaBuilder#toBigDecimal} + * + * @return this but as a {@link BigDecimal} + */ + public ExpressionImplementor asBigDecimal(); + + /** + * See {@link javax.persistence.criteria.CriteriaBuilder#toBigInteger} + * + * @return this but as a {@link BigInteger} + */ + public ExpressionImplementor asBigInteger(); + + /** + * See {@link javax.persistence.criteria.CriteriaBuilder#toString} + * + * @return this but as a string + */ + public ExpressionImplementor asString(); +} diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/SelectionImplementor.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/SelectionImplementor.java new file mode 100644 index 0000000000..0645d73d2c --- /dev/null +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/SelectionImplementor.java @@ -0,0 +1,36 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.List; +import javax.persistence.criteria.Selection; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +public interface SelectionImplementor extends TupleElementImplementor, Selection { + public List getConversions(); +} diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImplementor.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/TupleElementImplementor.java similarity index 63% rename from entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImplementor.java rename to entitymanager/src/main/java/org/hibernate/ejb/criteria/TupleElementImplementor.java index 596bcc8581..2e9bd8c93e 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImplementor.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/TupleElementImplementor.java @@ -1,10 +1,10 @@ /* * 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. + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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 @@ -21,16 +21,15 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.ejb.criteria.expression; +package org.hibernate.ejb.criteria; -import javax.persistence.criteria.Expression; - -import org.hibernate.ejb.criteria.Renderable; +import javax.persistence.TupleElement; /** * TODO : javadoc * * @author Steve Ebersole */ -public interface ExpressionImplementor extends Expression, Renderable { +public interface TupleElementImplementor extends TupleElement { + public ValueConverter.Conversion getConversion(); } diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/ValueConverter.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/ValueConverter.java new file mode 100644 index 0000000000..898d5fb7a3 --- /dev/null +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/ValueConverter.java @@ -0,0 +1,255 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.math.BigDecimal; +import java.math.BigInteger; + +/** + * Helper for generically converting a values into another type. + * + * @author Steve Ebersole + */ +public class ValueConverter { + private ValueConverter() { + } + + public static interface Conversion { + public T apply(Object value); + } + + public static class ByteConversion implements Conversion { + public static final ByteConversion INSTANCE = new ByteConversion(); + @SuppressWarnings({ "UnnecessaryBoxing" }) + public Byte apply(Object value) { + if ( value == null ) { + return null; + } + if ( Number.class.isInstance( value ) ) { + return Byte.valueOf( ( (Number) value ).byteValue() ); + } + else if ( String.class.isInstance( value ) ) { + return Byte.valueOf( ( (String) value ) ); + } + throw unknownConversion( value, Byte.class ); + } + } + + public static class ShortConversion implements Conversion { + public static final ShortConversion INSTANCE = new ShortConversion(); + @SuppressWarnings({ "UnnecessaryBoxing" }) + public Short apply(Object value) { + if ( value == null ) { + return null; + } + if ( Number.class.isInstance( value ) ) { + return Short.valueOf( ( (Number) value ).shortValue() ); + } + else if ( String.class.isInstance( value ) ) { + return Short.valueOf( ( (String) value ) ); + } + throw unknownConversion( value, Short.class ); + } + } + + public static class IntegerConversion implements Conversion { + public static final IntegerConversion INSTANCE = new IntegerConversion(); + @SuppressWarnings({ "UnnecessaryBoxing" }) + public Integer apply(Object value) { + if ( value == null ) { + return null; + } + if ( Number.class.isInstance( value ) ) { + return Integer.valueOf( ( (Number) value ).intValue() ); + } + else if ( String.class.isInstance( value ) ) { + return Integer.valueOf( ( (String) value ) ); + } + throw unknownConversion( value, Integer.class ); + } + } + + public static class LongConversion implements Conversion { + public static final LongConversion INSTANCE = new LongConversion(); + @SuppressWarnings({ "UnnecessaryBoxing" }) + public Long apply(Object value) { + if ( value == null ) { + return null; + } + if ( Number.class.isInstance( value ) ) { + return Long.valueOf( ( (Number) value ).longValue() ); + } + else if ( String.class.isInstance( value ) ) { + return Long.valueOf( ( (String) value ) ); + } + throw unknownConversion( value, Long.class ); + } + } + + public static class FloatConversion implements Conversion { + public static final FloatConversion INSTANCE = new FloatConversion(); + @SuppressWarnings({ "UnnecessaryBoxing" }) + public Float apply(Object value) { + if ( value == null ) { + return null; + } + if ( Number.class.isInstance( value ) ) { + return Float.valueOf( ( (Number) value ).floatValue() ); + } + else if ( String.class.isInstance( value ) ) { + return Float.valueOf( ( (String) value ) ); + } + throw unknownConversion( value, Float.class ); + } + } + + public static class DoubleConversion implements Conversion { + public static final DoubleConversion INSTANCE = new DoubleConversion(); + @SuppressWarnings({ "UnnecessaryBoxing" }) + public Double apply(Object value) { + if ( value == null ) { + return null; + } + if ( Number.class.isInstance( value ) ) { + return Double.valueOf( ( (Number) value ).doubleValue() ); + } + else if ( String.class.isInstance( value ) ) { + return Double.valueOf( ( (String) value ) ); + } + throw unknownConversion( value, Double.class ); + } + } + + public static class BigIntegerConversion implements Conversion { + public static final BigIntegerConversion INSTANCE = new BigIntegerConversion(); + public BigInteger apply(Object value) { + if ( value == null ) { + return null; + } + if ( Number.class.isInstance( value ) ) { + return BigInteger.valueOf( ( (Number) value ).longValue() ); + } + else if ( String.class.isInstance( value ) ) { + return new BigInteger( (String) value ); + } + throw unknownConversion( value, BigInteger.class ); + } + } + + public static class BigDecimalConversion implements Conversion { + public static final BigDecimalConversion INSTANCE = new BigDecimalConversion(); + public BigDecimal apply(Object value) { + if ( value == null ) { + return null; + } + if ( BigInteger.class.isInstance( value ) ) { + return new BigDecimal( (BigInteger) value ); + } + else if ( Number.class.isInstance( value ) ) { + return BigDecimal.valueOf( ( (Number) value ).doubleValue() ); + } + else if ( String.class.isInstance( value ) ) { + return new BigDecimal( (String) value ); + } + throw unknownConversion( value, BigDecimal.class ); + } + } + + public static class StringConversion implements Conversion { + public static final StringConversion INSTANCE = new StringConversion(); + public String apply(Object value) { + return value == null ? null : value.toString(); + } + } + + private static IllegalArgumentException unknownConversion(Object value, Class type) { + return new IllegalArgumentException( + "Unaware how to convert value [" + value + "] to requested type [" + type.getName() + "]" + ); + } + + /** + * Convert the given value into the specified target type. + * + * @param value The value to convert + * @param targetType The type to which it should be converted + * + * @return The converted value. + */ + @SuppressWarnings({ "unchecked" }) + public static T convert(Object value, Class targetType) { + if ( value == null ) { + return null; + } + if ( targetType.equals( value.getClass() ) ) { + return (T) value; + } + + Conversion conversion = determineAppropriateConversion( targetType ); + if ( conversion == null ) { + throw unknownConversion( value, targetType ); + } + return conversion.apply( value ); + } + + /** + * Determine the appropriate {@link Conversion} strategy for converting a value + * to the given target type + * + * @param targetType The target type (to which we want to convert values). + * @param parameterized type for the target type. + * @return The conversion + */ + @SuppressWarnings({ "unchecked" }) + public static Conversion determineAppropriateConversion(Class targetType) { + if ( String.class.equals( targetType ) ) { + return (Conversion) StringConversion.INSTANCE; + } + if ( Byte.class.equals( targetType ) ) { + return (Conversion) ByteConversion.INSTANCE; + } + if ( Short.class.equals( targetType ) ) { + return (Conversion) ShortConversion.INSTANCE; + } + if ( Integer.class.equals( targetType ) ) { + return (Conversion) IntegerConversion.INSTANCE; + } + if ( Long.class.equals( targetType ) ) { + return (Conversion) LongConversion.INSTANCE; + } + if ( Float.class.equals( targetType ) ) { + return (Conversion) FloatConversion.INSTANCE; + } + if ( Double.class.equals( targetType ) ) { + return (Conversion) DoubleConversion.INSTANCE; + } + if ( BigInteger.class.equals( targetType ) ) { + return (Conversion) BigIntegerConversion.INSTANCE; + } + if ( BigDecimal.class.equals( targetType ) ) { + return (Conversion) BigDecimalConversion.INSTANCE; + } + return null; + } +} diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/AbstractTupleElement.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/AbstractTupleElement.java index 822772d485..adcceb1d06 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/AbstractTupleElement.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/AbstractTupleElement.java @@ -23,22 +23,27 @@ */ package org.hibernate.ejb.criteria.expression; -import javax.persistence.TupleElement; - import org.hibernate.ejb.criteria.AbstractNode; import org.hibernate.ejb.criteria.CriteriaBuilderImpl; +import org.hibernate.ejb.criteria.TupleElementImplementor; +import org.hibernate.ejb.criteria.ValueConverter; /** * TODO : javadoc * * @author Steve Ebersole */ -public abstract class AbstractTupleElement extends AbstractNode implements TupleElement { - private final Class javaType; +public abstract class AbstractTupleElement + extends AbstractNode + implements TupleElementImplementor { + private final Class originalJavaType; + private Class javaType; private String alias; + private ValueConverter.Conversion conversion; protected AbstractTupleElement(CriteriaBuilderImpl criteriaBuilder, Class javaType) { super( criteriaBuilder ); + this.originalJavaType = javaType; this.javaType = javaType; } @@ -49,6 +54,26 @@ public abstract class AbstractTupleElement extends AbstractNode implements Tu return javaType; } + @SuppressWarnings({ "unchecked" }) + protected void resetJavaType(Class targetType) { + this.javaType = targetType; +// this.conversion = javaType.equals( originalJavaType ) +// ? null +// : ValueConverter.determineAppropriateConversion( javaType ); + this.conversion = ValueConverter.determineAppropriateConversion( javaType ); + } + + protected void forceConversion(ValueConverter.Conversion conversion) { + this.conversion = conversion; + } + + /** + * {@inheritDoc} + */ + public ValueConverter.Conversion getConversion() { + return conversion; + } + /** * {@inheritDoc} */ diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/CompoundSelectionImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/CompoundSelectionImpl.java index 561c8ac25a..d2f2944c0b 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/CompoundSelectionImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/CompoundSelectionImpl.java @@ -23,6 +23,7 @@ */ package org.hibernate.ejb.criteria.expression; +import java.util.ArrayList; import java.util.List; import javax.persistence.Tuple; import javax.persistence.criteria.CompoundSelection; @@ -32,6 +33,8 @@ import org.hibernate.ejb.criteria.CriteriaQueryCompiler; import org.hibernate.ejb.criteria.ParameterRegistry; import org.hibernate.ejb.criteria.CriteriaBuilderImpl; import org.hibernate.ejb.criteria.Renderable; +import org.hibernate.ejb.criteria.TupleElementImplementor; +import org.hibernate.ejb.criteria.ValueConverter; /** * The Hibernate implementation of the JPA {@link CompoundSelection} @@ -62,6 +65,21 @@ public class CompoundSelectionImpl extends SelectionImpl implements Compou return selectionItems; } + @Override + public List getConversions() { + if ( isConstructor ) { + return null; + } + boolean foundConversions = false; + ArrayList conversions = new ArrayList(); + for ( Selection selection : getCompoundSelectionItems() ) { + ValueConverter.Conversion conversion = ( (TupleElementImplementor) selection ).getConversion(); + conversions.add( conversion ); + foundConversions = foundConversions || conversion != null; + } + return foundConversions ? null : conversions; + } + public void registerParameters(ParameterRegistry registry) { for ( Selection selectionItem : getCompoundSelectionItems() ) { Helper.possibleParameter(selectionItem, registry); diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java index 394083cc2c..af47b5fa79 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java @@ -23,11 +23,14 @@ */ package org.hibernate.ejb.criteria.expression; +import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Collection; import javax.persistence.criteria.Expression; import javax.persistence.criteria.Predicate; import org.hibernate.ejb.criteria.CriteriaBuilderImpl; +import org.hibernate.ejb.criteria.ExpressionImplementor; import org.hibernate.ejb.criteria.expression.function.CastFunction; /** @@ -93,4 +96,67 @@ public abstract class ExpressionImpl public Predicate in(Expression> values) { return queryBuilder().in( this, values ); } + + /** + * {@inheritDoc} + */ + @SuppressWarnings({ "unchecked" }) + public ExpressionImplementor asLong() { + resetJavaType( Long.class ); + return (ExpressionImplementor) this; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings({ "unchecked" }) + public ExpressionImplementor asInteger() { + resetJavaType( Integer.class ); + return (ExpressionImplementor) this; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings({ "unchecked" }) + public ExpressionImplementor asFloat() { + resetJavaType( Float.class ); + return (ExpressionImplementor) this; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings({ "unchecked" }) + public ExpressionImplementor asDouble() { + resetJavaType( Double.class ); + return (ExpressionImplementor) this; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings({ "unchecked" }) + public ExpressionImplementor asBigDecimal() { + resetJavaType( BigDecimal.class ); + return (ExpressionImplementor) this; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings({ "unchecked" }) + public ExpressionImplementor asBigInteger() { + resetJavaType( BigInteger.class ); + return (ExpressionImplementor) this; + } + + /** + * {@inheritDoc} + */ + @SuppressWarnings({ "unchecked" }) + public ExpressionImplementor asString() { + resetJavaType( String.class ); + return (ExpressionImplementor) this; + } } diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/LiteralExpression.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/LiteralExpression.java index cbab0e73b3..4a566d2613 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/LiteralExpression.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/LiteralExpression.java @@ -25,6 +25,7 @@ package org.hibernate.ejb.criteria.expression; import javax.persistence.TypedQuery; +import org.hibernate.ejb.criteria.ValueConverter; import org.hibernate.ejb.criteria.ParameterRegistry; import org.hibernate.ejb.criteria.CriteriaBuilderImpl; import org.hibernate.ejb.criteria.CriteriaQueryCompiler; @@ -35,7 +36,7 @@ import org.hibernate.ejb.criteria.CriteriaQueryCompiler; * @author Steve Ebersole */ public class LiteralExpression extends ExpressionImpl { - private final T literal; + private Object literal; @SuppressWarnings({ "unchecked" }) public LiteralExpression(CriteriaBuilderImpl criteriaBuilder, T literal) { @@ -51,18 +52,27 @@ public class LiteralExpression extends ExpressionImpl { this.literal = literal; } + @SuppressWarnings({ "unchecked" }) public T getLiteral() { - return literal; + return (T) literal; } public void registerParameters(ParameterRegistry registry) { - // nothign to do + // nothing to do } public String render(CriteriaQueryCompiler.RenderingContext renderingContext) { final String parameterName = renderingContext.generateParameterName(); renderingContext.registerImplicitParameterBinding( new CriteriaQueryCompiler.ImplicitParameterBinding() { + public String getParameterName() { + return parameterName; + } + + public Class getJavaType() { + return LiteralExpression.this.getJavaType(); + } + public void bind(TypedQuery typedQuery) { typedQuery.setParameter( parameterName, getLiteral() ); } @@ -74,4 +84,19 @@ public class LiteralExpression extends ExpressionImpl { public String renderProjection(CriteriaQueryCompiler.RenderingContext renderingContext) { return render( renderingContext ); } + + @Override + @SuppressWarnings({ "unchecked" }) + protected void resetJavaType(Class targetType) { + super.resetJavaType( targetType ); + ValueConverter.Conversion conversion = getConversion(); + if ( conversion == null ) { + conversion = ValueConverter.determineAppropriateConversion( targetType ); + forceConversion( conversion ); + } + + if ( conversion != null ) { + literal = conversion.apply( literal ); + } + } } diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/SelectionImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/SelectionImpl.java index 305004226a..c991a452bd 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/SelectionImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/SelectionImpl.java @@ -23,11 +23,14 @@ */ package org.hibernate.ejb.criteria.expression; +import java.util.Collections; import java.util.List; import javax.persistence.criteria.Selection; import org.hibernate.ejb.criteria.ParameterContainer; import org.hibernate.ejb.criteria.CriteriaBuilderImpl; +import org.hibernate.ejb.criteria.SelectionImplementor; +import org.hibernate.ejb.criteria.ValueConverter; /** * The Hibernate implementation of the JPA {@link Selection} @@ -37,7 +40,7 @@ import org.hibernate.ejb.criteria.CriteriaBuilderImpl; */ public abstract class SelectionImpl extends AbstractTupleElement - implements Selection, ParameterContainer { + implements SelectionImplementor, ParameterContainer { public SelectionImpl(CriteriaBuilderImpl criteriaBuilder, Class javaType) { super( criteriaBuilder, javaType ); } @@ -51,6 +54,12 @@ public abstract class SelectionImpl return false; } + public List getConversions() { + return getConversion() == null + ? null + : Collections.singletonList( (ValueConverter.Conversion) getConversion() ); + } + public List> getCompoundSelectionItems() { throw new IllegalStateException( "Not a compund selection" ); } diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java index 9ad5570d74..cfd688a54e 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java @@ -25,6 +25,7 @@ package org.hibernate.ejb.criteria.predicate; import javax.persistence.criteria.Expression; +import org.hibernate.ejb.criteria.ValueConverter; import org.hibernate.ejb.criteria.ParameterRegistry; import org.hibernate.ejb.criteria.CriteriaBuilderImpl; import org.hibernate.ejb.criteria.CriteriaQueryCompiler; @@ -62,7 +63,29 @@ public class ComparisonPredicate extends AbstractSimplePredicate implements Bina super( criteriaBuilder ); this.comparisonOperator = comparisonOperator; this.leftHandSide = leftHandSide; - this.rightHandSide = new LiteralExpression( criteriaBuilder, rightHandSide ); + if ( Number.class.isAssignableFrom( leftHandSide.getJavaType() ) ) { + this.rightHandSide = new LiteralExpression( + criteriaBuilder, + ValueConverter.convert( rightHandSide, (Class) leftHandSide.getJavaType() ) + ); + } + else { + this.rightHandSide = new LiteralExpression( criteriaBuilder, rightHandSide ); + } + } + + public ComparisonPredicate( + CriteriaBuilderImpl criteriaBuilder, + ComparisonOperator comparisonOperator, + Expression leftHandSide, + Number rightHandSide) { + super( criteriaBuilder ); + this.comparisonOperator = comparisonOperator; + this.leftHandSide = leftHandSide; + this.rightHandSide = new LiteralExpression( + criteriaBuilder, + ValueConverter.convert( rightHandSide, leftHandSide.getJavaType() ) + ); } public ComparisonOperator getComparisonOperator() { diff --git a/entitymanager/src/test/java/org/hibernate/ejb/criteria/QueryBuilderTest.java b/entitymanager/src/test/java/org/hibernate/ejb/criteria/QueryBuilderTest.java new file mode 100644 index 0000000000..e4b618c245 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/criteria/QueryBuilderTest.java @@ -0,0 +1,190 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.List; +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +import javax.persistence.metamodel.EntityType; + +import org.hibernate.ejb.criteria.predicate.ComparisonPredicate; +import org.hibernate.ejb.metamodel.Address; +import org.hibernate.ejb.metamodel.Alias; +import org.hibernate.ejb.metamodel.Country; +import org.hibernate.ejb.metamodel.CreditCard; +import org.hibernate.ejb.metamodel.Customer; +import org.hibernate.ejb.metamodel.Info; +import org.hibernate.ejb.metamodel.LineItem; +import org.hibernate.ejb.metamodel.Order; +import org.hibernate.ejb.metamodel.MetamodelImpl; +import org.hibernate.ejb.metamodel.Phone; +import org.hibernate.ejb.metamodel.Product; +import org.hibernate.ejb.metamodel.ShelfLife; +import org.hibernate.ejb.metamodel.Spouse; +import org.hibernate.ejb.test.TestCase; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +public class QueryBuilderTest extends TestCase { + @Override + public Class[] getAnnotatedClasses() { + return new Class[] { + Address.class, + Alias.class, + Country.class, + CreditCard.class, + Customer.class, + Info.class, + LineItem.class, + Order.class, + Phone.class, + Product.class, + ShelfLife.class, + Spouse.class + }; + } + + public void testEqualityComparisonLiteralConversion() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + + CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder(); + MetamodelImpl mm = (MetamodelImpl) em.getMetamodel(); + + CriteriaQuery cquery = cb.createQuery( Integer.class ); + Root product = cquery.from( Product.class ); + EntityType Product_ = mm.entity( Product.class ); + + cquery.select( + cb.toInteger( + product.get( + Product_.getSingularAttribute("quantity", Integer.class)) + ) + ); + + ComparisonPredicate predicate = (ComparisonPredicate) cb.equal( + product.get( Product_.getSingularAttribute( "partNumber", Long.class ) ), + 373767373 + ); + assertEquals( Long.class, predicate.getRightHandOperand().getJavaType() ); + cquery.where( predicate ); + em.createQuery( cquery ).getResultList(); + + predicate = (ComparisonPredicate) cb.ge( + cb.length( product.get( Product_.getSingularAttribute( "name", String.class ) ) ), + 4L + ); + assertEquals( Integer.class, predicate.getRightHandOperand().getJavaType() ); + cquery.where( predicate ); + em.createQuery( cquery ).getResultList(); + + em.getTransaction().commit(); + em.close(); + } + + public void testTypeConversion() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder(); + MetamodelImpl mm = (MetamodelImpl) em.getMetamodel(); + EntityType Product_ = mm.entity( Product.class ); + + // toFloat + CriteriaQuery floatQuery = cb.createQuery( Float.class ); + Root product = floatQuery.from( Product.class ); + floatQuery.select( + cb.toFloat( + product.get(Product_.getSingularAttribute("quantity", Integer.class)) + ) + ); + em.createQuery( floatQuery ).getResultList(); + + // toDouble + CriteriaQuery doubleQuery = cb.createQuery(Double.class); + product = doubleQuery.from( Product.class ); + doubleQuery.select( + cb.toDouble( + product.get(Product_.getSingularAttribute("quantity", Integer.class)) + ) + ); + em.createQuery( doubleQuery ).getResultList(); + + em.getTransaction().commit(); + em.close(); + } + + public void testConstructor() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder(); + MetamodelImpl mm = (MetamodelImpl) em.getMetamodel(); + + CriteriaQuery cquery = cb.createQuery(Customer.class); + Root customer = cquery.from(Customer.class); + EntityType Customer_ = customer.getModel(); + + cquery.select( + cb.construct( + Customer.class, + customer.get(Customer_.getSingularAttribute("id", String.class)), + customer.get(Customer_.getSingularAttribute("name", String.class)) + ) + ); + TypedQuery tq = em.createQuery(cquery); + tq.getResultList(); + + em.getTransaction().commit(); + em.close(); + } + + public void testDateTimeFunctions() { + EntityManager em = getOrCreateEntityManager(); + em.getTransaction().begin(); + CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder(); + MetamodelImpl mm = (MetamodelImpl) em.getMetamodel(); + + CriteriaQuery dateQuery = cb.createQuery(java.sql.Date.class); + dateQuery.from( Customer.class ); + dateQuery.select( cb.currentDate() ); + em.createQuery( dateQuery ).getResultList(); + + CriteriaQuery timeQuery = cb.createQuery(java.sql.Time.class); + timeQuery.from( Customer.class ); + timeQuery.select( cb.currentTime() ); + em.createQuery( timeQuery ).getResultList(); + + CriteriaQuery tsQuery = cb.createQuery(java.sql.Timestamp.class); + tsQuery.from( Customer.class ); + tsQuery.select( cb.currentTimestamp() ); + em.createQuery( tsQuery ).getResultList(); + + em.getTransaction().commit(); + em.close(); + } +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Address.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Address.java new file mode 100644 index 0000000000..3e2fda2476 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Address.java @@ -0,0 +1,124 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.metamodel; + +import java.util.Collection; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +@Table(name = "ADDRESS") +public class Address implements java.io.Serializable { + private String id; + private String street; + private String city; + private String state; + private String zip; + private Collection phones = new java.util.ArrayList(); + + public Address() { + } + + public Address(String id, String street, String city, String state, String zip) { + this.id = id; + this.street = street; + this.city = city; + this.state = state; + this.zip = zip; + } + + public Address(String id, String street, String city, String state, String zip, + Collection phones) { + this.id = id; + this.street = street; + this.city = city; + this.state = state; + this.zip = zip; + this.phones = phones; + } + + @Id + @Column(name = "ID") + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Column(name = "STREET") + public String getStreet() { + return street; + } + + public void setStreet(String street) { + this.street = street; + } + + @Column(name = "CITY") + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + @Column(name = "STATE") + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + @Column(name = "ZIP") + public String getZip() { + return zip; + } + + public void setZip(String zip) { + this.zip = zip; + } + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "address") + public Collection getPhones() { + return phones; + } + + public void setPhones(Collection phones) { + this.phones = phones; + } +} \ No newline at end of file diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Alias.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Alias.java new file mode 100644 index 0000000000..7103cc433e --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Alias.java @@ -0,0 +1,123 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.metamodel; + +import java.util.Collection; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +@Table(name = "ALIAS_TABLE") +public class Alias implements java.io.Serializable { + private String id; + private String alias; + private Customer customerNoop; + private Collection customersNoop = new java.util.ArrayList(); + private Collection customers = new java.util.ArrayList(); + + public Alias() { + } + + public Alias(String id, String alias) { + this.id = id; + this.alias = alias; + } + + @Id + @Column(name = "ID") + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Column(name = "ALIAS") + public String getAlias() { + return alias; + } + + public void setAlias(String alias) { + this.alias = alias; + } + + @OneToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "FK1_FOR_CUSTOMER_TABLE", insertable = false, updatable = false) + public Customer getCustomerNoop() { + return customerNoop; + } + + public void setCustomerNoop(Customer customerNoop) { + this.customerNoop = customerNoop; + } + + @ManyToMany(cascade = CascadeType.ALL) + @JoinTable(name = "FKS_ANOOP_CNOOP", + joinColumns = + @JoinColumn( + name = "FK2_FOR_ALIAS_TABLE", referencedColumnName = "ID"), + inverseJoinColumns = + @JoinColumn( + name = "FK8_FOR_CUSTOMER_TABLE", referencedColumnName = "ID") + ) + public Collection getCustomersNoop() { + return customersNoop; + } + + public void setCustomersNoop(Collection customersNoop) { + this.customersNoop = customersNoop; + + } + + @ManyToMany(cascade = CascadeType.ALL) + @JoinTable(name = "FKS_ALIAS_CUSTOMER", + joinColumns = + @JoinColumn( + name = "FK_FOR_ALIAS_TABLE", referencedColumnName = "ID"), + inverseJoinColumns = + @JoinColumn( + name = "FK_FOR_CUSTOMER_TABLE", referencedColumnName = "ID") + ) + public Collection getCustomers() { + return customers; + } + + public void setCustomers(Collection customers) { + this.customers = customers; + } + +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Country.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Country.java new file mode 100644 index 0000000000..b374fa5c31 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Country.java @@ -0,0 +1,65 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.metamodel; + +import javax.persistence.Basic; +import javax.persistence.Embeddable; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Embeddable +public class Country implements java.io.Serializable { + private String country; + private String code; + + public Country() { + } + + public Country(String v1, String v2) { + country = v1; + code = v2; + } + + @Basic + public String getCountry() { + return country; + } + + public void setCountry(String v) { + country = v; + } + + @Basic + public String getCode() { + return code; + } + + public void setCode(String v) { + code = v; + } +} + diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/CreditCard.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/CreditCard.java new file mode 100644 index 0000000000..b3e0b8f567 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/CreditCard.java @@ -0,0 +1,153 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.metamodel; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +@Table(name = "CREDITCARD_TABLE") +public class CreditCard implements java.io.Serializable { + private String id; + private String number; + private String type; + private String expires; + private boolean approved; + private double balance; + private Order order; + private Customer customer; + + public CreditCard() { + } + + public CreditCard( + String v1, String v2, String v3, String v4, + boolean v5, double v6, Order v7, Customer v8) { + id = v1; + number = v2; + type = v3; + expires = v4; + approved = v5; + balance = v6; + order = v7; + customer = v8; + } + + public CreditCard( + String v1, String v2, String v3, String v4, + boolean v5, double v6) { + id = v1; + number = v2; + type = v3; + expires = v4; + approved = v5; + balance = v6; + } + + @Id + @Column(name = "ID") + public String getId() { + return id; + } + + public void setId(String v) { + id = v; + } + + @Column(name = "CREDITCARD_NUMBER") + public String getNumber() { + return number; + } + + public void setNumber(String v) { + number = v; + } + + @Column(name = "TYPE") + public String getType() { + return type; + } + + public void setType(String v) { + type = v; + } + + @Column(name = "EXPIRES") + public String getExpires() { + return expires; + } + + public void setExpires(String v) { + expires = v; + } + + @Column(name = "APPROVED") + public boolean getApproved() { + return approved; + } + + public void setApproved(boolean v) { + approved = v; + } + + @Column(name = "BALANCE") + public double getBalance() { + return balance; + } + + public void setBalance(double v) { + balance = v; + } + + @OneToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "FK_FOR_ORDER_TABLE") + public Order getOrder() { + return order; + } + + public void setOrder(Order v) { + order = v; + } + + @ManyToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "FK3_FOR_CUSTOMER_TABLE") + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer v) { + customer = v; + } +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Customer.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Customer.java new file mode 100644 index 0000000000..117354a6ae --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Customer.java @@ -0,0 +1,172 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.metamodel; + +import java.util.Collection; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Embedded; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToMany; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +@Table(name = "CUSTOMER_TABLE") +public class Customer implements java.io.Serializable { + private String id; + private String name; + private Address home; + private Address work; + private Country country; + private Spouse spouse; + private Collection creditCards = new java.util.ArrayList(); + private Collection orders = new java.util.ArrayList(); + private Collection aliases = new java.util.ArrayList(); + private Collection aliasesNoop = new java.util.ArrayList(); + + public Customer() { + } + + public Customer(String id, String name) { + this.id = id; + this.name = name; + } + + public Customer(String id, String name, Country country) { + this.id = id; + this.name = name; + this.country = country; + } + + public Customer(String id, String name, Address home, + Address work, Country country) { + this.id = id; + this.name = name; + this.home = home; + this.work = work; + this.country = country; + } + + @Id + @Column(name = "ID") + public String getId() { + return id; + } + + public void setId(String v) { + this.id = v; + } + + @Column(name = "NAME") + public String getName() { + return name; + } + + public void setName(String v) { + this.name = v; + } + + @Embedded + public Country getCountry() { + return country; + } + + public void setCountry(Country v) { + this.country = v; + } + + @OneToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "FK6_FOR_CUSTOMER_TABLE") + public Address getHome() { + return home; + } + + public void setHome(Address v) { + this.home = v; + } + + @OneToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "FK5_FOR_CUSTOMER_TABLE") + public Address getWork() { + return work; + } + + public void setWork(Address v) { + this.work = v; + } + + @OneToOne(cascade = CascadeType.ALL, mappedBy = "customer") + public Spouse getSpouse() { + return spouse; + } + + public void setSpouse(Spouse v) { + this.spouse = v; + } + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "customer") + public Collection getCreditCards() { + return creditCards; + } + + public void setCreditCards(Collection v) { + this.creditCards = v; + } + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "customer") + public Collection getOrders() { + return orders; + } + + public void setOrders(Collection v) { + this.orders = v; + } + + @ManyToMany(cascade = CascadeType.ALL, mappedBy = "customers") + public Collection getAliases() { + return aliases; + } + + public void setAliases(Collection v) { + this.aliases = v; + } + + @ManyToMany(cascade = CascadeType.ALL, mappedBy = "customersNoop") + public Collection getAliasesNoop() { + return aliasesNoop; + } + + public void setAliasesNoop(Collection v) { + this.aliasesNoop = v; + } +} \ No newline at end of file diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Info.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Info.java new file mode 100644 index 0000000000..b1b4560c9c --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Info.java @@ -0,0 +1,124 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.metamodel; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +@Table(name = "INFO_TABLE") +public class Info implements java.io.Serializable { + private String id; + private String street; + private String city; + private String state; + private String zip; + private Spouse spouse; + + public Info() { + } + + public Info(String v1, String v2, String v3, String v4, String v5) { + id = v1; + street = v2; + city = v3; + state = v4; + zip = v5; + } + + public Info( + String v1, String v2, String v3, String v4, + String v5, Spouse v6) { + id = v1; + street = v2; + city = v3; + state = v4; + zip = v5; + spouse = v6; + } + + @Id + @Column(name = "ID") + public String getId() { + return id; + } + + public void setId(String v) { + id = v; + } + + @Column(name = "INFOSTREET") + public String getStreet() { + return street; + } + + public void setStreet(String v) { + street = v; + } + + @Column(name = "INFOSTATE") + public String getState() { + return state; + } + + public void setState(String v) { + state = v; + } + + @Column(name = "INFOCITY") + public String getCity() { + return city; + } + + public void setCity(String v) { + city = v; + } + + @Column(name = "INFOZIP") + public String getZip() { + return zip; + } + + public void setZip(String v) { + zip = v; + } + + @OneToOne(mappedBy = "info") + public Spouse getSpouse() { + return spouse; + } + + public void setSpouse(Spouse v) { + this.spouse = v; + } + +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/LineItem.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/LineItem.java new file mode 100644 index 0000000000..44740b16ef --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/LineItem.java @@ -0,0 +1,99 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.metamodel; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +@Table(name = "LINEITEM_TABLE") +public class LineItem implements java.io.Serializable { + private String id; + private int quantity; + private Order order; + private Product product; + + public LineItem() { + } + + public LineItem(String v1, int v2, Order v3, Product v4) { + id = v1; + quantity = v2; + order = v3; + product = v4; + } + + public LineItem(String v1, int v2) { + id = v1; + quantity = v2; + } + + @Id + @Column(name = "ID") + public String getId() { + return id; + } + + public void setId(String v) { + id = v; + } + + @Column(name = "QUANTITY") + public int getQuantity() { + return quantity; + } + + public void setQuantity(int v) { + quantity = v; + } + + @ManyToOne + @JoinColumn(name = "FK1_FOR_ORDER_TABLE") + public Order getOrder() { + return order; + } + + public void setOrder(Order v) { + order = v; + } + + @ManyToOne + @JoinColumn(name = "FK_FOR_PRODUCT_TABLE") + public Product getProduct() { + return product; + } + + public void setProduct(Product v) { + product = v; + } +} \ No newline at end of file diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Order.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Order.java new file mode 100644 index 0000000000..6c1e362666 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Order.java @@ -0,0 +1,141 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.metamodel; + +import java.util.Collection; +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +@Table(name = "ORDER_TABLE") +public class Order implements java.io.Serializable { + private String id; + private double totalPrice; + private Customer customer; + private CreditCard creditCard; + private LineItem sampleLineItem; + private Collection lineItems = new java.util.ArrayList(); + + public Order() { + } + + public Order(String id, double totalPrice) { + this.id = id; + this.totalPrice = totalPrice; + } + + public Order(String id, Customer customer) { + this.id = id; + this.customer = customer; + } + + public Order(String id) { + this.id = id; + } + + //==================================================================== + // getters and setters for State fields + + @Id + @Column(name = "ID") + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Column(name = "TOTALPRICE") + public double getTotalPrice() { + return totalPrice; + } + + public void setTotalPrice(double price) { + this.totalPrice = price; + } + + //==================================================================== + // getters and setters for Association fields + + // MANYx1 + + @ManyToOne + @JoinColumn( + name = "FK4_FOR_CUSTOMER_TABLE") + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer customer) { + this.customer = customer; + } + + //1x1 + + @OneToOne(mappedBy = "order") + public CreditCard getCreditCard() { + return creditCard; + } + + public void setCreditCard(CreditCard cc) { + this.creditCard = cc; + } + + // 1x1 + + @OneToOne(cascade = CascadeType.REMOVE) + @JoinColumn( + name = "FK0_FOR_LINEITEM_TABLE") + public LineItem getSampleLineItem() { + return sampleLineItem; + } + + public void setSampleLineItem(LineItem l) { + this.sampleLineItem = l; + } + + //1xMANY + + @OneToMany(cascade = CascadeType.ALL, mappedBy = "order") + public Collection getLineItems() { + return lineItems; + } + + public void setLineItems(Collection c) { + this.lineItems = c; + } +} \ No newline at end of file diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Phone.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Phone.java new file mode 100644 index 0000000000..273d175af1 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Phone.java @@ -0,0 +1,99 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.metamodel; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +@Table(name = "PHONE_TABLE") +public class Phone implements java.io.Serializable { + private String id; + private String area; + private String number; + private Address address; + + public Phone() { + } + + public Phone(String v1, String v2, String v3) { + id = v1; + area = v2; + number = v3; + } + + public Phone(String v1, String v2, String v3, Address v4) { + id = v1; + area = v2; + number = v3; + address = v4; + } + + @Id + @Column(name = "ID") + public String getId() { + return id; + } + + public void setId(String v) { + id = v; + } + + @Column(name = "AREA") + public String getArea() { + return area; + } + + public void setArea(String v) { + area = v; + } + + @Column(name = "PHONE_NUMBER") + public String getNumber() { + return number; + } + + public void setNumber(String v) { + number = v; + } + + @ManyToOne + @JoinColumn(name = "FK_FOR_ADDRESS") + public Address getAddress() { + return address; + } + + public void setAddress(Address a) { + address = a; + } +} \ No newline at end of file diff --git a/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Spouse.java b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Spouse.java new file mode 100644 index 0000000000..167eda576d --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/metamodel/Spouse.java @@ -0,0 +1,143 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009, Red Hat Inc. or third-party contributors as + * indicated by the @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.metamodel; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; +import javax.persistence.Table; + +/** + * TODO : javadoc + * + * @author Steve Ebersole + */ +@Entity +@Table(name = "SPOUSE_TABLE") +public class Spouse implements java.io.Serializable { + private String id; + private String first; + private String maiden; + private String last; + private String sNumber; + private Info info; + private Customer customer; + + public Spouse() { + } + + public Spouse( + String v1, String v2, String v3, String v4, + String v5, Info v6) { + id = v1; + first = v2; + maiden = v3; + last = v4; + sNumber = v5; + info = v6; + } + + + public Spouse( + String v1, String v2, String v3, String v4, + String v5, Info v6, Customer v7) { + id = v1; + first = v2; + maiden = v3; + last = v4; + sNumber = v5; + info = v6; + customer = v7; + } + + @Id + @Column(name = "ID") + public String getId() { + return id; + } + + public void setId(String v) { + id = v; + } + + @Column(name = "FIRSTNAME") + public String getFirstName() { + return first; + } + + public void setFirstName(String v) { + first = v; + } + + @Column(name = "MAIDENNAME") + public String getMaidenName() { + return maiden; + } + + public void setMaidenName(String v) { + maiden = v; + } + + @Column(name = "LASTNAME") + public String getLastName() { + return last; + } + + public void setLastName(String v) { + last = v; + } + + @Column(name = "SOCSECNUM") + public String getSocialSecurityNumber() { + return sNumber; + } + + public void setSocialSecurityNumber(String v) { + sNumber = v; + } + + @OneToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "FK_FOR_INFO_TABLE") + public Info getInfo() { + return info; + } + + public void setInfo(Info v) { + info = v; + } + + @OneToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "FK7_FOR_CUSTOMER_TABLE") + public Customer getCustomer() { + return customer; + } + + public void setCustomer(Customer v) { + customer = v; + } + +}