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

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Feb 13 00:22:09 EST 2007


Author: epbernard
Date: 2007-02-13 00:22:09 -0500 (Tue, 13 Feb 2007)
New Revision: 11195

Modified:
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/ForeignKey.java
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Table.java
   branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.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/inheritance/joined/Document.java
   branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/Cat.java
Log:
ANN-525 @ForeignKey for secondary table and joined subclasses

Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/ForeignKey.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/ForeignKey.java	2007-02-13 05:09:17 UTC (rev 11194)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/ForeignKey.java	2007-02-13 05:22:09 UTC (rev 11195)
@@ -3,11 +3,12 @@
 
 import static java.lang.annotation.ElementType.FIELD;
 import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
 import java.lang.annotation.Retention;
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 import java.lang.annotation.Target;
 
- at Target({FIELD, METHOD})
+ at Target({FIELD, METHOD, TYPE})
 @Retention(RUNTIME)
 
 /**

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 05:09:17 UTC (rev 11194)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/annotations/Table.java	2007-02-13 05:22:09 UTC (rev 11195)
@@ -29,4 +29,10 @@
 	 */
 	String comment() default "";
 
+	/**
+	 * Defines the Foreign Key name of a secondary table
+	 * back to the propary table 
+	 */
+	ForeignKey foreignKey() default @ForeignKey( name="" );
+
 }

Modified: branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java	2007-02-13 05:09:17 UTC (rev 11194)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/AnnotationBinder.java	2007-02-13 05:22:09 UTC (rev 11195)
@@ -586,6 +586,10 @@
 			}
 			SimpleValue key = new DependantValue( jsc.getTable(), jsc.getIdentifier() );
 			jsc.setKey( key );
+			ForeignKey fk = annotatedClass.getAnnotation( ForeignKey.class );
+			if (fk != null && ! BinderHelper.isDefault( fk.name() ) ) {
+				key.setForeignKeyName( fk.name() );
+			}
 			if ( onDeleteAnn != null ) {
 				key.setCascadeDeleteEnabled( OnDeleteAction.CASCADE.equals( onDeleteAnn.action() ) );
 			}
@@ -750,7 +754,7 @@
 
 		if ( !inheritanceState.hasParents ) {
 			final RootClass rootClass = (RootClass) persistentClass;
-			//no need to handle inSecondPass this is an Etntiy related work
+			//no need to handle inSecondPass this is an Entity related work
 			mappings.addSecondPass( new CreateKeySecondPass( rootClass ) );
 		}
 		else {

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 05:09:17 UTC (rev 11194)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/OneToOneSecondPass.java	2007-02-13 05:22:09 UTC (rev 11195)
@@ -217,6 +217,7 @@
 		join.setTable( originalJoin.getTable() );
 		join.setInverse( true );
 		SimpleValue key = new DependantValue( join.getTable(), persistentClass.getIdentifier() );
+		//TODO support @ForeignKey
 		join.setKey( key );
 		join.setSequentialSelect( false );
 		join.setOptional( true ); //perhaps not quite per-spec, but a Good Thing anyway

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 05:09:17 UTC (rev 11194)
+++ branches/Branch_3_2/HibernateExt/annotations/src/java/org/hibernate/cfg/annotations/EntityBinder.java	2007-02-13 05:22:09 UTC (rev 11195)
@@ -491,14 +491,15 @@
 		for ( Ejb3JoinColumn joinColumn : ejb3JoinColumns ) {
 			joinColumn.forceNotNull();
 		}
-		bindJoinToPersistentClass( join, ejb3JoinColumns, persistentClass, mappings );
+		bindJoinToPersistentClass( join, ejb3JoinColumns );
 	}
 
-	public static void bindJoinToPersistentClass(
-			Join join, Ejb3JoinColumn[] ejb3JoinColumns, PersistentClass persistentClass, ExtendedMappings mappings
+	private void bindJoinToPersistentClass(
+			Join join, Ejb3JoinColumn[] ejb3JoinColumns
 	) {
 		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
@@ -509,6 +510,29 @@
 		persistentClass.addJoin( join );
 	}
 
+	private void setFKNameIfDefined(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;
+		if ( table != null && tableName.equals( table.appliesTo() ) ) {
+			matchingTable = table;
+		}
+		else {
+			org.hibernate.annotations.Tables tables = annotatedClass.getAnnotation( org.hibernate.annotations.Tables.class );
+			if ( tables != null) {
+				for (org.hibernate.annotations.Table current : tables.value() ) {
+					if ( tableName.equals( current.appliesTo() ) ) {
+						matchingTable = current;
+						break;
+					}
+				}
+			}
+		}
+		if (matchingTable != null && ! BinderHelper.isDefault( matchingTable.foreignKey().name() ) ) {
+			( (SimpleValue) join.getKey() ).setForeignKeyName( matchingTable.foreignKey().name() );
+		}
+	}
+
 	public void firstLevelSecondaryTablesBinding(
 			SecondaryTable secTable, SecondaryTables secTables
 	) {
@@ -666,19 +690,19 @@
 
 	public void processComplementaryTableDefinitions(org.hibernate.annotations.Table table) {
 		if ( table == null ) return;
-		String tableName = table.appliesTo();
+		String appliedTable = table.appliesTo();
 		Iterator tables = persistentClass.getTableClosureIterator();
 		Table hibTable = null;
 		while ( tables.hasNext() ) {
 			hibTable = (Table) tables.next();
-			if ( hibTable.getName().equals( tableName ) ) {
+			if ( hibTable.getQuotedName().equals( appliedTable ) ) {
 				//we are in the correct table to find columns
 				break;
 			}
 		}
 		if ( hibTable == null ) {
 			throw new AnnotationException(
-					"@org.hibernate.annotations.Table references an unknown table: " + tableName
+					"@org.hibernate.annotations.Table references an unknown table: " + appliedTable
 			);
 		}
 		if ( ! BinderHelper.isDefault( table.comment() ) ) hibTable.setComment( table.comment() );
@@ -687,7 +711,7 @@
 
 	public void processComplementaryTableDefinitions(Tables tables) {
 		if ( tables == null ) return;
-		for ( org.hibernate.annotations.Table table : tables.values() ) {
+		for ( org.hibernate.annotations.Table table : tables.value() ) {
 			processComplementaryTableDefinitions( table );
 		}
 	}

Modified: branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/inheritance/joined/Document.java
===================================================================
--- branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/inheritance/joined/Document.java	2007-02-13 05:09:17 UTC (rev 11194)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/inheritance/joined/Document.java	2007-02-13 05:22:09 UTC (rev 11195)
@@ -4,10 +4,13 @@
 import javax.persistence.Column;
 import javax.persistence.Entity;
 
+import org.hibernate.annotations.ForeignKey;
+
 /**
  * @author Emmanuel Bernard
  */
 @Entity
+ at ForeignKey(name = "FK_DOCU_FILE")
 public class Document extends File {
 	@Column(nullable = false)
 	private int size;

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 05:09:17 UTC (rev 11194)
+++ branches/Branch_3_2/HibernateExt/annotations/src/test/org/hibernate/test/annotations/join/Cat.java	2007-02-13 05:22:09 UTC (rev 11195)
@@ -12,6 +12,8 @@
 
 import org.hibernate.annotations.Index;
 import org.hibernate.annotations.Table;
+import org.hibernate.annotations.ForeignKey;
+import org.hibernate.annotations.Tables;
 
 /**
  * @author Emmanuel Bernard
@@ -21,7 +23,11 @@
 @SecondaryTable(name = "`Cat nbr1`"),
 @SecondaryTable(name = "Cat2", uniqueConstraints = {@UniqueConstraint(columnNames = {"storyPart2"})})
 		})
- at Table(appliesTo = "Cat", indexes = @Index(name = "secondname", columnNames = "secondName"), comment = "My cat table")
+ at Tables( {
+	@Table(appliesTo = "Cat", indexes = @Index(name = "secondname",
+			columnNames = "secondName"), comment = "My cat table" ),
+	@Table(appliesTo = "Cat2", foreignKey = @ForeignKey(name="FK_CAT2_CAT") )
+			} )
 public class Cat implements Serializable {
 
 	private Integer id;




More information about the hibernate-commits mailing list