A join table is not required for a OneToMany you'll get foreign key column in the Many side. And this is what I get when using your code.
With the above mapping, there is no join table created. A join table is not required for a OneToMany, you'll get foreign key column in the Many side. And this is what I get when using your code: create table Person ( id bigint not null, primary key (id) ) create table Relationship ( id bigint not null, personFrom_id bigint, personTo_id bigint, primary key (id) ) alter table Relationship add constraint FK499B69164A731563 foreign key (personTo_id) references Person alter table Relationship add constraint FK499B691698EA8314 foreign key (personFrom_id) references Person Which is the expected result (at least for me).
Maybe what you actually want is a ManyToMany. When I remove the mappedBy (@OneToMany(cascade = CascadeType. PERSIST) ), the join table is created and I could persist Relationship through Person."personFrom" field is empty, but I think that is normal as the relation is maintained through the join table.
I wrote a small unit test using the provided code (with bernate's API but this doesn't change anything) and I don't get what the problem is (the session is created before the test method and the method runs inside a transaction): Person p1 = new Person(); Person p2 = new Person(); Relationship r = new Relationship(); // create the personFrom bi-directional association r. SetPersonFrom(p1); List relationships = new ArrayList(); relationships. Add(r); p1.
SetRelationships(relationships); // these four lines should be moved to some // link management method (see update below). // create the personTo uni-directional association r. SetPersonTo(p2); session.
Persist(p2); session. Persist(p1); assertNotNull(p2.getId()); assertNotNull(p1.getId()); assertNotNull(r.getId()); The above code results in two insert in the Person table and one insert in the Relationship table (valuing the 3 columns). As I said, I don't get the problem.
Maybe you should explain what the expected result is (both the relational model and the queries). Update: To be totally clear, when working with bi-directional associations, you have to set both sides of the link and a common pattern is to use defensive link management methods to correctly set both sides of the association. Something like this: public void addToRelationships(Relationship relationship) { if (this.
Relationships == null) { this. Relationships = new ArrayList(); } this.relationships. Add(relationship); relationship.
SetPersonFrom(this); } This is detailed in the section 1.2.6. Working bi-directional links of the .
I can't thank you enough, as you are saving me the second time.. I had the impression that the owning entity (Person) manages the mapped relation, so I wan't setting it explicitly. Line: r. SetPersonFrom(p1); Your test case made it clear how to persist the entity correctly thanks.
I have a similar qn about unidirectional one-to-many, which I will ask in a seperate qn.. thanks again for your time, and help. – bsreekanth Jul 28 '10 at 1:33 @bsreekanth: You're welcome :) – Pascal Thivent Jul 28 '10 at 2:06 Pascal Thivent .. I read more about JPA oneToOne mapping, and got cleared from the doubts.. I have another doubt, related to yaml syntax for bidirectional mapping.. please see it here..stackoverflow. Com/questions/3349545/… your expertise may help me.Thanks.
– bsreekanth Jul 28 '10 at 2:52.
Assuming the foreign key column is named PERSON_ID, the code should look something like this: class Relationship { @ManyToOne @JoinColumn(name="PERSON_ID") public Person personFrom; ... }.
It didn't make any difference.. all the colum except person_id is filled up correctly.. but, there are no join table. – bsreekanth Jul 27 '10 at 21:20.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.