Hi Pablo,
Did yhou open a JIRA issue for this one, I can't find it. I remember I was alerted to this problem but I can't remember where ;-) While looking at your patch, I just realized that this was the expected behavior. Let me explain. by default @JoinColumn(nullable=true) which means that FK columns will be nullable, nullable columns cannot be part of a PK in most DBso Hibernate does not generate the PK for association table. If you override your inverse join columns by @JoinColumn(nullable=false) the PK will be created.

I'll then not apply your patch but I'll raise the problem to the EJB3 EG to kinda relax the nullable=true semantic on appropriate cases.

Thanks for the good feedback.

Pablo Nussembaum wrote:

Hello,
   First of all I'm sending this patch that resolves the problem of the
missing primary key on associations tables for many to many relations (I
set the pk to the concatenations of joinColumns+inverseJoinColumns).

   Second I have a doubt with the changes in the events classes.  I'm
using the Pre(Inset|Update|Delete)Event events to make an audit log
framework but in the HEAD branch this classes don't have the
getSession() method anymore. My question is how I obtain the jdbc
connection of the current session?

here is a example:

public class PreInsertAuditor extends DefaultPreInsertEventListener {
private AuditLog auditLog; @Override
   public boolean onPreInsert(PreInsertEvent event) {
       if (!(event.getEntity() instanceof AuditLogRecord)) {
           auditLog.logEvent("create", event.getEntity(),
               event.getSession().connection());//this line does not
compile :(
       }
       return super.onPreInsert(event);
   }

   public void setAuditLog(AuditLog auditLog) {
       this.auditLog = auditLog;
   }
}


   Thanks!

------------------------------------------------------------------------

Index: org/hibernate/cfg/AnnotationBinder.java
===================================================================
RCS file: 
/cvsroot/hibernate/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationBinder.java,v
retrieving revision 1.90
diff -u -r1.90 AnnotationBinder.java
--- org/hibernate/cfg/AnnotationBinder.java     9 Jun 2005 18:34:52 -0000       
1.90
+++ org/hibernate/cfg/AnnotationBinder.java     9 Jun 2005 22:38:57 -0000
@@ -8,6 +8,7 @@
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@@ -90,6 +91,7 @@
import org.hibernate.mapping.Join;
import org.hibernate.mapping.JoinedSubclass;
import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.PrimaryKey;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.RootClass;
import org.hibernate.mapping.SimpleValue;
@@ -99,6 +101,7 @@
import org.hibernate.persister.entity.JoinedSubclassEntityPersister;
import org.hibernate.persister.entity.SingleTableEntityPersister;
import org.hibernate.persister.entity.UnionSubclassEntityPersister;
+import org.hibernate.sql.Alias;
import org.hibernate.type.ByteArrayBlobType;
import org.hibernate.type.CharacterArrayClobType;
import org.hibernate.type.ForeignKeyDirection;
@@ -891,7 +894,7 @@
                                collectionBinder.setUnique(false);
                                collectionBinder.setTargetEntity( 
manyToManyAnn.targetEntity() );
                                collectionBinder.setFetchType( 
manyToManyAnn.fetch() );
-                               collectionBinder.setCascadeStrategy( 
getCascadeStrategy( manyToManyAnn.cascade(), hibernateCascade ) );
+                               collectionBinder.setCascadeStrategy( 
getCascadeStrategy( manyToManyAnn.cascade(), hibernateCascade ) ); 
                        }
                        collectionBinder.setMappedBy( mappedBy );
                        if (isForeignKey) {
@@ -1036,11 +1039,33 @@
                }
                Ejb3JoinColumn[] joinColumns = buildArrayOfEjb3JoinColumn( 
annJoins, entityBinder.getSecondaryTables(), propertyHolder, 
inferredData.getPropertyName(), mappedBy, mappings);
                Ejb3JoinColumn[] inverseJoinColumns = 
buildArrayOfEjb3JoinColumn( annInverseJoins, entityBinder.getSecondaryTables(), 
propertyHolder, inferredData.getPropertyName(), mappedBy, mappings);
+               buildPKForAssocTable(assocTable, joinColumns, 
inverseJoinColumns);
                collectionBinder.setTable(assocTable);
                collectionBinder.setJoinColumns(joinColumns);
                collectionBinder.setInverseJoinColumns(inverseJoinColumns);
        }

+       /**
+        * @param assocTable
+        * @param joinColumns
+        * @param inverseJoinColumns
+        */
+       private static void buildPKForAssocTable(Table assocTable, 
Ejb3JoinColumn[] joinColumns, Ejb3JoinColumn[] inverseJoinColumns) {
+               List<org.hibernate.mapping.Column> pkColumns = new 
LinkedList<org.hibernate.mapping.Column>();
+               for (Ejb3JoinColumn ejb3JoinColumn : joinColumns) {
+                       pkColumns.add(ejb3JoinColumn.getMappingColumn());
+               }
+               for (Ejb3JoinColumn ejb3JoinColumn : inverseJoinColumns) {
+                       pkColumns.add(ejb3JoinColumn.getMappingColumn());
+               }
+               PrimaryKey pk = new PrimaryKey();               
+               pk.setTable(assocTable);
+               pk.setName( new Alias(15, "PK").toAliasString( 
assocTable.getName() ) );
+               assocTable.setPrimaryKey(pk);
+
+               pk.addColumns( pkColumns.iterator() );
+       }
+
        private static Ejb3JoinColumn[] buildArrayOfEjb3JoinColumn(JoinColumn[] 
annJoins, Map<String, Join> secondaryTables,
                                                                                
                                           PropertyHolder propertyHolder, 
String propertyName, String mappedBy, ExtendedMappings mappings) {
                Ejb3JoinColumn[] joinColumns;


        

        
                
___________________________________________________________________________ Appel audio GRATUIT partout dans le monde avec le nouveau Yahoo! Messenger Téléchargez cette version sur http://fr.messenger.yahoo.com



-------------------------------------------------------
This SF.Net email is sponsored by: NEC IT Guy Games.  How far can you shotput
a projector? How fast can you ride your desk chair down the office luge track?
If you want to score the big prize, get to know the little guy. Play to win an NEC 61" plasma display: http://www.necitguy.com/?r=20
_______________________________________________
hibernate-devel mailing list
hibernate-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to