Hello
I have an entities model of an object which holds in a many to one manner another object example:
Object A{ @Id @GenericGenerator(name = "generator", strategy = "increment") @GeneratedValue(generator = "generator") private int ID; @column(name="ColName",unique=true) private String Name; @OneToMany(fetch = FetchType.LAZY, mappedBy = "objA") private List<B> ObjsB = new List<B>(); }
Object B{ @Id @GenericGenerator(name = "generator", strategy = "increment") @GeneratedValue(generator = "generator") private int ID;
@ManyToOne(fetch = FetchType.LAZY, cascade={CascadeType.ALL}) @JoinColumn(name = "ObjA", nullable = false) private A objA; }
when I persist an instance of obj B it creates a PK-FK relation with obj A (please notice I have a cascade ALL - a table A & B should be created and table B should keep a PK-FK to table A using A's ID). this is all good, took me a while to figure this out but it works perfectly!
my question is as follows: In a situation where an instance of object B holds an instance of object A which is already present in the DB (lets say I override the compare and 2 instances of object A are equal if they have the same name) I want hibernate to figure it out and just add a row to table B with the already existing ID from table A, if it doesn't exist them I want to insert a new row to A and use its auto generated id in B
Hope I made myself clear, I can't find anything about this Any help would be greatly appreciated
Thanks!
P.S - I am using JPA 2.0 with hibernate version 3.6.0 Final
|