-->
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.  [ 3 posts ] 
Author Message
 Post subject: @OneToMany causing wrong unique key?!
PostPosted: Wed May 19, 2010 2:46 pm 
Newbie

Joined: Thu Feb 12, 2004 5:42 am
Posts: 4
Location: Germany, Freiburg i.Br.
Hello *,

I'm usually using JDO with DataNucleus, but for a customer's project, I'm porting some of our existing data models to JPA with Hibernate. Thus I'm pretty new to JPA and maybe I made a mistake in my usage of @OneToMany, but I'm pretty sure that I found a bug in Hibernate.

Here's the situation (just a small & simplified excerpt):
Code:
public class RoleGroupRef
{
  @EmbeddedId
  private RoleGroupRefID objectID;

  @OneToMany
  @JoinTable(name="JFireBase_RoleGroupRef_authorizedObjectRefs")
  @MapKey(name="objectID.authorizedObjectID")
  private Map<String, AuthorizedObjectRef> authorizedObjectRefs = new HashMap<String, AuthorizedObjectRef>();
}

public class RoleGroupRefID extends AbstractObjectID
{
   @Column(length=100)
   private String organisationID;

   @Column(length=100)
   private String authorityID;

   @Column(length=100)
   private String roleGroupID;
}

public class AuthorizedObjectRef
{
  @EmbeddedId
  private AuthorizedObjectRefID objectID;
}

public class AuthorizedObjectRefID extends AbstractObjectID
{
  @Column(length=100)
  private String organisationID;

  @Column(length=50)
  private String authorityID;

  @Column(length=255)
  private String authorizedObjectID;
}

This leads to the following join-table:
CREATE TABLE `test1`.`JFireBase_RoleGroupRef_authorizedObjectRefs` (
`JFireBase_RoleGroupRef_authorityID` varchar(100) NOT NULL,
`JFireBase_RoleGroupRef_organisationID` varchar(100) NOT NULL,
`JFireBase_RoleGroupRef_roleGroupID` varchar(100) NOT NULL,
`authorizedObjectRefs_authorityID` varchar(50) NOT NULL,
`authorizedObjectRefs_authorizedObjectID` varchar(255) NOT NULL,
`authorizedObjectRefs_organisationID` varchar(100) NOT NULL,
PRIMARY KEY
(
`JFireBase_RoleGroupRef_authorityID`,
`JFireBase_RoleGroupRef_organisationID`,
`JFireBase_RoleGroupRef_roleGroupID`,
`authorizedObjectRefs_authorityID`,
`authorizedObjectRefs_authorizedObjectID`,
`authorizedObjectRefs_organisationID`
),
UNIQUE KEY `authorizedObjectRefs_authorityID`
(
`authorizedObjectRefs_authorityID`,
`authorizedObjectRefs_authorizedObjectID`,
`authorizedObjectRefs_organisationID`
),

KEY `FKA9642EC17F28764D` (`JFireBase_RoleGroupRef_authorityID`,`JFireBase_RoleGroupRef_organisationID`,`JFireBase_RoleGroupRef_roleGroupID`),
KEY `FKA9642EC1818A1973` (`authorizedObjectRefs_authorityID`,`authorizedObjectRefs_authorizedObjectID`,`authorizedObjectRefs_organisationID`),
CONSTRAINT `FKA9642EC1818A1973` FOREIGN KEY (`authorizedObjectRefs_authorityID`, `authorizedObjectRefs_authorizedObjectID`, `authorizedObjectRefs_organisationID`) REFERENCES `JFireBase_AuthorizedObjectRef` (`authorityID`, `authorizedObjectID`, `organisationID`),
CONSTRAINT `FKA9642EC17F28764D` FOREIGN KEY (`JFireBase_RoleGroupRef_authorityID`, `JFireBase_RoleGroupRef_organisationID`, `JFireBase_RoleGroupRef_roleGroupID`) REFERENCES `JFireBase_RoleGroupRef` (`authorityID`, `organisationID`, `roleGroupID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


The red UNIQUE KEY is IMHO wrong. Why should I not be able to put the same AuthorizedObjectRef into multiple RoleGroupRef instances?

Now the question: Are my annotations at the field RoleGroupRef.authorizedObjectRefs wrong or is this a bug in Hibernate? If it's my fault, how do I annotate it correctly?

Best regards, Marco :-)


Top
 Profile  
 
 Post subject: Re: @OneToMany causing wrong unique key?!
PostPosted: Wed May 19, 2010 4:01 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Why should I not be able to put the same AuthorizedObjectRef into multiple RoleGroupRef instances?


Because you have mapped it as @OneToMany. You'll need to map the association as a @ManyToMany but I can't tell you exactly how to do that since I am not using annotations myself.


Top
 Profile  
 
 Post subject: Re: @OneToMany causing wrong unique key?!
PostPosted: Thu May 20, 2010 11:43 am 
Newbie

Joined: Thu Feb 12, 2004 5:42 am
Posts: 4
Location: Germany, Freiburg i.Br.
Thanks for the quick reply! Changing "One" to "Many" seems to have solved the problem!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.