-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Annotating foreign key constraint names?
PostPosted: Sun Jan 01, 2006 4:50 pm 
Newbie

Joined: Sun Mar 20, 2005 10:40 am
Posts: 11
Location: London (UK)
Do the Hibernate annotations provide any way of over-riding the default foreign key names on many-to-many relationships?

For example, with respect to the following annotated class...
Code:

@Entity(access = AccessType.PROPERTY)
@Table(name="RoleGroup")
public class RoleGroup {

   @Id(generate=GeneratorType.AUTO)
   @Column(name="roleGroupId")
   private Long getGroupId() {
      return this.roleGroupId;
   }

    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(table=@Table(name="ROLEGROUP_TO_ROLE"))
    public Set<Role> getRoles() {
      return this.roles;
   }

    // ...
}

I'd like to be able to denote the appropriate names of the foreign keys in the ROLEGROUP_TO_ROLE table as ROLEGROUPID and ROLEID (or something similar), so that the hbm2ddl Ant task will generate more user-friendly constraints.

I can't see anything in the EJB3 spec that allows for this - will Hibernate provide an extension to permit this? Or does it already and I've missed it? Or do I just have to use plain old mapping files if I want that facility?

Thanks,
Dave.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 02, 2006 6:15 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is not supported. Patch welcome though

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 02, 2006 7:42 am 
Newbie

Joined: Sun Mar 20, 2005 10:40 am
Posts: 11
Location: London (UK)
emmanuel wrote:
This is not supported. Patch welcome though

Gauntlet received and understood ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 02, 2006 7:45 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
:-)
It could have been worse, I could have said "no and I don't want such a thing" ;-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Created a patch
PostPosted: Fri Jun 30, 2006 7:03 pm 
Newbie

Joined: Fri Jun 30, 2006 7:00 pm
Posts: 6
I've attached a patch for renaming keys. Just add the ForeignKey annotation.

If there's a better place to send this, please let me know!

diff -uNrw ./src/org/hibernate/annotations/ForeignKey.java ../../javalibs/hibernate-annotations-3.2.0.CR1/src/org/hibernate/annotations/ForeignKey.java
--- ./src/org/hibernate/annotations/ForeignKey.java 1969-12-31 17:00:00.000000000 -0700
+++ ../../javalibs/hibernate-annotations-3.2.0.CR1/src/org/hibernate/annotations/ForeignKey.java 2006-06-30 16:29:25.000000000 -0600
@@ -0,0 +1,13 @@
+package org.hibernate.annotations;
+
+import java.lang.annotation.Target;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+
+@Target({FIELD, METHOD})
+@Retention(RUNTIME)
+public @interface ForeignKey {
+ String name();
+}
diff -uNrw ./src/org/hibernate/cfg/AnnotationBinder.java ../../javalibs/hibernate-annotations-3.2.0.CR1/src/org/hibernate/cfg/AnnotationBinder.java
--- ./src/org/hibernate/cfg/AnnotationBinder.java 2006-05-13 18:06:58.000000000 -0600
+++ ../../javalibs/hibernate-annotations-3.2.0.CR1/src/org/hibernate/cfg/AnnotationBinder.java 2006-06-30 16:39:40.000000000 -0600
@@ -1,128 +1,53 @@
//$Id: AnnotationBinder.java 9921 2006-05-10 13:40:04Z epbernard $
package org.hibernate.cfg;

-import java.util.ArrayList;
-import java.util.EnumSet;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-import javax.persistence.Basic;
-import javax.persistence.Column;
-import javax.persistence.DiscriminatorType;
-import javax.persistence.DiscriminatorValue;
-import javax.persistence.Embeddable;
-import javax.persistence.Embedded;
-import javax.persistence.EmbeddedId;
-import javax.persistence.Entity;
-import javax.persistence.FetchType;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
-import javax.persistence.IdClass;
-import javax.persistence.InheritanceType;
-import javax.persistence.JoinColumn;
-import javax.persistence.JoinColumns;
-import javax.persistence.JoinTable;
-import javax.persistence.ManyToMany;
-import javax.persistence.ManyToOne;
-import javax.persistence.MapKey;
-import javax.persistence.MappedSuperclass;
-import javax.persistence.NamedNativeQueries;
-import javax.persistence.NamedNativeQuery;
-import javax.persistence.NamedQueries;
-import javax.persistence.NamedQuery;
-import javax.persistence.OneToMany;
-import javax.persistence.OneToOne;
-import javax.persistence.PrimaryKeyJoinColumn;
-import javax.persistence.PrimaryKeyJoinColumns;
-import javax.persistence.SequenceGenerator;
-import javax.persistence.SqlResultSetMapping;
-import javax.persistence.SqlResultSetMappings;
-import javax.persistence.TableGenerator;
-import javax.persistence.Transient;
-import javax.persistence.Version;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.FetchMode;
import org.hibernate.MappingException;
-import org.hibernate.annotations.AccessType;
-import org.hibernate.annotations.BatchSize;
-import org.hibernate.annotations.Cache;
-import org.hibernate.annotations.Cascade;
+import org.hibernate.annotations.*;
import org.hibernate.annotations.CascadeType;
-import org.hibernate.annotations.Check;
-import org.hibernate.annotations.CollectionOfElements;
-import org.hibernate.annotations.Columns;
-import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.Filter;
-import org.hibernate.annotations.FilterDef;
-import org.hibernate.annotations.FilterDefs;
-import org.hibernate.annotations.Filters;
import org.hibernate.annotations.Formula;
-import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Index;
-import org.hibernate.annotations.LazyToOne;
-import org.hibernate.annotations.LazyToOneOption;
-import org.hibernate.annotations.MapKeyManyToMany;
-import org.hibernate.annotations.NotFound;
-import org.hibernate.annotations.NotFoundAction;
-import org.hibernate.annotations.OnDelete;
-import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.annotations.OrderBy;
-import org.hibernate.annotations.ParamDef;
-import org.hibernate.annotations.Parameter;
-import org.hibernate.annotations.Parent;
-import org.hibernate.annotations.Proxy;
-import org.hibernate.annotations.Sort;
-import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
-import org.hibernate.annotations.TypeDefs;
-import org.hibernate.annotations.Where;
-import org.hibernate.cfg.annotations.CollectionBinder;
-import org.hibernate.cfg.annotations.EntityBinder;
-import org.hibernate.cfg.annotations.Nullability;
-import org.hibernate.cfg.annotations.PropertyBinder;
-import org.hibernate.cfg.annotations.QueryBinder;
-import org.hibernate.cfg.annotations.SimpleValueBinder;
-import org.hibernate.cfg.annotations.TableBinder;
+import org.hibernate.annotations.ForeignKey;
+import org.hibernate.cfg.annotations.*;
import org.hibernate.engine.FilterDefinition;
import org.hibernate.engine.Versioning;
import org.hibernate.id.MultipleHiLoPerTableGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.id.SequenceHiLoGenerator;
import org.hibernate.id.TableHiLoGenerator;
-import org.hibernate.mapping.Component;
-import org.hibernate.mapping.DependantValue;
-import org.hibernate.mapping.IdGenerator;
-import org.hibernate.mapping.Join;
-import org.hibernate.mapping.JoinedSubclass;
-import org.hibernate.mapping.PersistentClass;
-import org.hibernate.mapping.Property;
-import org.hibernate.mapping.RootClass;
-import org.hibernate.mapping.SimpleValue;
-import org.hibernate.mapping.SingleTableSubclass;
-import org.hibernate.mapping.Subclass;
+import org.hibernate.mapping.*;
import org.hibernate.mapping.Table;
-import org.hibernate.mapping.ToOne;
-import org.hibernate.mapping.UnionSubclass;
import org.hibernate.persister.entity.JoinedSubclassEntityPersister;
import org.hibernate.persister.entity.SingleTableEntityPersister;
import org.hibernate.persister.entity.UnionSubclassEntityPersister;
-import org.hibernate.reflection.ReflectionManager;
-import org.hibernate.reflection.XAnnotatedElement;
-import org.hibernate.reflection.XClass;
-import org.hibernate.reflection.XPackage;
-import org.hibernate.reflection.XProperty;
+import org.hibernate.reflection.*;
import org.hibernate.type.TypeFactory;
import org.hibernate.util.StringHelper;

+import javax.persistence.*;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.ManyToOne;
+import javax.persistence.MapKey;
+import javax.persistence.NamedNativeQueries;
+import javax.persistence.NamedNativeQuery;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.OneToMany;
+import javax.persistence.OneToOne;
+import javax.persistence.Version;
+import java.util.*;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
/**
* JSR 175 annotation binder
* Will read the annotation from classes, apply the
@@ -1247,6 +1172,11 @@
joinColumn.setSecondaryTableName( join.getTable().getName() );
}
}
+ String foreignKeyName = null;
+ ForeignKey foreignKey = property.getAnnotation( ForeignKey.class );
+ if (foreignKey != null) {
+ foreignKeyName = foreignKey.name();
+ }
bindManyToOne(
getCascadeStrategy( ann.cascade(), hibernateCascade ),
joinColumns,
@@ -1254,7 +1184,7 @@
ignoreNotFound, onDeleteCascade,
mappings.getReflectionManager().toXClass( ann.targetEntity() ),
propertyHolder,
- inferredData, false, isIdentifierMapper, mappings
+ inferredData, false, isIdentifierMapper, foreignKeyName, mappings
);
}
else if ( property.isAnnotationPresent( OneToOne.class ) ) {
@@ -1282,6 +1212,11 @@
joinColumn.setSecondaryTableName( join.getTable().getName() );
}
}
+ String foreignKeyName = null;
+ ForeignKey foreignKey = property.getAnnotation( ForeignKey.class );
+ if (foreignKey != null) {
+ foreignKeyName = foreignKey.name();
+ }
bindOneToOne(
getCascadeStrategy( ann.cascade(), hibernateCascade ),
joinColumns,
@@ -1290,7 +1225,8 @@
ignoreNotFound, onDeleteCascade,
mappings.getReflectionManager().toXClass( ann.targetEntity() ),
propertyHolder,
- inferredData, ann.mappedBy(), trueOneToOne, isIdentifierMapper, mappings
+ inferredData, ann.mappedBy(), trueOneToOne, isIdentifierMapper,
+ foreignKeyName, mappings
);
}
else if ( property.isAnnotationPresent( OneToMany.class )
@@ -1759,7 +1695,8 @@
String cascadeStrategy, Ejb3JoinColumn[] columns, boolean optional,
boolean ignoreNotFound, boolean cascadeOnDelete,
XClass targetEntity, PropertyHolder propertyHolder,
- PropertyData inferredData, boolean unique, boolean isIdentifierMapper, ExtendedMappings mappings
+ PropertyData inferredData, boolean unique, boolean isIdentifierMapper,
+ String foreignKeyName, ExtendedMappings mappings
) {
//All FK columns should be in the same table
org.hibernate.mapping.ManyToOne value = new org.hibernate.mapping.ManyToOne( columns[0].getTable() );
@@ -1783,6 +1720,7 @@
final String propertyName = inferredData.getPropertyName();
value.setTypeUsingReflection( propertyHolder.getClassName(), propertyName );

+ value.setForeignKeyName(foreignKeyName);
//value.createForeignKey();
String path = propertyHolder.getPath() + "." + propertyName;
mappings.addSecondPass(
@@ -1869,7 +1807,7 @@
PropertyHolder propertyHolder,
PropertyData inferredData, String mappedBy,
boolean trueOneToOne,
- boolean isIdentifierMapper, ExtendedMappings mappings
+ boolean isIdentifierMapper, String foreignKeyName, ExtendedMappings mappings
) {
//column.getTable() => persistentClass.getTable()
final String propertyName = inferredData.getPropertyName();
@@ -1909,7 +1847,7 @@
bindManyToOne(
cascadeStrategy, joinColumns, optional, ignoreNotFound, cascadeOnDelete,
targetEntity,
- propertyHolder, inferredData, true, isIdentifierMapper, mappings
+ propertyHolder, inferredData, true, isIdentifierMapper, null, mappings
);
}
}


Last edited by dhay on Sat Jul 01, 2006 5:06 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Created a patch
PostPosted: Sat Jul 01, 2006 2:46 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 2:46 pm
Posts: 45
Location: Saskatoon SK Canada
dhay wrote:
If there's a better place to send this, please let me know!

You might want open an issue in the JIRA and attach the patch there.

http://opensource.atlassian.com/projects/hibernate/secure/Dashboard.jspa

_________________
Maury


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 01, 2006 5:10 pm 
Newbie

Joined: Fri Jun 30, 2006 7:00 pm
Posts: 6
I went ahead and attached the patch to the existing JIRA issue, ANN-261

David


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 9:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I'll look at that, thanks

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.