You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.
| Online: | 11 Members of 4089 |
| Forum: Seam Users |
09. Jul 2008, 23:02 CET | Link |
Hi all,
This question might be more of a jpa question, but here goes...
I have two entities that are mapped using @ManyToMany. Works great. However, I'm not sure what to do if I want to add an additional property (column) to the mapping table created by my jpa provider to represent the many-to-many relationship.
Any help is much appreciated.
Rick
Well, I know how to do this using Hibernate. I'm not sure where JPA and Hibernate annotations overlap exactly, but...
Also, I'm kinda new, but your question is one of things that I've successfully transitioned from the ConceptState.DONOTUNDERSTANDTHAT state to ConceptState.CANMAKETHATWORK state in my head. :P
So, you can either make an intermediate entity class for the join table and map it to your 2 entities with onetomany associations, or you can make a collection of components with with a value-type class for the join table. (There may be other ways)
I did it the 2nd way. It involved making an Embeddable entity for my join table. One of the related tables was the @Parent, because I would mostly be using unidirectional navigation...
So I had something like this for my link table:
@Embeddable public class Question_Guy{ @Parent // hibernate annotation (optional), gives a backpointer to the link object private Guy guy; @ManyToOne @JoinColumn(name="question_id", nullable=false, updatable=false) private Question question; @Temporal(TemporalType.TIMESTAMP) @Column(name="dateadded", nullable=false) private Date dateAdded; public Question_Guy{Question questionIn, Guy guyIn){ this.question = questionIn; this.guy = guyIn; this.dateAdded = //get current date }And then the Guy class...
... @CollectionOfElements //hibernate annotation @JoinTable(name="question_guy", joinColumns = @JoinColumn(name="guy_id")) private Set<Question_Guy> question_guys = new HashSet<Question_Guy>(); ... public void addQuestion(Question questionIn){ Question_Guy link = new Question_Guy(questionIn, this); question_guys.add(link);So when I call addQuestion(Question), some magical process puts the question into the database with the appropriate date in the extra column.
But there are other solutions, and I won't be able to explain the pros and cons very well. I just wanted to show off that I know something, mostly :P
And sorry that it's using Hibernate, I learned all this from the Hibernate docs.
Rick