-->
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 M-N relationship with additional data in join table
PostPosted: Wed Mar 19, 2008 3:02 pm 
Newbie

Joined: Tue Apr 18, 2006 6:43 pm
Posts: 11
HEllo, im using JPA (EJB3) for a website where i need to map the relationship between users.

I have the old User class:

- User
+ Long id
+ String name
+ Set<UserRelationship> userRelationships

a UserRelationship class:

- UserRelationship
+ User relatedUser;
+ User relatingUser;
+ int type;

facts:

1) the type can be blocked, friend, awaiting friend approval, denied, so an Enum (Typesafe) could be useful here
2) relatedUser+relatedUser+type should be unique to insure data consistency

3) for a user i need to know:
3.1) which users are blocked
3.2) which users are friends
3.2) which users are awaiting friendship request approval

I have read the docs and tried mapping this several times with no sucess using @ManyToMany, @OneToMany, etc ....

help? ate least a starting point.

thank you


Top
 Profile  
 
 Post subject: Re: Mapping M-N relationship with additional data in join ta
PostPosted: Tue Mar 25, 2008 12:31 pm 
Newbie

Joined: Mon Mar 24, 2008 10:29 am
Posts: 2
josemoreira wrote:
HEllo, im using JPA (EJB3) for a website where i need to map the relationship between users.

I have the old User class:
[..]
a UserRelationship class:


Hi!

I also have basically the same problem with JPA and Hibernate.
I've got a class to store architectural information.

The architecture class has an id, title, description, styleType and additional data that is optional and only added to a additional table if required (formal architecture description).

The architecture styleType can be "general-purpose", "abstract" and "concrete", so I use the Java Enum and "@Enumerated(EnumType.STRING)" as JPA Annotation.
The different architectures are then referencing each other, but it's not a simple "an archicitecture has only one parent". One concreate architecture can be linked as "loose extension" to one general-purpose object and the same concrete architecture can be an "strictly inherited" from another general-purpose object.
It's an m:n referencing object.

Without JPA/Hibernate I would just create two tables, one for the architecture objects and another table for the relationship information.
As a general-purpose style object has no "parent" there will be no entry in the ArchitectureRef table. A style can also not reference itself but that are things that I take care of in my application.

Architectures table columns:
id, title, description and styleType (which is an Enum that can be: "general-purpose", "abstract" and "concrete")

ArchitectureRef table columns:
architectureId, architectureParentId and typeOfReference

TypeOfReference is again an Enum that can be: "strict", "variant", "loose", "builds-upon".

The big question is how to define this in a Java POJO with JPA-Annotations using Hibernate?
Can this be done using "set", "collection",..? If yes, how?


All books and other resources I searched so far do not cover the problem with these referenced objects with more then one parent.
As my problem described above is basically the same problem that josemoreira described I hope someone shows us an example class to do this.

Many thanks in advance.

[Edit1] Fixed some typos


Top
 Profile  
 
 Post subject: Re: Mapping M-N relationship with additional data in join table
PostPosted: Tue Dec 01, 2009 11:40 am 
Newbie

Joined: Tue Dec 01, 2009 11:25 am
Posts: 1
Hello!

I have the same problem as the two guys above.

I want to have a many-to-many relationship on the same table. And also for every link I need an additional column storing some other information. I know from my current experience with Hibernate, that a "middle" table must be created. It should contain two ids pointing to the same table. Also other columns can be added for the additional information. But I am coming short at writing the mapping annotations. The thing which bothers me is that the mapping is to the same table. I could solve the problem if it were two different tables.

I searched a lot on the Internet and couldn't find a solution.

Hope you will help me.

Regards,
Petar


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.