-->
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.  [ 2 posts ] 
Author Message
 Post subject: Issue with bi-directional one to many using association tabl
PostPosted: Sun Nov 16, 2008 5:40 am 
Newbie

Joined: Sun Nov 16, 2008 5:36 am
Posts: 1
Hi ,

I have a many to many relation ship between two table say a and b. Many to many relation ship is by an association table say a_b.
I am using hibernate annotations(Version 3.2)
Please find below the mapping,SQL to create table

package com.test;

import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;


@Entity
@Table(name="a")
public class A {
@Id
@Column(name="a_id")
int id;

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinTable(name="a_b",joinColumns = @JoinColumn(name="a_key"),inverseJoinColumns= @JoinColumn(name="b_key"))
Collection <B> b;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Collection<B> getB() {
return b;
}

public void setB(Collection<B> b) {
this.b = b;
}

}



package com.test;

import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;


@Entity
@Table(name="b")
public class B {
@Id
@Column(name="b_id")
int id;

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinTable(name="a_b",joinColumns = @JoinColumn (name="b_key"),inverseJoinColumns = @JoinColumn(name="a_key"))
Collection <A> a;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Collection<A> getA() {
return a;
}

public void setA(Collection<A> a) {
this.a = a;
}

}

Following are the SQL's to create the table

create table a (
a_id integer primary key
);

create table b (
b_id integer primary key
);

create table map (
a_key integer REFERENCES a(a_id),

b_key integer REFERENCES b(b_id)
);


Lets say a is mapped to 3 entities in b. So the content of the table will be

select * from a;
A_id
----
1


select * from b;
b_id
----
1
2
3

select * from a_b;

a_key b_key
----- -----
1 1
1 2
1 3


Now if i want to save a entity of A, with only one mapping , then it deletes the already existing mapping.

I.e..,

List <B> bs = new ArrayList<B>();
B b1 = new B();
b1.setId(4);
bs.add(b);

A a = new A();
a.setId(1);
a.setB(bs);



session.saveOrUpdate(a);

so it delete above mentioned 3 relations and insert only the newely defined 4th relations.


Hibernate: select a_.a_id from a a_ where a_.a_id=?
Hibernate: delete from map where a_key=?
Hibernate: insert into map (a_key, b_key) values (?, ?)


now when see the content of table a_b i get following

select * from a_b;
a_key b_key
----- -----
1 4


So my question , is there any way to retaint the older relation and add a new as above?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2008 4:10 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
What you are seeing is expected since you are creating a new list that only contains the B with id=4.

You'll have to do something like this instead (with your original code commented out):

Code:
// This will replace all existing associations.
// A a = new A();
// a.setId(1);
// a.setB(bs);
// session.saveOrUpdate(a);

// Do like this to add 'b1'
A a = (A)session.get(A.class, 1);
a.getB().add(b1);
session.getTransaction().commit();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.