-->
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: Mapping beteween 2 tables
PostPosted: Thu Feb 25, 2010 6:01 pm 
Newbie

Joined: Sat Dec 05, 2009 8:08 pm
Posts: 3
Hello everyone,
I hope somebody can help me to resolve my issue.
I have this situation ,
My table User has a primary key form with 2 columns (organizationName,userId).
My table Role has a primary key form with 2 columns (organizationName,roleName)

Now , here is the problem , I need a relation beteween User and Role , we have called USER_ROLE
and his fields has to be organizationName,userId,roleId
So , in the User's Entity , I have a relation defined like this.

@OneToMany(targetEntity=Role.class)
@JoinTable(name="QF_USER_ROLE",
joinColumns={
@JoinColumn(name="UorganizationName", referencedColumnName="organizationName"),
@JoinColumn(name="userId", referencedColumnName="userId")},
inverseJoinColumns={
@JoinColumn(name="organizationName", referencedColumnName="organizationName"),
@JoinColumn(name="roleName", referencedColumnName="roleName")}
)
private Set<Role> roles;

And in the Role's Entity I have defined the relation ManyToMay like this
@ManyToMany(targetEntity=User.class, mappedBy="roles")
private Set<User> users;


Now I have gotten this Exception when we run the application.
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.quickflows.metadata.test.suite.TestSuite4InitialDB
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
javax.persistence.PersistenceException: [PersistenceUnit: QuickFlowsPU] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
at com.quickflows.metadata.JPAUtil.<clinit>(JPAUtil.java:33)
at com.quickflows.metadata.test.suite.QuickFlowsMetadata.initialize(QuickFlowsMetadata.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: com.quickflows.metadata.entities.Role.users column: organizationName
at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:329)
at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:352)
at org.hibernate.mapping.Collection.validate(Collection.java:309)
at org.hibernate.mapping.Set.validate(Set.java:42)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1153)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1334)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)


Top
 Profile  
 
 Post subject: Re: Mapping beteween 2 tables
PostPosted: Fri Feb 26, 2010 3:01 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
My idea is define both of the fields as unique key, dan put primary key.
Code:
@Entity
@Table(name = "user", uniqueConstraints = @UniqueConstraint(columnNames = {
      "organizationName", "userId" }))
class User implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.Auto)
    private Long id;
}


I think this way is easier.


Top
 Profile  
 
 Post subject: Re: Mapping beteween 2 tables
PostPosted: Wed Mar 03, 2010 5:01 am 
Newbie

Joined: Wed Mar 03, 2010 4:31 am
Posts: 1
Location: usa
Today, I visited this forum so I found that it's very intersted for me.. there are many topics available here so I like this so much.....
I hope that you will read care fully and tell me more benifits about this .........

_________________
Acai Optimum
Acai Optimum


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.