[hibernate-commits] Hibernate SVN: r11200 - in branches/Branch_3_2/HibernateExt/annotations/src: java/org/hibernate/cfg and 2 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Feb 13 17:20:24 EST 2007


Author: epbernard
Date: 2007-02-13 17:20:24 -0500 (Tue, 13 Feb 2007)
New Revision: 11200

Modified:
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Table.java
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/OneToOneSecondPass.java
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/Cat.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java
Log:
ANN-444 ability to define fetchmode, inverse and optional to a secondary table

Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Table.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Table.java	2007-02-13 21:04:24 UTC (rev 11199)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Table.java	2007-02-13 22:20:24 UTC (rev 11200)
@@ -31,8 +31,29 @@
 
 	/**
 	 * Defines the Foreign Key name of a secondary table
-	 * back to the propary table 
+	 * back to the primary table
 	 */
 	ForeignKey foreignKey() default @ForeignKey( name="" );
 
+	/**
+	 * If set to JOIN, the default, Hibernate will use an inner join to retrieve a
+	 * secondary table defined by a class or its superclasses and an outer join for a
+	 * secondary table defined by a subclass.
+	 * If set to select then Hibernate will use a
+	 * sequential select for a secondary table defined on a subclass, which will be issued only if a row
+	 * turns out to represent an instance of the subclass. Inner joins will still be used to retrieve a
+	 * secondary defined by the class and its superclasses.
+	 */
+	FetchMode fetch() default FetchMode.JOIN;
+
+	/**
+	 * If enabled, Hibernate will not try to insert or update the properties defined by this join.
+	 */
+	boolean inverse() default false;
+
+	/**
+	 * If enabled, Hibernate will insert a row only if the properties defined by this join are non-null
+	 * and will always use an outer join to retrieve the properties.
+	 */
+	boolean optional() default true;
 }

Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/OneToOneSecondPass.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/OneToOneSecondPass.java	2007-02-13 21:04:24 UTC (rev 11199)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/OneToOneSecondPass.java	2007-02-13 22:20:24 UTC (rev 11200)
@@ -220,6 +220,7 @@
 		//TODO support @ForeignKey
 		join.setKey( key );
 		join.setSequentialSelect( false );
+		//TODO support for inverse and optional
 		join.setOptional( true ); //perhaps not quite per-spec, but a Good Thing anyway
 		key.setCascadeDeleteEnabled( false );
 		Iterator mappedByColumns = otherSideProperty.getValue().getColumnIterator();

Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java	2007-02-13 21:04:24 UTC (rev 11199)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java	2007-02-13 22:20:24 UTC (rev 11200)
@@ -38,6 +38,7 @@
 import org.hibernate.annotations.Tuplizers;
 import org.hibernate.annotations.Tuplizer;
 import org.hibernate.annotations.Immutable;
+import org.hibernate.annotations.FetchMode;
 import org.hibernate.cache.CacheFactory;
 import org.hibernate.cfg.AnnotationBinder;
 import org.hibernate.cfg.BinderHelper;
@@ -500,9 +501,6 @@
 		SimpleValue key = new DependantValue( join.getTable(), persistentClass.getIdentifier() );
 		join.setKey( key );
 		setFKNameIfDefined(join);
-		join.setSequentialSelect( false );
-		join.setInverse( false );
-		join.setOptional( true ); //perhaps not quite per-spec, but a Good Thing anyway
 		key.setCascadeDeleteEnabled( false );
 		TableBinder.bindFk( persistentClass, null, ejb3JoinColumns, key, false, mappings );
 		join.createPrimaryKey();
@@ -511,6 +509,13 @@
 	}
 
 	private void setFKNameIfDefined(Join join) {
+		org.hibernate.annotations.Table matchingTable = findMatchingComplimentTableAnnotation( join );
+		if (matchingTable != null && ! BinderHelper.isDefault( matchingTable.foreignKey().name() ) ) {
+			( (SimpleValue) join.getKey() ).setForeignKeyName( matchingTable.foreignKey().name() );
+		}
+	}
+
+	private org.hibernate.annotations.Table findMatchingComplimentTableAnnotation(Join join) {
 		String tableName = join.getTable().getQuotedName();
 		org.hibernate.annotations.Table table = annotatedClass.getAnnotation( org.hibernate.annotations.Table.class );
 		org.hibernate.annotations.Table matchingTable = null;
@@ -518,7 +523,7 @@
 			matchingTable = table;
 		}
 		else {
-			org.hibernate.annotations.Tables tables = annotatedClass.getAnnotation( org.hibernate.annotations.Tables.class );
+			Tables tables = annotatedClass.getAnnotation( Tables.class );
 			if ( tables != null) {
 				for (org.hibernate.annotations.Table current : tables.value() ) {
 					if ( tableName.equals( current.appliesTo() ) ) {
@@ -528,9 +533,7 @@
 				}
 			}
 		}
-		if (matchingTable != null && ! BinderHelper.isDefault( matchingTable.foreignKey().name() ) ) {
-			( (SimpleValue) join.getKey() ).setForeignKeyName( matchingTable.foreignKey().name() );
-		}
+		return matchingTable;
 	}
 
 	public void firstLevelSecondaryTablesBinding(
@@ -547,10 +550,9 @@
 		}
 	}
 
+	//Used for @*ToMany @JoinTable
 	public Join addJoin(JoinTable joinTable, PropertyHolder holder, boolean noDelayInPkColumnCreation) {
-		Join join = addJoin( null, joinTable, holder, noDelayInPkColumnCreation );
-		join.setOptional( true );
-		return join;
+		return addJoin( null, joinTable, holder, noDelayInPkColumnCreation );
 	}
 
 	/**
@@ -566,29 +568,29 @@
 		String catalog;
 		String table;
 		String realTable;
-		UniqueConstraint[] uniqueCosntraintsAnn;
+		UniqueConstraint[] uniqueConstraintsAnn;
 		if ( secondaryTable != null ) {
 			schema = secondaryTable.schema();
 			catalog = secondaryTable.catalog();
 			table = secondaryTable.name();
 			realTable = mappings.getNamingStrategy().tableName( table ); //always an explicit table name
-			uniqueCosntraintsAnn = secondaryTable.uniqueConstraints();
+			uniqueConstraintsAnn = secondaryTable.uniqueConstraints();
 		}
 		else if ( joinTable != null ) {
 			schema = joinTable.schema();
 			catalog = joinTable.catalog();
 			table = joinTable.name();
 			realTable = mappings.getNamingStrategy().tableName( table ); //always an explicit table name
-			uniqueCosntraintsAnn = joinTable.uniqueConstraints();
+			uniqueConstraintsAnn = joinTable.uniqueConstraints();
 		}
 		else {
 			throw new AssertionFailure( "Both JoinTable and SecondaryTable are null" );
 		}
-		List uniqueConstraints = new ArrayList( uniqueCosntraintsAnn == null ?
+		List uniqueConstraints = new ArrayList( uniqueConstraintsAnn == null ?
 				0 :
-				uniqueCosntraintsAnn.length );
-		if ( uniqueCosntraintsAnn != null && uniqueCosntraintsAnn.length != 0 ) {
-			for ( UniqueConstraint uc : uniqueCosntraintsAnn ) {
+				uniqueConstraintsAnn.length );
+		if ( uniqueConstraintsAnn != null && uniqueConstraintsAnn.length != 0 ) {
+			for ( UniqueConstraint uc : uniqueConstraintsAnn ) {
 				uniqueConstraints.add( uc.columnNames() );
 			}
 		}
@@ -617,9 +619,22 @@
 							.getName()
 			);
 		}
+
+		org.hibernate.annotations.Table matchingTable = findMatchingComplimentTableAnnotation( join );
+		if (matchingTable != null) {
+			join.setSequentialSelect( FetchMode.JOIN != matchingTable.fetch() );
+			join.setInverse( matchingTable.inverse() );
+			join.setOptional( matchingTable.optional() );
+		}
+		else {
+			//default
+			join.setSequentialSelect( false );
+			join.setInverse( false );
+			join.setOptional( true ); //perhaps not quite per-spec, but a Good Thing anyway
+		}
+
 		if ( noDelayInPkColumnCreation ) {
 			createPrimaryColumnsToSecondaryTable( joinColumns, propertyHolder, join );
-
 		}
 		else {
 			secondaryTables.put( table, join );

Modified: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/Cat.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/Cat.java	2007-02-13 21:04:24 UTC (rev 11199)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/Cat.java	2007-02-13 22:20:24 UTC (rev 11200)
@@ -14,6 +14,7 @@
 import org.hibernate.annotations.Table;
 import org.hibernate.annotations.ForeignKey;
 import org.hibernate.annotations.Tables;
+import org.hibernate.annotations.FetchMode;
 
 /**
  * @author Emmanuel Bernard
@@ -26,7 +27,7 @@
 @Tables( {
 	@Table(appliesTo = "Cat", indexes = @Index(name = "secondname",
 			columnNames = "secondName"), comment = "My cat table" ),
-	@Table(appliesTo = "Cat2", foreignKey = @ForeignKey(name="FK_CAT2_CAT") )
+	@Table(appliesTo = "Cat2", foreignKey = @ForeignKey(name="FK_CAT2_CAT"), fetch = FetchMode.SELECT)
 			} )
 public class Cat implements Serializable {
 

Modified: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java	2007-02-13 21:04:24 UTC (rev 11199)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/JoinTest.java	2007-02-13 22:20:24 UTC (rev 11200)
@@ -140,6 +140,23 @@
 		}
 	}
 
+	public void testFetchModeOnSecondaryTable() throws Exception {
+		Cat cat = new Cat();
+		cat.setStoryPart2( "My long story" );
+		Session s = openSession();
+		Transaction tx = s.beginTransaction();
+
+		s.persist( cat );
+		s.flush();
+		s.clear();
+		
+		s.get( Cat.class, cat.getId() );
+		//Find a way to test it, I need to define the secondary table on a subclass
+
+		tx.rollback();
+		s.close();
+	}
+
 	/**
 	 * @see org.hibernate.test.annotations.TestCase#getMappings()
 	 */




More information about the hibernate-commits mailing list