[hibernate-commits] Hibernate SVN: r21044 - in annotations/branches/v3_4_0_GA_CP/src: main/java/org/hibernate/cfg/annotations and 4 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Jul 25 03:13:17 EDT 2012


Author: stliu
Date: 2012-07-25 03:13:17 -0400 (Wed, 25 Jul 2012)
New Revision: 21044

Added:
   annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/
   annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWork.java
   annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkPk.java
   annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkTest.java
   annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkWithHbmTest.java
   annotations/branches/v3_4_0_GA_CP/src/test/resources/org/hibernate/test/annotations/propertyref/
   annotations/branches/v3_4_0_GA_CP/src/test/resources/org/hibernate/test/annotations/propertyref/Mapping.hbm.xml
   annotations/branches/v3_4_0_GA_CP/src/test/resources/org/hibernate/test/annotations/propertyref/import.sql
Modified:
   annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/BinderHelper.java
   annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java
   annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/annotations/TableBinder.java
Log:
JBPAPP-6476 HHH-6204 - JoinColumn on non key field fails to populate collection

Modified: annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/BinderHelper.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/BinderHelper.java	2012-07-25 07:11:30 UTC (rev 21043)
+++ annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/BinderHelper.java	2012-07-25 07:13:17 UTC (rev 21044)
@@ -90,13 +90,129 @@
 		return clone;
 	}
 
+//	public static void createSyntheticPropertyReference(
+//			Ejb3JoinColumn[] columns,
+//			PersistentClass ownerEntity,
+//			PersistentClass associatedEntity,
+//			Value value,
+//			boolean inverse, ExtendedMappings mappings
+//	) {
+//		//associated entity only used for more precise exception, yuk!
+//		if ( columns[0].isImplicit() || StringHelper.isNotEmpty( columns[0].getMappedBy() ) ) return;
+//		int fkEnum = Ejb3JoinColumn.checkReferencedColumnsType( columns, ownerEntity, mappings );
+//		PersistentClass associatedClass = columns[0].getPropertyHolder() != null ?
+//				columns[0].getPropertyHolder().getPersistentClass() :
+//				null;
+//		if ( Ejb3JoinColumn.NON_PK_REFERENCE == fkEnum ) {
+//			/**
+//			 * Create a synthetic property to refer to including an
+//			 * embedded component value containing all the properties
+//			 * mapped to the referenced columns
+//			 * We need to shallow copy those properties to mark them
+//			 * as non insertable / non updatable
+//			 */
+//			StringBuilder propertyNameBuffer = new StringBuilder( "_" );
+//			propertyNameBuffer.append( associatedClass.getEntityName().replace( '.', '_' ) );
+//			propertyNameBuffer.append( "_" ).append( columns[0].getPropertyName() );
+//			String syntheticPropertyName = propertyNameBuffer.toString();
+//			//find properties associated to a certain column
+//			Object columnOwner = findColumnOwner( ownerEntity, columns[0].getReferencedColumn(), mappings );
+//			List<Property> properties = findPropertiesByColumns( columnOwner, columns, mappings );
+//			//create an embeddable component
+//			Property synthProp = null;
+//			if ( properties != null ) {
+//				//todo how about properties.size() == 1, this should be much simpler
+//				Component embeddedComp = columnOwner instanceof PersistentClass ?
+//						new Component( (PersistentClass) columnOwner ) :
+//						new Component( (Join) columnOwner );
+//				embeddedComp.setEmbedded( true );
+//				embeddedComp.setNodeName( syntheticPropertyName );
+//				embeddedComp.setComponentClassName( embeddedComp.getOwner().getClassName() );
+//				for (Property property : properties) {
+//					Property clone = BinderHelper.shallowCopy( property );
+//					clone.setInsertable( false );
+//					clone.setUpdateable( false );
+//					clone.setNaturalIdentifier( false );
+//					embeddedComp.addProperty( clone );
+//				}
+//				synthProp = new Property();
+//				synthProp.setName( syntheticPropertyName );
+//				synthProp.setNodeName( syntheticPropertyName );
+//				synthProp.setPersistentClass( ownerEntity );
+//				synthProp.setUpdateable( false );
+//				synthProp.setInsertable( false );
+//				synthProp.setValue( embeddedComp );
+//				synthProp.setPropertyAccessorName( "embedded" );
+//				ownerEntity.addProperty( synthProp );
+//				//make it unique
+//				TableBinder.createUniqueConstraint( embeddedComp );
+//			}
+//			else {
+//				//TODO use a ToOne type doing a second select
+//				StringBuilder columnsList = new StringBuilder();
+//				columnsList.append( "referencedColumnNames(" );
+//				for (Ejb3JoinColumn column : columns) {
+//					columnsList.append( column.getReferencedColumn() ).append( ", " );
+//				}
+//				columnsList.setLength( columnsList.length() - 2 );
+//				columnsList.append( ") " );
+//
+//				if ( associatedEntity != null ) {
+//					//overidden destination
+//					columnsList.append( "of " )
+//							.append( associatedEntity.getEntityName() )
+//							.append( "." )
+//							.append( columns[0].getPropertyName() )
+//							.append( " " );
+//				}
+//				else {
+//					if ( columns[0].getPropertyHolder() != null ) {
+//						columnsList.append( "of " )
+//								.append( columns[0].getPropertyHolder().getEntityName() )
+//								.append( "." )
+//								.append( columns[0].getPropertyName() )
+//								.append( " " );
+//					}
+//				}
+//				columnsList.append( "referencing " )
+//						.append( ownerEntity.getEntityName() )
+//						.append( " not mapped to a single property" );
+//				throw new AnnotationException( columnsList.toString() );
+//			}
+//
+//			/**
+//			 * creating the property ref to the new synthetic property
+//			 */
+//			if ( value instanceof ToOne ) {
+//				( (ToOne) value ).setReferencedPropertyName( syntheticPropertyName );
+//				mappings.addUniquePropertyReference( ownerEntity.getEntityName(), syntheticPropertyName );
+//			}
+//			else if ( value instanceof Collection ) {
+//				( (Collection) value ).setReferencedPropertyName( syntheticPropertyName );
+//				//not unique because we could create a mtm wo association table
+//				mappings.addPropertyReference( ownerEntity.getEntityName(), syntheticPropertyName );
+//			}
+//			else {
+//				throw new AssertionFailure(
+//						"Do a property ref on an unexpected Value type: "
+//								+ value.getClass().getName()
+//				);
+//			}
+//			mappings.addPropertyReferencedAssociation(
+//					( inverse ? "inverse__" : "" ) + associatedClass.getEntityName(),
+//					columns[0].getPropertyName(),
+//					syntheticPropertyName
+//			);
+//		}
+//	}
+
 	public static void createSyntheticPropertyReference(
 			Ejb3JoinColumn[] columns,
 			PersistentClass ownerEntity,
 			PersistentClass associatedEntity,
 			Value value,
-			boolean inverse, ExtendedMappings mappings
-	) {
+			boolean inverse,
+			ExtendedMappings mappings) {
 		//associated entity only used for more precise exception, yuk!
 		if ( columns[0].isImplicit() || StringHelper.isNotEmpty( columns[0].getMappedBy() ) ) return;
 		int fkEnum = Ejb3JoinColumn.checkReferencedColumnsType( columns, ownerEntity, mappings );
@@ -133,6 +249,7 @@
 					clone.setInsertable( false );
 					clone.setUpdateable( false );
 					clone.setNaturalIdentifier( false );
+					clone.setGeneration( property.getGeneration() );
 					embeddedComp.addProperty( clone );
 				}
 				synthProp = new Property();
@@ -207,6 +324,7 @@
 	}
 
 
+
 	private static List<Property> findPropertiesByColumns(
 			Object columnOwner, Ejb3JoinColumn[] columns,
 			ExtendedMappings mappings

Modified: annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java	2012-07-25 07:11:30 UTC (rev 21043)
+++ annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/annotations/CollectionBinder.java	2012-07-25 07:13:17 UTC (rev 21044)
@@ -1027,7 +1027,7 @@
 		}
 		else {
 			keyVal = (KeyValue) collValue.getOwner()
-					.getRecursiveProperty( propRef )
+					.getReferencedProperty( propRef )
 					.getValue();
 		}
 		DependantValue key = new DependantValue( collValue.getCollectionTable(), keyVal );

Modified: annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/annotations/TableBinder.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/annotations/TableBinder.java	2012-07-25 07:11:30 UTC (rev 21043)
+++ annotations/branches/v3_4_0_GA_CP/src/main/java/org/hibernate/cfg/annotations/TableBinder.java	2012-07-25 07:13:17 UTC (rev 21044)
@@ -256,7 +256,7 @@
 							"No property ref found while expected"
 					);
 				}
-				Property synthProp = referencedEntity.getRecursiveProperty( referencedPropertyName );
+				Property synthProp = referencedEntity.getReferencedProperty( referencedPropertyName );
 				if ( synthProp == null ) {
 					throw new AssertionFailure(
 							"Cannot find synthProp: " + referencedEntity.getEntityName() + "." + referencedPropertyName

Added: annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWork.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWork.java	                        (rev 0)
+++ annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWork.java	2012-07-25 07:13:17 UTC (rev 21044)
@@ -0,0 +1,103 @@
+package org.hibernate.test.annotations.propertyref;
+
+
+import javax.persistence.Column;
+import javax.persistence.EmbeddedId;
+import javax.persistence.Entity;
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinTable;
+import javax.persistence.Table;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.hibernate.annotations.CollectionOfElements;
+import org.hibernate.annotations.IndexColumn;
+
+/**
+ * @author Steve Ebersole
+ */
+ at Entity
+ at Table(name = "vgras007_v031")
+public class DoesNotWork implements Serializable {
+
+	private static final long serialVersionUID = 1L;
+
+	@EmbeddedId
+	private DoesNotWorkPk doesNotWorkPk;
+
+	@Column(name = "production_credits_tid", insertable = false, updatable = false)
+	private Long globAdditInfoTid;
+
+	@CollectionOfElements
+	@JoinTable(
+			name = "vgras029_v031",
+			joinColumns = @JoinColumn(name = "text_id", referencedColumnName = "production_credits_tid")
+	)
+	@Column(name = "text_part", insertable = false, updatable = false)
+	@IndexColumn(name = "seq_no", base = 1)
+	private List<String> globalNotes = new ArrayList<String>();
+
+	public DoesNotWork() {
+	}
+
+	public DoesNotWork(DoesNotWorkPk doesNotWorkPk) {
+		this.doesNotWorkPk = doesNotWorkPk;
+	}
+
+	public DoesNotWorkPk getDoesNotWorkPk() {
+		return doesNotWorkPk;
+	}
+
+	public void setDoesNotWorkPk(DoesNotWorkPk doesNotWorkPk) {
+		this.doesNotWorkPk = doesNotWorkPk;
+	}
+
+	public List<String> getGlobalNotes() {
+		return globalNotes;
+	}
+
+	public void setGlobalNotes(List<String> globalNotes) {
+		this.globalNotes = globalNotes;
+	}
+
+	public Long getGlobAdditInfoTid() {
+		return globAdditInfoTid;
+	}
+
+	public void setGlobAdditInfoTid(Long globAdditInfoTid) {
+		this.globAdditInfoTid = globAdditInfoTid;
+	}
+
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + ((doesNotWorkPk == null) ? 0 : doesNotWorkPk.hashCode());
+		return result;
+	}
+
+	@Override
+	public boolean equals(Object obj) {
+		if ( this == obj ) {
+			return true;
+		}
+		if ( obj == null ) {
+			return false;
+		}
+		if ( !(obj instanceof DoesNotWork) ) {
+			return false;
+		}
+		DoesNotWork other = (DoesNotWork) obj;
+		if ( doesNotWorkPk == null ) {
+			if ( other.doesNotWorkPk != null ) {
+				return false;
+			}
+		}
+		else if ( !doesNotWorkPk.equals( other.doesNotWorkPk ) ) {
+			return false;
+		}
+		return true;
+	}
+
+}

Added: annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkPk.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkPk.java	                        (rev 0)
+++ annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkPk.java	2012-07-25 07:13:17 UTC (rev 21044)
@@ -0,0 +1,78 @@
+package org.hibernate.test.annotations.propertyref;
+
+
+import javax.persistence.Column;
+import javax.persistence.Embeddable;
+import java.io.Serializable;
+
+/**
+ * @author Steve Ebersole
+ */
+ at Embeddable
+public class DoesNotWorkPk implements Serializable {
+
+	private static final long serialVersionUID = 1L;
+
+	@Column(name = "track_no")
+	private String id1;
+
+	@Column(name = "track_ext")
+	private String id2;
+
+	public String getId1() {
+		return id1;
+	}
+
+	public void setId1(String id1) {
+		this.id1 = id1;
+	}
+
+	public String getId2() {
+		return id2;
+	}
+
+	public void setId2(String id2) {
+		this.id2 = id2;
+	}
+
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + ((id1 == null) ? 0 : id1.hashCode());
+		result = prime * result + ((id2 == null) ? 0 : id2.hashCode());
+		return result;
+	}
+
+	@Override
+	public boolean equals(Object obj) {
+		if ( this == obj ) {
+			return true;
+		}
+		if ( obj == null ) {
+			return false;
+		}
+		if ( !(obj instanceof DoesNotWorkPk) ) {
+			return false;
+		}
+		DoesNotWorkPk other = (DoesNotWorkPk) obj;
+		if ( id1 == null ) {
+			if ( other.id1 != null ) {
+				return false;
+			}
+		}
+		else if ( !id1.equals( other.id1 ) ) {
+			return false;
+		}
+		if ( id2 == null ) {
+			if ( other.id2 != null ) {
+				return false;
+			}
+		}
+		else if ( !id2.equals( other.id2 ) ) {
+			return false;
+		}
+		return true;
+	}
+
+}
\ No newline at end of file

Added: annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkTest.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkTest.java	                        (rev 0)
+++ annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkTest.java	2012-07-25 07:13:17 UTC (rev 21044)
@@ -0,0 +1,59 @@
+package org.hibernate.test.annotations.propertyref;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.List;
+
+import org.hibernate.Session;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Environment;
+import org.hibernate.jdbc.Work;
+import org.hibernate.test.annotations.TestCase;
+
+/**
+ * @author Steve Ebersole
+ */
+public class DoesNotWorkTest extends TestCase {
+	@Override
+	protected Class<?>[] getAnnotatedClasses() {
+		return new Class[] {DoesNotWork.class};
+	}
+
+	@Override
+	protected void configure(Configuration configuration) {
+		super.configure( configuration );
+		configuration.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
+		configuration.setProperty( Environment.HBM2DDL_IMPORT_FILES, "/org/hibernate/test/annotations/propertyref/import.sql" );
+	}
+
+	public void testIt() {
+		DoesNotWorkPk pk = new DoesNotWorkPk();
+		pk.setId1( "ZZZ" );
+		pk.setId2( "00" );
+
+//		{
+//			Session session = openSession();
+//			session.beginTransaction();
+//			DoesNotWork entity = new DoesNotWork( pk );
+//			entity.setGlobalNotes( Arrays.asList( "My first note!" ) );
+//			session.save( entity );
+//			session.getTransaction().commit();
+//			session.close();
+//		}
+
+		{
+			Session session = openSession();
+			session.beginTransaction();
+			DoesNotWork entity = (DoesNotWork) session.get( DoesNotWork.class, pk );
+			List<String> notes = entity.getGlobalNotes();
+			if ( notes != null && notes.size() > 0 ) {
+				for ( String note : notes ) {
+					System.out.println( note );
+				}
+			}
+			session.delete( entity );
+			session.getTransaction().commit();
+			session.close();
+		}
+	}
+}

Added: annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkWithHbmTest.java
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkWithHbmTest.java	                        (rev 0)
+++ annotations/branches/v3_4_0_GA_CP/src/test/java/org/hibernate/test/annotations/propertyref/DoesNotWorkWithHbmTest.java	2012-07-25 07:13:17 UTC (rev 21044)
@@ -0,0 +1,64 @@
+package org.hibernate.test.annotations.propertyref;
+
+
+import java.util.List;
+
+import org.hibernate.Session;
+import org.hibernate.cfg.Configuration;
+
+import org.hibernate.cfg.Environment;
+import org.hibernate.junit.functional.FunctionalTestCase;
+
+/**
+ * @author Steve Ebersole
+ */
+public class DoesNotWorkWithHbmTest extends FunctionalTestCase {
+	public DoesNotWorkWithHbmTest(String string) {
+		super( string );
+	}
+
+	@Override
+	public String[] getMappings() {
+		return new String[] { "annotations/propertyref/Mapping.hbm.xml" };
+	}
+
+	@Override
+	public void configure(Configuration configuration) {
+		super.configure( configuration );
+		configuration.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" );
+		configuration.setProperty( Environment.HBM2DDL_IMPORT_FILES, "/org/hibernate/test/annotations/propertyref/import.sql" );
+	}
+
+	public void testIt() {
+		DoesNotWorkPk pk = new DoesNotWorkPk();
+		pk.setId1( "ZZZ" );
+		pk.setId2( "00" );
+
+//		{
+//			Session session = openSession();
+//			session.beginTransaction();
+//			DoesNotWork entity = new DoesNotWork( pk );
+//			entity.setGlobalNotes( Arrays.asList( "My first note!" ) );
+//			session.save( entity );
+//			session.getTransaction().commit();
+//			session.close();
+//		}
+
+		{
+			Session session = openSession();
+			session.beginTransaction();
+			DoesNotWork entity = (DoesNotWork) session.get( DoesNotWork.class, pk );
+			assertNotNull( entity );
+			List<String> notes = entity.getGlobalNotes();
+			assertNotNull( notes );
+			assertEquals( 2, notes.size() );
+			for ( String s : notes ) {
+				System.out.println( s );
+			}
+			session.delete( entity );
+			session.getTransaction().commit();
+			session.close();
+		}
+	}
+}
+

Added: annotations/branches/v3_4_0_GA_CP/src/test/resources/org/hibernate/test/annotations/propertyref/Mapping.hbm.xml
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/test/resources/org/hibernate/test/annotations/propertyref/Mapping.hbm.xml	                        (rev 0)
+++ annotations/branches/v3_4_0_GA_CP/src/test/resources/org/hibernate/test/annotations/propertyref/Mapping.hbm.xml	2012-07-25 07:13:17 UTC (rev 21044)
@@ -0,0 +1,19 @@
+<?xml version="1.0"?>
+<!DOCTYPE hibernate-mapping PUBLIC
+        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
+
+<hibernate-mapping package="org.hibernate.test.annotations.propertyref">
+    <class name="DoesNotWork" table="vgras007_v031">
+        <composite-id name="doesNotWorkPk" class="DoesNotWorkPk">
+            <key-property name="id1" column="track_no"/>
+            <key-property name="id2" column="track_ext"/>
+        </composite-id>
+        <property name="globAdditInfoTid" column="production_credits_tid"/>
+        <list name="globalNotes" table="vgras029_v031">
+            <key column="text_id" property-ref="globAdditInfoTid"/>
+            <list-index column="seq_no" base="1"/>
+            <element column="text_part" type="string"/>
+        </list>
+    </class>
+</hibernate-mapping>
\ No newline at end of file

Added: annotations/branches/v3_4_0_GA_CP/src/test/resources/org/hibernate/test/annotations/propertyref/import.sql
===================================================================
--- annotations/branches/v3_4_0_GA_CP/src/test/resources/org/hibernate/test/annotations/propertyref/import.sql	                        (rev 0)
+++ annotations/branches/v3_4_0_GA_CP/src/test/resources/org/hibernate/test/annotations/propertyref/import.sql	2012-07-25 07:13:17 UTC (rev 21044)
@@ -0,0 +1,3 @@
+INSERT INTO vgras007_v031 VALUES ('ZZZ','00',1);
+INSERT INTO vgras029_v031 VALUES (1,'Foo Foo Foo',1);
+INSERT INTO vgras029_v031 VALUES (1,'Bar Bar Bar',2);
\ No newline at end of file



More information about the hibernate-commits mailing list