|
From: <hib...@li...> - 2006-06-08 20:53:32
|
Author: ste...@jb...
Date: 2006-06-08 16:52:40 -0400 (Thu, 08 Jun 2006)
New Revision: 9999
Added:
trunk/Hibernate3/test/org/hibernate/test/hql/Classification.java
trunk/Hibernate3/test/org/hibernate/test/hql/ClassificationType.java
Modified:
trunk/Hibernate3/test/org/hibernate/test/hql/ASTParserLoadingTest.java
trunk/Hibernate3/test/org/hibernate/test/hql/Animal.hbm.xml
trunk/Hibernate3/test/org/hibernate/test/hql/Zoo.java
Log:
enums as constant literal in HQL
Modified: trunk/Hibernate3/test/org/hibernate/test/hql/ASTParserLoadingTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/hql/ASTParserLoadingTest.java 2006-06-07 16:10:43 UTC (rev 9998)
+++ trunk/Hibernate3/test/org/hibernate/test/hql/ASTParserLoadingTest.java 2006-06-08 20:52:40 UTC (rev 9999)
@@ -85,6 +85,17 @@
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
}
+
+ public void testJdkEnumStyleEnumConstant() throws Exception {
+ Session s = openSession();
+ s.beginTransaction();
+
+ s.createQuery( "from Zoo z where z.classification = org.hibernate.test.hql.Classification.LAME" ).list();
+
+ s.getTransaction().commit();
+ s.close();
+ }
+
public void testParameterTypeMismatchFails() {
Session s = openSession();
s.beginTransaction();
Modified: trunk/Hibernate3/test/org/hibernate/test/hql/Animal.hbm.xml
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/hql/Animal.hbm.xml 2006-06-07 16:10:43 UTC (rev 9998)
+++ trunk/Hibernate3/test/org/hibernate/test/hql/Animal.hbm.xml 2006-06-08 20:52:40 UTC (rev 9999)
@@ -107,7 +107,8 @@
</id>
<discriminator column="zooType" type="character"/>
<property name="name" type="string"/>
- <map name="mammals">
+ <property name="classification" type="org.hibernate.test.hql.ClassificationType"/>
+ <map name="mammals">
<key column="mammalZoo_id"/>
<index type="string" column="name"/>
<one-to-many class="Mammal"/>
Added: trunk/Hibernate3/test/org/hibernate/test/hql/Classification.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/hql/Classification.java 2006-06-07 16:10:43 UTC (rev 9998)
+++ trunk/Hibernate3/test/org/hibernate/test/hql/Classification.java 2006-06-08 20:52:40 UTC (rev 9999)
@@ -0,0 +1,67 @@
+package org.hibernate.test.hql;
+
+import java.io.Serializable;
+import java.util.HashMap;
+
+/**
+ * Mimic a JDK 5 enum.
+ *
+ * @author Steve Ebersole
+ */
+public class Classification implements Serializable, Comparable {
+
+ public static final Classification COOL = new Classification( "COOL", 0 );
+ public static final Classification LAME = new Classification( "LAME", 1 );
+
+ private static final HashMap INSTANCES = new HashMap();
+ static {
+ INSTANCES.put( COOL.name, COOL );
+ INSTANCES.put( LAME.name, LAME );
+ }
+
+ private final String name;
+ private final int ordinal;
+ private final int hashCode;
+
+ private Classification(String name, int ordinal) {
+ this.name = name;
+ this.ordinal = ordinal;
+
+ int hashCode = name.hashCode();
+ hashCode = 29 * hashCode + ordinal;
+ this.hashCode = hashCode;
+ }
+
+ public String name() {
+ return name;
+ }
+
+ public int ordinal() {
+ return ordinal;
+ }
+
+ public boolean equals(Object obj) {
+ return compareTo( obj ) == 0;
+ }
+
+ public int compareTo(Object o) {
+ int otherOrdinal = ( ( Classification ) o ).ordinal;
+ if ( ordinal == otherOrdinal ) {
+ return 0;
+ }
+ else if ( ordinal > otherOrdinal ) {
+ return 1;
+ }
+ else {
+ return -1;
+ }
+ }
+
+ public int hashCode() {
+ return hashCode;
+ }
+
+ public static Classification valueOf(String name) {
+ return ( Classification ) INSTANCES.get( name );
+ }
+}
Added: trunk/Hibernate3/test/org/hibernate/test/hql/ClassificationType.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/hql/ClassificationType.java 2006-06-07 16:10:43 UTC (rev 9998)
+++ trunk/Hibernate3/test/org/hibernate/test/hql/ClassificationType.java 2006-06-08 20:52:40 UTC (rev 9999)
@@ -0,0 +1,90 @@
+package org.hibernate.test.hql;
+
+import org.hibernate.usertype.UserType;
+import org.hibernate.usertype.EnhancedUserType;
+import org.hibernate.HibernateException;
+import org.hibernate.Hibernate;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.PreparedStatement;
+import java.sql.Types;
+import java.io.Serializable;
+
+/**
+ * todo: describe ClassificationType
+ *
+ * @author Steve Ebersole
+ */
+public class ClassificationType implements EnhancedUserType {
+
+ public int[] sqlTypes() {
+ return new int[] { Types.TINYINT };
+ }
+
+ public Class returnedClass() {
+ return Classification.class;
+ }
+
+ public boolean equals(Object x, Object y) throws HibernateException {
+ if ( x == null && y == null ) {
+ return false;
+ }
+ else if ( x != null ) {
+ return x.equals( y );
+ }
+ else {
+ return y.equals( x );
+ }
+ }
+
+ public int hashCode(Object x) throws HibernateException {
+ return x.hashCode();
+ }
+
+ public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
+ String name = ( String ) Hibernate.STRING.nullSafeGet( rs, names[0] );
+ return name == null ? null : Classification.valueOf( name );
+ }
+
+ public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
+ String name = value == null ? null : ( ( Classification ) value ).name();
+ Hibernate.STRING.nullSafeSet( st, name, index );
+ }
+
+ public Object deepCopy(Object value) throws HibernateException {
+ return value;
+ }
+
+ public boolean isMutable() {
+ return false;
+ }
+
+ public Serializable disassemble(Object value) throws HibernateException {
+ return ( Classification ) value;
+ }
+
+ public Object assemble(Serializable cached, Object owner) throws HibernateException {
+ return cached;
+ }
+
+ public Object replace(Object original, Object target, Object owner) throws HibernateException {
+ return original;
+ }
+
+ public String objectToSQLString(Object value) {
+ return '\'' + extractName( value ) + '\'';
+ }
+
+ public String toXMLString(Object value) {
+ return extractName( value );
+ }
+
+ public Object fromXMLString(String xmlValue) {
+ return Classification.valueOf( xmlValue );
+ }
+
+ private String extractName(Object obj) {
+ return ( ( Classification ) obj ).name();
+ }
+}
Modified: trunk/Hibernate3/test/org/hibernate/test/hql/Zoo.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/hql/Zoo.java 2006-06-07 16:10:43 UTC (rev 9998)
+++ trunk/Hibernate3/test/org/hibernate/test/hql/Zoo.java 2006-06-08 20:52:40 UTC (rev 9999)
@@ -9,6 +9,7 @@
public class Zoo {
private Long id;
private String name;
+ private Classification classification;
private Map animals;
private Map mammals;
private Address address;
|