[hibernate-commits] Hibernate SVN: r11496 - in trunk/Hibernate3: src/org/hibernate/mapping and 5 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Tue May 8 23:54:06 EDT 2007
Author: steve.ebersole at jboss.com
Date: 2007-05-08 23:54:06 -0400 (Tue, 08 May 2007)
New Revision: 11496
Added:
trunk/Hibernate3/test/org/hibernate/test/usercollection/UserCollectionTypeSuite.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/
trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/Email.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/IMyList.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/MyList.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/MyListType.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/PersistentMyList.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/User.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/UserCollectionTypeTest.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/UserPermissions.hbm.xml
trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/
trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableList.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableListImpl.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableListType.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Entity.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml
trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/ParameterizedUserCollectionTypeTest.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/PersistentDefaultableList.java
Removed:
trunk/Hibernate3/test/org/hibernate/test/usercollection/Email.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/IMyList.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/MyList.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/MyListType.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/PersistentMyList.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/User.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/UserCollectionTypeTest.java
trunk/Hibernate3/test/org/hibernate/test/usercollection/UserPermissions.hbm.xml
Modified:
trunk/Hibernate3/src/org/hibernate/cfg/HbmBinder.java
trunk/Hibernate3/src/org/hibernate/mapping/Collection.java
trunk/Hibernate3/src/org/hibernate/type/CustomCollectionType.java
trunk/Hibernate3/src/org/hibernate/type/TypeFactory.java
trunk/Hibernate3/test/org/hibernate/test/AllTests.java
Log:
HHH-2336 : parameterized UserCollectionType
Modified: trunk/Hibernate3/src/org/hibernate/cfg/HbmBinder.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/cfg/HbmBinder.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/src/org/hibernate/cfg/HbmBinder.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -1345,7 +1345,17 @@
}
Attribute typeNode = node.attribute( "collection-type" );
- if ( typeNode != null ) collection.setTypeName( typeNode.getValue() );
+ if ( typeNode != null ) {
+ String typeName = typeNode.getValue();
+ TypeDef typeDef = mappings.getTypeDef( typeName );
+ if ( typeDef != null ) {
+ collection.setTypeName( typeDef.getTypeClass() );
+ collection.setTypeParameters( typeDef.getParameters() );
+ }
+ else {
+ collection.setTypeName( typeName );
+ }
+ }
// FETCH STRATEGY
Modified: trunk/Hibernate3/src/org/hibernate/mapping/Collection.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/mapping/Collection.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/src/org/hibernate/mapping/Collection.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.Properties;
import org.hibernate.FetchMode;
import org.hibernate.MappingException;
@@ -56,6 +57,7 @@
private boolean optimisticLocked = true;
private Class collectionPersisterClass;
private String typeName;
+ private Properties typeParameters;
private final java.util.Map filters = new HashMap();
private final java.util.Map manyToManyFilters = new HashMap();
private final java.util.Set synchronizedTables = new HashSet();
@@ -345,7 +347,7 @@
return getDefaultCollectionType();
}
else {
- return TypeFactory.customCollection( typeName, role, referencedPropertyName, isEmbedded() );
+ return TypeFactory.customCollection( typeName, typeParameters, role, referencedPropertyName, isEmbedded() );
}
}
@@ -541,6 +543,14 @@
this.typeName = typeName;
}
+ public Properties getTypeParameters() {
+ return typeParameters;
+ }
+
+ public void setTypeParameters(Properties parameterMap) {
+ this.typeParameters = parameterMap;
+ }
+
public boolean[] getColumnInsertability() {
return ArrayHelper.EMPTY_BOOLEAN_ARRAY;
}
Modified: trunk/Hibernate3/src/org/hibernate/type/CustomCollectionType.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/type/CustomCollectionType.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/src/org/hibernate/type/CustomCollectionType.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -87,4 +87,8 @@
return super.renderLoggableString( value, factory );
}
}
+
+ public UserCollectionType getUserType() {
+ return userType;
+ }
}
Modified: trunk/Hibernate3/src/org/hibernate/type/TypeFactory.java
===================================================================
--- trunk/Hibernate3/src/org/hibernate/type/TypeFactory.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/src/org/hibernate/type/TypeFactory.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -256,16 +256,37 @@
}
- public static CollectionType customCollection(String typeName, String role, String propertyRef,
+ /**
+ * The legacy contract.
+ *
+ * @deprecated Use {@link #customCollection(String, java.util.Properties, String, String, boolean)} instead
+ */
+ public static CollectionType customCollection(
+ String typeName,
+ String role,
+ String propertyRef,
boolean embedded) {
+ return customCollection( typeName, null, role, propertyRef, embedded );
+ }
+
+ public static CollectionType customCollection(
+ String typeName,
+ Properties typeParameters,
+ String role,
+ String propertyRef,
+ boolean embedded) {
Class typeClass;
try {
typeClass = ReflectHelper.classForName( typeName );
}
- catch (ClassNotFoundException cnfe) {
- throw new MappingException( "user colllection type class not found: " + typeName, cnfe );
+ catch ( ClassNotFoundException cnfe ) {
+ throw new MappingException( "user collection type class not found: " + typeName, cnfe );
}
- return new CustomCollectionType( typeClass, role, propertyRef, embedded );
+ CustomCollectionType result = new CustomCollectionType( typeClass, role, propertyRef, embedded );
+ if ( typeParameters != null ) {
+ TypeFactory.injectParameters( result.getUserType(), typeParameters );
+ }
+ return result;
}
// Collection Types:
Modified: trunk/Hibernate3/test/org/hibernate/test/AllTests.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/AllTests.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/test/org/hibernate/test/AllTests.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -123,7 +123,7 @@
import org.hibernate.test.unconstrained.UnconstrainedTest;
import org.hibernate.test.unidir.BackrefTest;
import org.hibernate.test.unionsubclass.UnionSubclassTest;
-import org.hibernate.test.usercollection.UserCollectionTypeTest;
+import org.hibernate.test.usercollection.UserCollectionTypeSuite;
import org.hibernate.test.util.UtilSuite;
import org.hibernate.test.version.VersionTest;
import org.hibernate.test.version.db.DbVersionTest;
@@ -298,7 +298,7 @@
suite.addTest( LobSuite.suite() );
suite.addTest( IdentifierPropertyReferencesTest.suite() );
suite.addTest( DeleteTransientEntityTest.suite() );
- suite.addTest( UserCollectionTypeTest.suite() );
+ suite.addTest( UserCollectionTypeSuite.suite() );
suite.addTest( KeyManyToOneSuite.suite() );
suite.addTest( DialectFunctionalTestsSuite.suite() );
suite.addTest( DialectUnitTestsSuite.suite() );
Deleted: trunk/Hibernate3/test/org/hibernate/test/usercollection/Email.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/Email.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/Email.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -1,39 +0,0 @@
-//$Id$
-package org.hibernate.test.usercollection;
-
-/**
- * @author Gavin King
- */
-public class Email {
-
- private Long id;
- private String address;
-
- Email() {}
-
- public String getAddress() {
- return address;
- }
- public void setAddress(String type) {
- this.address = type;
- }
- public Email(String type) {
- this.address = type;
- }
- public boolean equals(Object that) {
- if ( !(that instanceof Email) ) return false;
- Email p = (Email) that;
- return this.address.equals(p.address);
- }
- public int hashCode() {
- return address.hashCode();
- }
-
- public Long getId() {
- return id;
- }
-
- private void setId(Long id) {
- this.id = id;
- }
-}
Deleted: trunk/Hibernate3/test/org/hibernate/test/usercollection/IMyList.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/IMyList.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/IMyList.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -1,7 +0,0 @@
-package org.hibernate.test.usercollection;
-
-import java.util.List;
-
-public interface IMyList extends List {
-
-}
Deleted: trunk/Hibernate3/test/org/hibernate/test/usercollection/MyList.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/MyList.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/MyList.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -1,14 +0,0 @@
-package org.hibernate.test.usercollection;
-
-import java.util.ArrayList;
-
-/**
- * A custom collection class. We extend a java.util.Collection class, but that is not required.
- * It could be totally non-java-collection type, but then we would need to implement all the PersistentCollection methods.
- *
- * @author max
- *
- */
-public class MyList extends ArrayList implements IMyList {
-
-}
Deleted: trunk/Hibernate3/test/org/hibernate/test/usercollection/MyListType.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/MyListType.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/MyListType.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -1,60 +0,0 @@
-package org.hibernate.test.usercollection;
-
-import java.util.Iterator;
-import java.util.Map;
-
-import org.hibernate.EntityMode;
-import org.hibernate.HibernateException;
-import org.hibernate.collection.PersistentCollection;
-import org.hibernate.engine.SessionImplementor;
-import org.hibernate.persister.collection.CollectionPersister;
-import org.hibernate.usertype.UserCollectionType;
-
-public class MyListType implements UserCollectionType {
-
- static int lastInstantiationRequest = -2;
-
- public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister) throws HibernateException {
- return new PersistentMyList(session);
- }
-
- public PersistentCollection wrap(SessionImplementor session, Object collection) {
- if ( session.getEntityMode()==EntityMode.DOM4J ) {
- throw new IllegalStateException("dom4j not supported");
- }
- else {
- return new PersistentMyList( session, (IMyList) collection );
- }
- }
-
- public Iterator getElementsIterator(Object collection) {
- return ( (IMyList) collection ).iterator();
- }
-
- public boolean contains(Object collection, Object entity) {
- return ( (IMyList) collection ).contains(entity);
- }
-
- public Object indexOf(Object collection, Object entity) {
- int l = ( (IMyList) collection ).indexOf(entity);
- if(l<0) {
- return null;
- } else {
- return new Integer(l);
- }
- }
-
- public Object replaceElements(Object original, Object target, CollectionPersister persister, Object owner, Map copyCache, SessionImplementor session) throws HibernateException {
- IMyList result = (IMyList) target;
- result.clear();
- result.addAll((MyList)original);
- return result;
- }
-
- public Object instantiate(int anticipatedSize) {
- lastInstantiationRequest = anticipatedSize;
- return new MyList();
- }
-
-
-}
Deleted: trunk/Hibernate3/test/org/hibernate/test/usercollection/PersistentMyList.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/PersistentMyList.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/PersistentMyList.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -1,18 +0,0 @@
-package org.hibernate.test.usercollection;
-
-import org.hibernate.collection.PersistentList;
-import org.hibernate.engine.SessionImplementor;
-
-public class PersistentMyList extends PersistentList implements IMyList {
-
- public PersistentMyList(SessionImplementor session) {
- super(session);
- }
-
- public PersistentMyList(SessionImplementor session, IMyList list) {
- super(session, list);
- }
-
-
-
-}
Deleted: trunk/Hibernate3/test/org/hibernate/test/usercollection/User.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/User.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/User.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -1,39 +0,0 @@
-//$Id$
-package org.hibernate.test.usercollection;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * @author Gavin King
- */
-public class User {
- private String userName;
- private IMyList emailAddresses = new MyList();
- private Map sessionData = new HashMap();
-
- User() {}
- public User(String name) {
- userName = name;
- }
-
- public String getUserName() {
- return userName;
- }
- public void setUserName(String userName) {
- this.userName = userName;
- }
- public List getEmailAddresses() {
- return emailAddresses;
- }
- public void setEmailAddresses(IMyList emailAddresses) {
- this.emailAddresses = emailAddresses;
- }
- public Map getSessionData() {
- return sessionData;
- }
- public void setSessionData(Map sessionData) {
- this.sessionData = sessionData;
- }
-}
Added: trunk/Hibernate3/test/org/hibernate/test/usercollection/UserCollectionTypeSuite.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/UserCollectionTypeSuite.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/UserCollectionTypeSuite.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,21 @@
+package org.hibernate.test.usercollection;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.hibernate.test.usercollection.basic.UserCollectionTypeTest;
+import org.hibernate.test.usercollection.parameterized.ParameterizedUserCollectionTypeTest;
+
+/**
+ * Suite for testing various aspects of user collection types.
+ *
+ * @author Steve Ebersole
+ */
+public class UserCollectionTypeSuite {
+ public static Test suite() {
+ TestSuite suite = new TestSuite( "user collection type tests" );
+ suite.addTest( UserCollectionTypeTest.suite() );
+ suite.addTest( ParameterizedUserCollectionTypeTest.suite() );
+ return suite;
+ }
+}
Deleted: trunk/Hibernate3/test/org/hibernate/test/usercollection/UserCollectionTypeTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/UserCollectionTypeTest.java 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/UserCollectionTypeTest.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -1,63 +0,0 @@
-//$Id$
-package org.hibernate.test.usercollection;
-
-import junit.framework.Test;
-
-import org.hibernate.Hibernate;
-import org.hibernate.Session;
-import org.hibernate.Transaction;
-import org.hibernate.junit.functional.FunctionalTestCase;
-import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
-
-/**
- * @author Max Rydahl Andersen
- */
-public class UserCollectionTypeTest extends FunctionalTestCase {
-
- public UserCollectionTypeTest(String str) {
- super(str);
- }
-
- public String[] getMappings() {
- return new String[] { "usercollection/UserPermissions.hbm.xml" };
- }
-
- public static Test suite() {
- return new FunctionalTestClassTestSuite( UserCollectionTypeTest.class );
- }
-
- public void testBasicOperation() {
- Session s = openSession();
- Transaction t = s.beginTransaction();
- User u = new User("max");
- u.getEmailAddresses().add( new Email("max at hibernate.org") );
- u.getEmailAddresses().add( new Email("max.andersen at jboss.com") );
- s.persist(u);
- t.commit();
- s.close();
-
- s = openSession();
- t = s.beginTransaction();
- User u2 = (User) s.createCriteria(User.class).uniqueResult();
- assertTrue( Hibernate.isInitialized( u2.getEmailAddresses() ) );
- assertEquals( u2.getEmailAddresses().size(), 2 );
- t.commit();
- s.close();
-
- s = openSession();
- t = s.beginTransaction();
- u2 = ( User ) s.get( User.class, u.getUserName() );
- u2.getEmailAddresses().size();
- assertEquals( 2, MyListType.lastInstantiationRequest );
- t.commit();
- s.close();
-
- s = openSession();
- t = s.beginTransaction();
- s.delete( u );
- t.commit();
- s.close();
- }
-
-}
-
Deleted: trunk/Hibernate3/test/org/hibernate/test/usercollection/UserPermissions.hbm.xml
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/UserPermissions.hbm.xml 2007-05-09 03:52:56 UTC (rev 11495)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/UserPermissions.hbm.xml 2007-05-09 03:54:06 UTC (rev 11496)
@@ -1,33 +0,0 @@
-<?xml version="1.0"?>
-<!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
-
-<!--
-
- This mapping is a basic example of how to write a UserCollectionType.
-
--->
-
-<hibernate-mapping
- package="org.hibernate.test.usercollection">
-
- <import class="Permission"/>
-
- <class name="User" table="`Users`">
- <id name="userName"/>
- <list name="emailAddresses" fetch="join" cascade="all, delete-orphan" collection-type="org.hibernate.test.usercollection.MyListType">
- <key column="userName"/>
- <list-index column="displayOrder" base="1"/>
- <one-to-many class="Email"/>
- </list>
- </class>
-
- <class name="Email">
- <id name="id">
- <generator class="native"/>
- </id>
- <property name="address"/>
- </class>
-
-</hibernate-mapping>
Copied: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/Email.java (from rev 11075, trunk/Hibernate3/test/org/hibernate/test/usercollection/Email.java)
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/Email.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/Email.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,39 @@
+//$Id$
+package org.hibernate.test.usercollection.basic;
+
+/**
+ * @author Gavin King
+ */
+public class Email {
+
+ private Long id;
+ private String address;
+
+ Email() {}
+
+ public String getAddress() {
+ return address;
+ }
+ public void setAddress(String type) {
+ this.address = type;
+ }
+ public Email(String type) {
+ this.address = type;
+ }
+ public boolean equals(Object that) {
+ if ( !(that instanceof Email) ) return false;
+ Email p = (Email) that;
+ return this.address.equals(p.address);
+ }
+ public int hashCode() {
+ return address.hashCode();
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ private void setId(Long id) {
+ this.id = id;
+ }
+}
Property changes on: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/Email.java
___________________________________________________________________
Name: svn:eol-style
+ native
Copied: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/IMyList.java (from rev 11075, trunk/Hibernate3/test/org/hibernate/test/usercollection/IMyList.java)
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/IMyList.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/IMyList.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,7 @@
+package org.hibernate.test.usercollection.basic;
+
+import java.util.List;
+
+public interface IMyList extends List {
+
+}
Property changes on: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/IMyList.java
___________________________________________________________________
Name: svn:eol-style
+ native
Copied: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/MyList.java (from rev 11075, trunk/Hibernate3/test/org/hibernate/test/usercollection/MyList.java)
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/MyList.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/MyList.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,14 @@
+package org.hibernate.test.usercollection.basic;
+
+import java.util.ArrayList;
+
+/**
+ * A custom collection class. We extend a java.util.Collection class, but that is not required.
+ * It could be totally non-java-collection type, but then we would need to implement all the PersistentCollection methods.
+ *
+ * @author max
+ *
+ */
+public class MyList extends ArrayList implements IMyList {
+
+}
Property changes on: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/MyList.java
___________________________________________________________________
Name: svn:eol-style
+ native
Copied: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/MyListType.java (from rev 11075, trunk/Hibernate3/test/org/hibernate/test/usercollection/MyListType.java)
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/MyListType.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/MyListType.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,60 @@
+package org.hibernate.test.usercollection.basic;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import org.hibernate.EntityMode;
+import org.hibernate.HibernateException;
+import org.hibernate.collection.PersistentCollection;
+import org.hibernate.engine.SessionImplementor;
+import org.hibernate.persister.collection.CollectionPersister;
+import org.hibernate.usertype.UserCollectionType;
+
+public class MyListType implements UserCollectionType {
+
+ static int lastInstantiationRequest = -2;
+
+ public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister) throws HibernateException {
+ return new PersistentMyList(session);
+ }
+
+ public PersistentCollection wrap(SessionImplementor session, Object collection) {
+ if ( session.getEntityMode()==EntityMode.DOM4J ) {
+ throw new IllegalStateException("dom4j not supported");
+ }
+ else {
+ return new PersistentMyList( session, (IMyList) collection );
+ }
+ }
+
+ public Iterator getElementsIterator(Object collection) {
+ return ( (IMyList) collection ).iterator();
+ }
+
+ public boolean contains(Object collection, Object entity) {
+ return ( (IMyList) collection ).contains(entity);
+ }
+
+ public Object indexOf(Object collection, Object entity) {
+ int l = ( (IMyList) collection ).indexOf(entity);
+ if(l<0) {
+ return null;
+ } else {
+ return new Integer(l);
+ }
+ }
+
+ public Object replaceElements(Object original, Object target, CollectionPersister persister, Object owner, Map copyCache, SessionImplementor session) throws HibernateException {
+ IMyList result = (IMyList) target;
+ result.clear();
+ result.addAll((MyList)original);
+ return result;
+ }
+
+ public Object instantiate(int anticipatedSize) {
+ lastInstantiationRequest = anticipatedSize;
+ return new MyList();
+ }
+
+
+}
Property changes on: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/MyListType.java
___________________________________________________________________
Name: svn:eol-style
+ native
Copied: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/PersistentMyList.java (from rev 11075, trunk/Hibernate3/test/org/hibernate/test/usercollection/PersistentMyList.java)
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/PersistentMyList.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/PersistentMyList.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,18 @@
+package org.hibernate.test.usercollection.basic;
+
+import org.hibernate.collection.PersistentList;
+import org.hibernate.engine.SessionImplementor;
+
+public class PersistentMyList extends PersistentList implements IMyList {
+
+ public PersistentMyList(SessionImplementor session) {
+ super(session);
+ }
+
+ public PersistentMyList(SessionImplementor session, IMyList list) {
+ super(session, list);
+ }
+
+
+
+}
Property changes on: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/PersistentMyList.java
___________________________________________________________________
Name: svn:eol-style
+ native
Copied: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/User.java (from rev 11075, trunk/Hibernate3/test/org/hibernate/test/usercollection/User.java)
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/User.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/User.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,39 @@
+//$Id$
+package org.hibernate.test.usercollection.basic;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author Gavin King
+ */
+public class User {
+ private String userName;
+ private IMyList emailAddresses = new MyList();
+ private Map sessionData = new HashMap();
+
+ User() {}
+ public User(String name) {
+ userName = name;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+ public void setUserName(String userName) {
+ this.userName = userName;
+ }
+ public List getEmailAddresses() {
+ return emailAddresses;
+ }
+ public void setEmailAddresses(IMyList emailAddresses) {
+ this.emailAddresses = emailAddresses;
+ }
+ public Map getSessionData() {
+ return sessionData;
+ }
+ public void setSessionData(Map sessionData) {
+ this.sessionData = sessionData;
+ }
+}
Property changes on: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/User.java
___________________________________________________________________
Name: svn:eol-style
+ native
Copied: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/UserCollectionTypeTest.java (from rev 11075, trunk/Hibernate3/test/org/hibernate/test/usercollection/UserCollectionTypeTest.java)
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/UserCollectionTypeTest.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/UserCollectionTypeTest.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,63 @@
+//$Id $
+package org.hibernate.test.usercollection.basic;
+
+import junit.framework.Test;
+
+import org.hibernate.Hibernate;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.junit.functional.FunctionalTestCase;
+import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
+
+/**
+ * @author Max Rydahl Andersen
+ */
+public class UserCollectionTypeTest extends FunctionalTestCase {
+
+ public UserCollectionTypeTest(String str) {
+ super(str);
+ }
+
+ public String[] getMappings() {
+ return new String[] { "usercollection/basic/UserPermissions.hbm.xml" };
+ }
+
+ public static Test suite() {
+ return new FunctionalTestClassTestSuite( UserCollectionTypeTest.class );
+ }
+
+ public void testBasicOperation() {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ User u = new User("max");
+ u.getEmailAddresses().add( new Email("max at hibernate.org") );
+ u.getEmailAddresses().add( new Email("max.andersen at jboss.com") );
+ s.persist(u);
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ User u2 = (User) s.createCriteria(User.class).uniqueResult();
+ assertTrue( Hibernate.isInitialized( u2.getEmailAddresses() ) );
+ assertEquals( u2.getEmailAddresses().size(), 2 );
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ u2 = ( User ) s.get( User.class, u.getUserName() );
+ u2.getEmailAddresses().size();
+ assertEquals( 2, MyListType.lastInstantiationRequest );
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ s.delete( u );
+ t.commit();
+ s.close();
+ }
+
+}
+
Property changes on: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/UserCollectionTypeTest.java
___________________________________________________________________
Name: svn:eol-style
+ native
Copied: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/UserPermissions.hbm.xml (from rev 11075, trunk/Hibernate3/test/org/hibernate/test/usercollection/UserPermissions.hbm.xml)
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/UserPermissions.hbm.xml (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/UserPermissions.hbm.xml 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,30 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+ "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<!--
+ This mapping is a basic example of how to write a UserCollectionType.
+-->
+
+<hibernate-mapping package="org.hibernate.test.usercollection.basic">
+
+ <import class="Permission"/>
+
+ <class name="User" table="UC_BSC_USER">
+ <id name="userName"/>
+ <list name="emailAddresses" fetch="join" cascade="all, delete-orphan" collection-type="org.hibernate.test.usercollection.basic.MyListType">
+ <key column="userName"/>
+ <list-index column="displayOrder" base="1"/>
+ <one-to-many class="Email"/>
+ </list>
+ </class>
+
+ <class name="Email">
+ <id name="id">
+ <generator class="native"/>
+ </id>
+ <property name="address"/>
+ </class>
+
+</hibernate-mapping>
Property changes on: trunk/Hibernate3/test/org/hibernate/test/usercollection/basic/UserPermissions.hbm.xml
___________________________________________________________________
Name: svn:eol-style
+ native
Added: trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableList.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableList.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableList.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,13 @@
+package org.hibernate.test.usercollection.parameterized;
+
+import java.util.List;
+
+/**
+ * Our specialized collection contract
+ *
+ * @author Holger Brands
+ * @author Steve Ebersole
+ */
+public interface DefaultableList extends List {
+ public String getDefaultValue();
+}
Added: trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableListImpl.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableListImpl.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableListImpl.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,28 @@
+package org.hibernate.test.usercollection.parameterized;
+
+import java.util.ArrayList;
+
+/**
+ * Implementation of our specialized collection contract
+ *
+ * @author Holger Brands
+ * @author Steve Ebersole
+ */
+public class DefaultableListImpl extends ArrayList implements DefaultableList {
+ private String defaultValue;
+
+ public DefaultableListImpl() {
+ }
+
+ public DefaultableListImpl(int anticipatedSize) {
+ super( anticipatedSize + ( int ) Math.ceil( anticipatedSize * .75f ) );
+ }
+
+ public String getDefaultValue() {
+ return defaultValue;
+ }
+
+ public void setDefaultValue(String defaultValue) {
+ this.defaultValue = defaultValue;
+ }
+}
Added: trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableListType.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableListType.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/DefaultableListType.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,75 @@
+package org.hibernate.test.usercollection.parameterized;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+import java.util.List;
+
+import org.hibernate.usertype.UserCollectionType;
+import org.hibernate.usertype.ParameterizedType;
+import org.hibernate.collection.PersistentCollection;
+import org.hibernate.engine.SessionImplementor;
+import org.hibernate.persister.collection.CollectionPersister;
+import org.hibernate.EntityMode;
+
+/**
+ * Our Hibernate type-system extension for defining our specialized collection
+ * contract.
+ *
+ * @author Holger Brands
+ * @author Steve Ebersole
+ */
+public class DefaultableListType implements UserCollectionType, ParameterizedType {
+ private String defaultValue;
+
+ public Object instantiate(int anticipatedSize) {
+ DefaultableListImpl list = anticipatedSize < 0 ? new DefaultableListImpl() : new DefaultableListImpl( anticipatedSize );
+ list.setDefaultValue( defaultValue );
+ return list;
+ }
+
+ public PersistentCollection instantiate(
+ SessionImplementor session,
+ CollectionPersister persister) {
+ return new PersistentDefaultableList( session );
+ }
+
+ public PersistentCollection wrap(SessionImplementor session, Object collection) {
+ if ( session.getEntityMode() == EntityMode.DOM4J ) {
+ throw new IllegalStateException( "dom4j not supported" );
+ }
+ else {
+ return new PersistentDefaultableList( session, ( List ) collection );
+ }
+ }
+
+ public Iterator getElementsIterator(Object collection) {
+ return ( ( DefaultableList ) collection ).iterator();
+ }
+
+ public boolean contains(Object collection, Object entity) {
+ return ( ( DefaultableList ) collection ).contains( entity );
+ }
+
+ public Object indexOf(Object collection, Object entity) {
+ int index = ( ( DefaultableList ) collection ).indexOf( entity );
+ return index >= 0 ? new Integer( index ) : null;
+ }
+
+ public Object replaceElements(
+ Object original,
+ Object target,
+ CollectionPersister persister,
+ Object owner,
+ Map copyCache,
+ SessionImplementor session) {
+ DefaultableList result = ( DefaultableList ) target;
+ result.clear();
+ result.addAll( ( DefaultableList ) original );
+ return result;
+ }
+
+ public void setParameterValues(Properties parameters) {
+ defaultValue = parameters.getProperty( "default" );
+ }
+}
Added: trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Entity.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Entity.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Entity.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,33 @@
+package org.hibernate.test.usercollection.parameterized;
+
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ * Our test entity
+ *
+ * @author Steve Ebersole
+ */
+public class Entity {
+ private String name;
+ private List values = new ArrayList();
+
+ public Entity() {
+ }
+
+ public Entity(String name) {
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public List getValues() {
+ return values;
+ }
+
+ public void setValues(List values) {
+ this.values = values;
+ }
+}
Added: trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/Mapping.hbm.xml 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,21 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+ "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<hibernate-mapping package="org.hibernate.test.usercollection.parameterized" default-access="field">
+
+ <typedef name="DefaultableList" class="org.hibernate.test.usercollection.parameterized.DefaultableListType">
+ <param name="default">Hello</param>
+ </typedef>
+
+ <class name="Entity">
+ <id name="name" type="string"/>
+ <list name="values" fetch="join" table="ENT_VAL" collection-type="DefaultableList">
+ <key column="ENT_ID"/>
+ <list-index column="POSITION"/>
+ <element type="string" column="VAL"/>
+ </list>
+ </class>
+
+</hibernate-mapping>
\ No newline at end of file
Added: trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/ParameterizedUserCollectionTypeTest.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/ParameterizedUserCollectionTypeTest.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/ParameterizedUserCollectionTypeTest.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,49 @@
+package org.hibernate.test.usercollection.parameterized;
+
+import junit.framework.Test;
+
+import org.hibernate.junit.functional.FunctionalTestCase;
+import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.Hibernate;
+
+/**
+ * Tes for parameterized user collection types.
+ *
+ * @author Holger Brands
+ * @author Steve Ebersole
+ */
+public class ParameterizedUserCollectionTypeTest extends FunctionalTestCase {
+ public ParameterizedUserCollectionTypeTest(String string) {
+ super( string );
+ }
+
+ public static Test suite() {
+ return new FunctionalTestClassTestSuite( ParameterizedUserCollectionTypeTest.class );
+ }
+
+ public String[] getMappings() {
+ return new String[] { "usercollection/parameterized/Mapping.hbm.xml" };
+ }
+
+ public void testBasicOperation() {
+ Session s = openSession();
+ Transaction t = s.beginTransaction();
+ Entity entity = new Entity( "tester" );
+ entity.getValues().add( "value-1" );
+ s.persist( entity );
+ t.commit();
+ s.close();
+
+ s = openSession();
+ t = s.beginTransaction();
+ entity = ( Entity ) s.get( Entity.class, "tester" );
+ assertTrue( Hibernate.isInitialized( entity.getValues() ) );
+ assertEquals( 1, entity.getValues().size() );
+ assertEquals( "Hello", ( ( DefaultableList ) entity.getValues() ).getDefaultValue() );
+ s.delete( entity );
+ t.commit();
+ s.close();
+ }
+}
Added: trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/PersistentDefaultableList.java
===================================================================
--- trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/PersistentDefaultableList.java (rev 0)
+++ trunk/Hibernate3/test/org/hibernate/test/usercollection/parameterized/PersistentDefaultableList.java 2007-05-09 03:54:06 UTC (rev 11496)
@@ -0,0 +1,29 @@
+package org.hibernate.test.usercollection.parameterized;
+
+import java.util.List;
+
+import org.hibernate.collection.PersistentList;
+import org.hibernate.engine.SessionImplementor;
+
+/**
+ * The "persistent wrapper" around our specialized collection contract
+ *
+ * @author Holger Brands
+ * @author Steve Ebersole
+ */
+public class PersistentDefaultableList extends PersistentList implements DefaultableList {
+ public PersistentDefaultableList(SessionImplementor session) {
+ super( session );
+ }
+
+ public PersistentDefaultableList(SessionImplementor session, List list) {
+ super( session, list );
+ }
+
+ public PersistentDefaultableList() {
+ }
+
+ public String getDefaultValue() {
+ return ( ( DefaultableList ) this.list ).getDefaultValue();
+ }
+}
More information about the hibernate-commits
mailing list