-->
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.  [ 1 post ] 
Author Message
 Post subject: JPA with hibernate persist question
PostPosted: Wed Dec 01, 2010 6:38 am 
Newbie

Joined: Wed Dec 01, 2010 6:15 am
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.