Is it possible to create relationships in hibernate / jpa that are retrieved when the object containing the object is retrieved, but will never result in any db updates when the saved object is saved? I will try to make the requirement clear with an example.
I have a simple object B
@Entity
public class B {
private int bId;
@Id
public int getBId() {
return bId;
}
public void setBId(int aId) {
bId = aId;
}
}
And another object A, which contains a one-to-many-to-many conversion to this class.
@Entity
public class A {
private int aId;
private List<B> bs;
@Id
public int getAId() {
return aId;
}
public void setAId(int aId) {
this.aId = aId;
}
@ManyToMany
@JoinTable(name = "A_B",
joinColumns = {@JoinColumn(name = "AID")},
inverseJoinColumns = {@JoinColumn(name = "BID")}
)
public List<B> getBs() {
return bs;
}
public void setBs(List<B> aBs) {
bs = aBs;
}
}
When object A is extracted from db and then merged as follows
A a = em.find(A.class, 1);
a.getBs().size();
em.merge(a);
merge results in the following SQL statements
Hibernate:
delete
from
A_B
where
AID=?
Hibernate:
insert
into
A_B
(AID, BID)
values
(?, ?)
Hibernate:
insert
into
A_B
(AID, BID)
values
(?, ?)
+ . , . , .
, : " " db?
Update:
, :
CREATE TABLE A (
AID INTEGER NOT NULL
)
DATA CAPTURE NONE ;
CREATE TABLE B (
BID INTEGER NOT NULL
)
DATA CAPTURE NONE ;
CREATE TABLE A_B (
AID INTEGER NOT NULL,
BID INTEGER NOT NULL
)
DATA CAPTURE NONE ;
INSERT INTO A (AID) VALUES (1);
INSERT INTO B (BID) VALUES (1);
INSERT INTO B (BID) VALUES (2);
INSERT INTO A_B (AID, BID) VALUES (1, 1);
INSERT INTO A_B (AID, BID) VALUES (1, 2);
, :
a.getBs().size();
. .