[hibernate-commits] Hibernate SVN: r10297 - in trunk/HibernateExt/metadata/src: java/org/hibernate/annotations java/org/hibernate/cfg java/org/hibernate/cfg/annotations test/org/hibernate/test/annotations/manytomany test/org/hibernate/test/annotations/manytoone test/org/hibernate/test/annotations/onetomany
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Fri Aug 18 18:46:07 EDT 2006
Author: epbernard
Date: 2006-08-18 18:46:02 -0400 (Fri, 18 Aug 2006)
New Revision: 10297
Added:
trunk/HibernateExt/metadata/src/java/org/hibernate/annotations/ForeignKey.java
Modified:
trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationBinder.java
trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/OneToOneSecondPass.java
trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytomany/Woman.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytoone/Car.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytoone/ForestType.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/onetomany/City.java
trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/onetomany/Trainer.java
Log:
ANN-261 add ForeignKey naming (inspired by David Hay's patch)
Added: trunk/HibernateExt/metadata/src/java/org/hibernate/annotations/ForeignKey.java
===================================================================
--- trunk/HibernateExt/metadata/src/java/org/hibernate/annotations/ForeignKey.java 2006-08-18 22:02:38 UTC (rev 10296)
+++ trunk/HibernateExt/metadata/src/java/org/hibernate/annotations/ForeignKey.java 2006-08-18 22:46:02 UTC (rev 10297)
@@ -0,0 +1,28 @@
+//$Id: $
+package org.hibernate.annotations;
+
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
+
+ at Target({FIELD, METHOD})
+ at Retention(RUNTIME)
+
+/**
+ * Define the foreign key name
+ */
+public @interface ForeignKey {
+ /**
+ * Name of the foreign key. Used in OneToMany, ManyToOne, and OneToOne
+ * relationships. Used for the owning side in ManyToMany relationships
+ */
+ String name();
+
+ /**
+ * Used for the non-owning side of a ManyToMany relationship. Ignored
+ * in other relationships
+ */
+ String inverseName() default "";
+}
\ No newline at end of file
Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationBinder.java 2006-08-18 22:02:38 UTC (rev 10296)
+++ trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationBinder.java 2006-08-18 22:46:02 UTC (rev 10297)
@@ -86,6 +86,7 @@
import org.hibernate.annotations.TypeDefs;
import org.hibernate.annotations.Where;
import org.hibernate.annotations.CollectionId;
+import org.hibernate.annotations.ForeignKey;
import org.hibernate.cfg.annotations.CollectionBinder;
import org.hibernate.cfg.annotations.EntityBinder;
import org.hibernate.cfg.annotations.Nullability;
@@ -1749,7 +1750,10 @@
final String propertyName = inferredData.getPropertyName();
value.setTypeUsingReflection( propertyHolder.getClassName(), propertyName );
- //value.createForeignKey();
+ ForeignKey fk = inferredData.getProperty().getAnnotation( ForeignKey.class );
+ String fkName = fk != null ? fk.name() : "";
+ if ( ! BinderHelper.isDefault( fkName ) ) value.setForeignKeyName( fkName );
+
String path = propertyHolder.getPath() + "." + propertyName;
mappings.addSecondPass(
new FkSecondPass(
Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/OneToOneSecondPass.java
===================================================================
--- trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/OneToOneSecondPass.java 2006-08-18 22:02:38 UTC (rev 10296)
+++ trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/OneToOneSecondPass.java 2006-08-18 22:46:02 UTC (rev 10297)
@@ -6,6 +6,7 @@
import org.hibernate.AnnotationException;
import org.hibernate.MappingException;
+import org.hibernate.annotations.ForeignKey;
import org.hibernate.cfg.annotations.PropertyBinder;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.DependantValue;
@@ -201,6 +202,9 @@
);
}
}
+ ForeignKey fk = inferredData.getProperty().getAnnotation( ForeignKey.class );
+ String fkName = fk != null ? fk.name() : "";
+ if ( ! BinderHelper.isDefault( fkName ) ) value.setForeignKeyName( fkName );
}
//dirty dupe of EntityBinder.bindSecondaryTable
Modified: trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java
===================================================================
--- trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java 2006-08-18 22:02:38 UTC (rev 10296)
+++ trunk/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/CollectionBinder.java 2006-08-18 22:46:02 UTC (rev 10297)
@@ -34,6 +34,7 @@
import org.hibernate.annotations.SortType;
import org.hibernate.annotations.Where;
import org.hibernate.annotations.CollectionId;
+import org.hibernate.annotations.ForeignKey;
import org.hibernate.cfg.AnnotatedClassType;
import org.hibernate.cfg.AnnotationBinder;
import org.hibernate.cfg.BinderHelper;
@@ -589,7 +590,7 @@
"Mapping collection: " + collection.getRole() + " -> " + collection.getCollectionTable().getName()
);
- bindCollectionSecondPass( collection, null, fkJoinColumns, cascadeDeleteEnabled, mappings );
+ bindCollectionSecondPass( collection, null, fkJoinColumns, cascadeDeleteEnabled, property, mappings );
if ( !collection.isInverse()
&& !collection.getKey().isNullable() ) {
// for non-inverse one-to-many, with a not-null fk, add a backref!
@@ -807,7 +808,7 @@
private static SimpleValue buildCollectionKey(
Collection collValue, Ejb3JoinColumn[] joinColumns, boolean cascadeDeleteEnabled,
- ExtendedMappings mappings
+ XProperty property, ExtendedMappings mappings
) {
//binding key reference using column
KeyValue keyVal;
@@ -837,11 +838,14 @@
}
DependantValue key = new DependantValue( collValue.getCollectionTable(), keyVal );
key.setTypeName( null );
- Ejb3JoinColumn.checkPropertyConsistency( joinColumns, collValue.getOwnerEntityName() );
+ Ejb3Column.checkPropertyConsistency( joinColumns, collValue.getOwnerEntityName() );
key.setNullable( joinColumns.length == 0 || joinColumns[0].isNullable() );
key.setUpdateable( joinColumns.length == 0 || joinColumns[0].isUpdatable() );
key.setCascadeDeleteEnabled( cascadeDeleteEnabled );
collValue.setKey( key );
+ ForeignKey fk = property != null ? property.getAnnotation( ForeignKey.class ) : null;
+ String fkName = fk != null ? fk.name() : "";
+ if ( ! BinderHelper.isDefault( fkName ) ) key.setForeignKeyName( fkName );
return key;
}
@@ -961,7 +965,7 @@
collValue.setCollectionTable( associationTableBinder.bind() );
}
- bindCollectionSecondPass( collValue, collectionEntity, joinColumns, cascadeDeleteEnabled, mappings );
+ bindCollectionSecondPass( collValue, collectionEntity, joinColumns, cascadeDeleteEnabled, property, mappings );
ManyToOne element = null;
if ( isCollectionOfEntities ) {
@@ -980,6 +984,9 @@
buildOrderByClauseFromHql( hqlOrderBy, collectionEntity, collValue.getRole() )
);
}
+ ForeignKey fk = property != null ? property.getAnnotation( ForeignKey.class ) : null;
+ String fkName = fk != null ? fk.inverseName() : "";
+ if ( ! BinderHelper.isDefault( fkName ) ) element.setForeignKeyName( fkName );
}
else {
XClass elementClass;
@@ -1105,13 +1112,13 @@
private static void bindCollectionSecondPass(
Collection collValue, PersistentClass collectionEntity, Ejb3JoinColumn[] joinColumns,
- boolean cascadeDeleteEnabled,
+ boolean cascadeDeleteEnabled, XProperty property,
ExtendedMappings mappings
) {
BinderHelper.createSyntheticPropertyReference(
joinColumns, collValue.getOwner(), collectionEntity, collValue, false, mappings
);
- SimpleValue key = buildCollectionKey( collValue, joinColumns, cascadeDeleteEnabled, mappings );
+ SimpleValue key = buildCollectionKey( collValue, joinColumns, cascadeDeleteEnabled, property, mappings );
TableBinder.bindFk( collValue.getOwner(), collectionEntity, joinColumns, key, false, mappings );
}
Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytomany/Woman.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytomany/Woman.java 2006-08-18 22:02:38 UTC (rev 10296)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytomany/Woman.java 2006-08-18 22:46:02 UTC (rev 10297)
@@ -10,6 +10,8 @@
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
+import org.hibernate.annotations.ForeignKey;
+
/**
* Woman knowing several mens
*
@@ -44,6 +46,7 @@
@JoinColumn(name = "manFirstName", referencedColumnName = "firstName")
}
)
+ @ForeignKey(name = "WM_W_FK", inverseName = "WM_M_FK")
public Set<Man> getMens() {
return mens;
}
Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytoone/Car.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytoone/Car.java 2006-08-18 22:02:38 UTC (rev 10296)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytoone/Car.java 2006-08-18 22:46:02 UTC (rev 10297)
@@ -7,6 +7,8 @@
import javax.persistence.Id;
import javax.persistence.ManyToOne;
+import org.hibernate.annotations.ForeignKey;
+
/**
* Many to one sample using default mapping values
*
@@ -29,6 +31,7 @@
}
@ManyToOne(fetch = FetchType.EAGER)
+ @ForeignKey(name="BODY_COLOR_FK")
public Color getBodyColor() {
return bodyColor;
}
Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytoone/ForestType.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytoone/ForestType.java 2006-08-18 22:02:38 UTC (rev 10296)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/manytoone/ForestType.java 2006-08-18 22:46:02 UTC (rev 10297)
@@ -10,6 +10,8 @@
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
+import org.hibernate.annotations.ForeignKey;
+
/**
* @author Emmanuel Bernard
*/
@@ -25,6 +27,9 @@
joinColumns = @JoinColumn(name="forest_type"),
inverseJoinColumns = @JoinColumn(name="forest")
)
+ @ForeignKey(name="A_TYP_FK",
+ inverseName = "A_FOR_FK" //inverse fail cause it involves a Join
+ )
public BiggestForest getBiggestRepresentative() {
return biggestRepresentative;
}
Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/onetomany/City.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/onetomany/City.java 2006-08-18 22:02:38 UTC (rev 10296)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/onetomany/City.java 2006-08-18 22:46:02 UTC (rev 10297)
@@ -10,6 +10,8 @@
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
+import org.hibernate.annotations.ForeignKey;
+
/**
* @author Emmanuel Bernard
*/
@@ -50,6 +52,7 @@
@OneToMany()
@JoinColumn(name = "mainstreetcity_id")
+ @ForeignKey(name = "CITYSTR_FK")
@OrderBy
public List<Street> getMainStreets() {
return mainStreets;
Modified: trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/onetomany/Trainer.java
===================================================================
--- trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/onetomany/Trainer.java 2006-08-18 22:02:38 UTC (rev 10296)
+++ trunk/HibernateExt/metadata/src/test/org/hibernate/test/annotations/onetomany/Trainer.java 2006-08-18 22:46:02 UTC (rev 10297)
@@ -9,6 +9,8 @@
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
+import org.hibernate.annotations.ForeignKey;
+
/**
* Unidirectional one to many sample
*
@@ -54,6 +56,7 @@
joinColumns = {@JoinColumn(name = "trainer_id")},
inverseJoinColumns = @JoinColumn(name = "monkey_id")
)
+ @ForeignKey(name = "TM_TRA_FK", inverseName = "TM_MON_FK")
public Set<Monkey> getTrainedMonkeys() {
return trainedMonkeys;
}
More information about the hibernate-commits
mailing list