[hibernate-commits] Hibernate SVN: r11650 - in trunk/HibernateExt/annotations/src: test/org/hibernate/test/annotations/cid and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Thu Jun 7 12:44:52 EDT 2007


Author: epbernard
Date: 2007-06-07 12:44:52 -0400 (Thu, 07 Jun 2007)
New Revision: 11650

Added:
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/FkSecondPass.java
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/JoinedSubclassFkSecondPass.java
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/ToOneFkSecondPass.java
Removed:
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/FkSecondPass.java
Modified:
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationConfiguration.java
   trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/OneToOneSecondPass.java
   trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/cid/CompositeIdTest.java
Log:
ANN-621 handle JoinedSubclass FK/PK in a second pass

Modified: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java	2007-06-07 14:34:51 UTC (rev 11649)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java	2007-06-07 16:44:52 UTC (rev 11650)
@@ -601,8 +601,9 @@
 			else {
 				key.setCascadeDeleteEnabled( false );
 			}
-			TableBinder.bindFk( jsc.getSuperclass(), jsc, inheritanceJoinedColumns, key, false, mappings );
-			//no need to handle inSecondPass this is an Etntiy related work
+			//we are never in a second pass at that stage, so queue it
+			SecondPass sp = new JoinedSubclassFkSecondPass(jsc, inheritanceJoinedColumns, key, mappings);
+			mappings.addSecondPass( sp );
 			mappings.addSecondPass( new CreateKeySecondPass( jsc ) );
 
 		}
@@ -1858,7 +1859,7 @@
 		if ( !BinderHelper.isDefault( fkName ) ) value.setForeignKeyName( fkName );
 
 		String path = propertyHolder.getPath() + "." + propertyName;
-		FkSecondPass secondPass = new FkSecondPass(
+		FkSecondPass secondPass = new ToOneFkSecondPass(
 				value, columns,
 				!optional && unique, //cannot have nullabe and unique on certain DBs like Derby
 				propertyHolder.getEntityOwnerClassName(),

Modified: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationConfiguration.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationConfiguration.java	2007-06-07 14:34:51 UTC (rev 11649)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationConfiguration.java	2007-06-07 16:44:52 UTC (rev 11650)
@@ -302,17 +302,6 @@
 				iter.remove();
 			}
 		}
-
-		//process OneToManySecondPass in order: first
-		iter = secondPasses.iterator();
-		while ( iter.hasNext() ) {
-			SecondPass sp = (SecondPass) iter.next();
-
-			if ( sp instanceof CreateKeySecondPass ) {
-				sp.doSecondPass( classes );
-				iter.remove();
-			}
-		}
 		super.secondPassCompile();
 		inSecondPass = false;
 		Iterator tables = (Iterator<Map.Entry<Table, List<String[]>>>) tableUniqueConstraints.entrySet().iterator();
@@ -372,21 +361,16 @@
 	}
 
 	private void processFkSecondPassInOrder() {
-		log.debug( "processing manytoone fk mappings" );
+		log.debug( "processing fk mappings (*ToOne and JoinedSubclass)" );
 		Iterator iter = secondPasses.iterator();
 		/* We need to process FKSecond pass trying to resolve any
 		 * graph circularity (ie PK made of a many to one linking to
 		 * an entity having a PK made of a ManyToOne ...
 		 */
 		SortedSet<FkSecondPass> fkSecondPasses = new TreeSet<FkSecondPass>(
-				new Comparator() {
+				new Comparator<FkSecondPass>() {
 					//The comparator implementation has to respect the compare=0 => equals() = true for sets
-					public int compare(Object o1, Object o2) {
-						if (! (o1 instanceof FkSecondPass &&  o2 instanceof FkSecondPass) ) {
-							throw new AssertionFailure("comparint FkSecondPass with non FkSecondPass");
-						}
-						FkSecondPass f1 = (FkSecondPass) o1;
-						FkSecondPass f2 = (FkSecondPass) o2;
+					public int compare(FkSecondPass f1, FkSecondPass f2) {
 						int compare = f1.getValue().getTable().getQuotedName().compareTo(
 								f2.getValue().getTable().getQuotedName()
 						);
@@ -422,7 +406,7 @@
 			Iterator it = fkSecondPasses.iterator();
 			while ( it.hasNext() ) {
 				FkSecondPass sp = (FkSecondPass) it.next();
-				String referenceEntityName = sp.getValue().getReferencedEntityName();
+				String referenceEntityName = sp.getReferencedEntityName();
 				PersistentClass classMapping = getClassMapping( referenceEntityName );
 				if ( sp.isInPrimaryKey() ) {
 					String dependentTable = classMapping.getTable().getQuotedName();

Deleted: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/FkSecondPass.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/FkSecondPass.java	2007-06-07 14:34:51 UTC (rev 11649)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/FkSecondPass.java	2007-06-07 16:44:52 UTC (rev 11650)
@@ -1,77 +0,0 @@
-// $Id$
-package org.hibernate.cfg;
-
-import org.hibernate.AnnotationException;
-import org.hibernate.AssertionFailure;
-import org.hibernate.MappingException;
-import org.hibernate.util.StringHelper;
-import org.hibernate.cfg.annotations.TableBinder;
-import org.hibernate.mapping.ManyToOne;
-import org.hibernate.mapping.OneToOne;
-import org.hibernate.mapping.PersistentClass;
-import org.hibernate.mapping.ToOne;
-import org.hibernate.mapping.Property;
-
-/**
- * Enable a proper set of the FK columns in respect with the id column order
- * Allow the correct implementation of the default EJB3 values which needs both
- * sides of the association to be resolved
- *
- * @author Emmanuel Bernard
- */
-public class FkSecondPass implements SecondPass {
-	private ToOne value;
-	private Ejb3JoinColumn[] columns;
-	private boolean unique;
-	private ExtendedMappings mappings;
-	private String path;
-	private String entityClassName;
-
-	FkSecondPass(
-			ToOne value, Ejb3JoinColumn[] columns, boolean unique, String entityClassName, String path, ExtendedMappings mappings
-	) {
-		this.mappings = mappings;
-		this.value = value;
-		this.columns = columns;
-		this.unique = unique;
-		this.entityClassName = entityClassName;
-		this.path = entityClassName != null ? path.substring( entityClassName.length() + 1 ) : path;
-	}
-
-	public ToOne getValue() {
-		return value;
-	}
-
-	public boolean isInPrimaryKey() {
-		if (entityClassName == null) return false;
-		Property property = mappings.getClass( entityClassName ).getIdentifierProperty();
-		return property != null && path != null && path.startsWith( property.getName() );
-	}
-
-	public void doSecondPass(java.util.Map persistentClasses) throws MappingException {
-		if ( value instanceof ManyToOne ) {
-			ManyToOne manyToOne = (ManyToOne) value;
-			PersistentClass ref = (PersistentClass) persistentClasses.get( manyToOne.getReferencedEntityName() );
-			if ( ref == null ) {
-				throw new AnnotationException(
-						"@OneToOne or @ManyToOne on "
-								+ StringHelper.qualify(entityClassName, path)
-								+ " references an unknown entity: "
-								+ manyToOne.getReferencedEntityName()
-				);
-			}
-			BinderHelper.createSyntheticPropertyReference( columns, ref, null, manyToOne, false, mappings );
-			TableBinder.bindFk( ref, null, columns, manyToOne, unique, mappings );
-			/*
-			 * HbmBinder does this only when property-ref != null, but IMO, it makes sense event if it is null
-			 */
-			if ( ! manyToOne.isIgnoreNotFound() ) manyToOne.createPropertyRefConstraints( persistentClasses );
-		}
-		else if ( value instanceof OneToOne ) {
-			( (OneToOne) value ).createForeignKey();
-		}
-		else {
-			throw new AssertionFailure( "FkSecondPass for a wrong value type: " + value.getClass().getName() );
-		}
-	}
-}

Added: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/FkSecondPass.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/FkSecondPass.java	                        (rev 0)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/FkSecondPass.java	2007-06-07 16:44:52 UTC (rev 11650)
@@ -0,0 +1,27 @@
+//$Id$
+package org.hibernate.cfg;
+
+import org.hibernate.mapping.Value;
+import org.hibernate.mapping.ToOne;
+import org.hibernate.mapping.SimpleValue;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public abstract class FkSecondPass implements SecondPass {
+	protected SimpleValue value;
+	protected Ejb3JoinColumn[] columns;
+
+	public FkSecondPass(SimpleValue value, Ejb3JoinColumn[] columns) {
+		this.value = value;
+		this.columns = columns;
+	}
+
+	public Value getValue() {
+		return value;
+	}
+
+	public abstract String getReferencedEntityName();
+
+	public abstract boolean isInPrimaryKey();
+}

Added: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/JoinedSubclassFkSecondPass.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/JoinedSubclassFkSecondPass.java	                        (rev 0)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/JoinedSubclassFkSecondPass.java	2007-06-07 16:44:52 UTC (rev 11650)
@@ -0,0 +1,37 @@
+//$Id$
+package org.hibernate.cfg;
+
+import java.util.Map;
+
+import org.hibernate.MappingException;
+import org.hibernate.cfg.annotations.TableBinder;
+import org.hibernate.mapping.Value;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.JoinedSubclass;
+import org.hibernate.mapping.SimpleValue;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class JoinedSubclassFkSecondPass extends FkSecondPass {
+	private JoinedSubclass entity;
+	private ExtendedMappings mappings;
+
+	public JoinedSubclassFkSecondPass(JoinedSubclass entity, Ejb3JoinColumn[] inheritanceJoinedColumns, SimpleValue key, ExtendedMappings mappings) {
+		super(key, inheritanceJoinedColumns);
+		this.entity = entity;
+		this.mappings = mappings;
+	}
+	
+	public String getReferencedEntityName() {
+		return entity.getSuperclass().getEntityName();
+	}
+
+	public boolean isInPrimaryKey() {
+		return true;
+	}
+
+	public void doSecondPass(Map persistentClasses) throws MappingException {
+		TableBinder.bindFk( entity.getSuperclass(), entity, columns, value, false, mappings );
+	}
+}

Modified: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/OneToOneSecondPass.java
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/OneToOneSecondPass.java	2007-06-07 14:34:51 UTC (rev 11649)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/OneToOneSecondPass.java	2007-06-07 16:44:52 UTC (rev 11650)
@@ -102,7 +102,7 @@
 
 			if (rightOrder) {
 				String path = StringHelper.qualify( propertyHolder.getPath(), propertyName );
-				( new FkSecondPass(
+				( new ToOneFkSecondPass(
 						value, joinColumns,
 						!optional, //cannot have nullabe and unique on certain DBs
 						propertyHolder.getEntityOwnerClassName(),

Copied: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/ToOneFkSecondPass.java (from rev 11648, trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/FkSecondPass.java)
===================================================================
--- trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/ToOneFkSecondPass.java	                        (rev 0)
+++ trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/ToOneFkSecondPass.java	2007-06-07 16:44:52 UTC (rev 11650)
@@ -0,0 +1,75 @@
+// $Id$
+package org.hibernate.cfg;
+
+import org.hibernate.AnnotationException;
+import org.hibernate.AssertionFailure;
+import org.hibernate.MappingException;
+import org.hibernate.util.StringHelper;
+import org.hibernate.cfg.annotations.TableBinder;
+import org.hibernate.mapping.ManyToOne;
+import org.hibernate.mapping.OneToOne;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.ToOne;
+import org.hibernate.mapping.Property;
+import org.hibernate.mapping.Value;
+
+/**
+ * Enable a proper set of the FK columns in respect with the id column order
+ * Allow the correct implementation of the default EJB3 values which needs both
+ * sides of the association to be resolved
+ *
+ * @author Emmanuel Bernard
+ */
+public class ToOneFkSecondPass extends FkSecondPass {
+	private boolean unique;
+	private ExtendedMappings mappings;
+	private String path;
+	private String entityClassName;
+
+	ToOneFkSecondPass(
+			ToOne value, Ejb3JoinColumn[] columns, boolean unique, String entityClassName, String path, ExtendedMappings mappings
+	) {
+		super( value, columns );
+		this.mappings = mappings;
+		this.unique = unique;
+		this.entityClassName = entityClassName;
+		this.path = entityClassName != null ? path.substring( entityClassName.length() + 1 ) : path;
+	}
+
+	public String getReferencedEntityName() {
+		return ((ToOne) value).getReferencedEntityName();
+	}
+
+	public boolean isInPrimaryKey() {
+		if (entityClassName == null) return false;
+		Property property = mappings.getClass( entityClassName ).getIdentifierProperty();
+		return property != null && path != null && path.startsWith( property.getName() );
+	}
+
+	public void doSecondPass(java.util.Map persistentClasses) throws MappingException {
+		if ( value instanceof ManyToOne ) {
+			ManyToOne manyToOne = (ManyToOne) value;
+			PersistentClass ref = (PersistentClass) persistentClasses.get( manyToOne.getReferencedEntityName() );
+			if ( ref == null ) {
+				throw new AnnotationException(
+						"@OneToOne or @ManyToOne on "
+								+ StringHelper.qualify(entityClassName, path)
+								+ " references an unknown entity: "
+								+ manyToOne.getReferencedEntityName()
+				);
+			}
+			BinderHelper.createSyntheticPropertyReference( columns, ref, null, manyToOne, false, mappings );
+			TableBinder.bindFk( ref, null, columns, manyToOne, unique, mappings );
+			/*
+			 * HbmBinder does this only when property-ref != null, but IMO, it makes sense event if it is null
+			 */
+			if ( ! manyToOne.isIgnoreNotFound() ) manyToOne.createPropertyRefConstraints( persistentClasses );
+		}
+		else if ( value instanceof OneToOne ) {
+			( (OneToOne) value ).createForeignKey();
+		}
+		else {
+			throw new AssertionFailure( "FkSecondPass for a wrong value type: " + value.getClass().getName() );
+		}
+	}
+}


Property changes on: trunk/HibernateExt/annotations/src/java/org/hibernate/cfg/ToOneFkSecondPass.java
___________________________________________________________________
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Modified: trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/cid/CompositeIdTest.java
===================================================================
--- trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/cid/CompositeIdTest.java	2007-06-07 14:34:51 UTC (rev 11649)
+++ trunk/HibernateExt/annotations/src/test/org/hibernate/test/annotations/cid/CompositeIdTest.java	2007-06-07 16:44:52 UTC (rev 11650)
@@ -8,10 +8,6 @@
 import org.hibernate.Session;
 import org.hibernate.Transaction;
 import org.hibernate.test.annotations.TestCase;
-import org.hibernate.test.annotations.cid.OrderLine;
-import org.hibernate.test.annotations.cid.Order;
-import org.hibernate.test.annotations.cid.Product;
-import org.hibernate.test.annotations.cid.OrderLinePk;
 
 /**
  * test some composite id functionalities
@@ -24,45 +20,47 @@
 	}
 
 	/**
-		 * This feature is not supported by the EJB3
-		 * this is an hibernate extension
-		 */
-		public void testManyToOneInCompositePk() throws Exception {
-			Session s;
-			Transaction tx;
-			s = openSession();
-			tx = s.beginTransaction();
-			ParentPk ppk = new ParentPk();
-			ppk.setFirstName( "Emmanuel" );
-			ppk.setLastName( "Bernard" );
-			Parent p = new Parent();
-			p.id = ppk;
-			s.persist( p );
-			ChildPk cpk = new ChildPk();
-			cpk.parent = p;
-			cpk.nthChild = 1;
-			Child c = new Child();
-			c.id = cpk;
-			s.persist( c );
-			tx.commit();
-			s.close();
-	
-			s = openSession();
-			tx = s.beginTransaction();
-			Query q = s.createQuery( "select c from Child c where c.id.nthChild = :nth" );
-			q.setInteger( "nth", 1 );
-			List results = q.list();
-			assertEquals( 1, results.size() );
-			c = (Child) results.get( 0 );
-			assertNotNull( c );
-			assertNotNull( c.id.parent );
-			//FIXME mke it work in unambigious cases
-	//		assertNotNull(c.id.parent.id);
-	//		assertEquals(p.id.getFirstName(), c.id.parent.id.getFirstName());
-			tx.commit();
-			s.close();
-		}
+	 * This feature is not supported by the EJB3
+	 * this is an hibernate extension
+	 */
+	public void testManyToOneInCompositePk() throws Exception {
+		Session s;
+		Transaction tx;
+		s = openSession();
+		tx = s.beginTransaction();
+		ParentPk ppk = new ParentPk();
+		ppk.setFirstName( "Emmanuel" );
+		ppk.setLastName( "Bernard" );
+		Parent p = new Parent();
+		p.id = ppk;
+		s.persist( p );
+		ChildPk cpk = new ChildPk();
+		cpk.parent = p;
+		cpk.nthChild = 1;
+		Child c = new Child();
+		c.id = cpk;
+		s.persist( c );
+		tx.commit();
+		s.close();
 
+		s = openSession();
+		tx = s.beginTransaction();
+		Query q = s.createQuery( "select c from Child c where c.id.nthChild = :nth" );
+		q.setInteger( "nth", 1 );
+		List results = q.list();
+		assertEquals( 1, results.size() );
+		c = (Child) results.get( 0 );
+		assertNotNull( c );
+		assertNotNull( c.id.parent );
+		//FIXME mke it work in unambigious cases
+		//		assertNotNull(c.id.parent.id);
+		//		assertEquals(p.id.getFirstName(), c.id.parent.id.getFirstName());
+		s.delete( c );
+		s.delete( c.id.parent );
+		tx.commit();
+		s.close();
+	}
+
 	/**
 	 * This feature is not supported by the EJB3
 	 * this is an hibernate extension
@@ -100,6 +98,8 @@
 		//FIXME mke it work in unambigious cases
 //		assertNotNull(c.id.parent.id);
 //		assertEquals(p.id.getFirstName(), c.id.parent.id.getFirstName());
+		s.delete( c );
+		s.delete( c.id.parent );
 		tx.commit();
 		s.close();
 	}
@@ -150,7 +150,7 @@
 		OrderLine orderLine = new OrderLine();
 		orderLine.order = order;
 		orderLine.product = product;
-		s.persist(orderLine);
+		s.persist( orderLine );
 		s.flush();
 		s.clear();
 
@@ -159,23 +159,23 @@
 		assertEquals( order.id, orderLine.order.id );
 		assertNotNull( orderLine.product );
 		assertEquals( product.name, orderLine.product.name );
-		
+
 		tx.rollback();
 		s.close();
 	}
 
 	protected Class[] getMappings() {
-		return new Class[]{
+		return new Class[] {
 				Parent.class,
 				Child.class,
 				Channel.class,
 				TvMagazin.class,
 				Presenter.class,
-                Order.class,
-                Product.class,
-                OrderLine.class,
-                OrderLinePk.class,
-                LittleGenius.class
-        };
+				Order.class,
+				Product.class,
+				OrderLine.class,
+				OrderLinePk.class,
+				LittleGenius.class
+		};
 	}
 }




More information about the hibernate-commits mailing list