-->
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.  [ 4 posts ] 
Author Message
 Post subject: Facing issue with EntityManager
PostPosted: Wed Oct 12, 2011 10:13 am 
Newbie

Joined: Wed Oct 12, 2011 10:11 am
Posts: 7
Hi Friends,

I have a Person entity and Address entity and there is @OneToMany relation b/w Person and address.

See below mapping

===================

@Entity
@Table(name="Person")
public class Person {
@Id
@GeneratedValue
@Column(name = "p_id")
private Long id;
@OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.REMOVE, CascadeType.REFRESH })
@JoinColumn(name = "p_id")
private List<Address> addressList = new ArrayList<Address>();

setters and getters...

==========================================
@Entity
@Table(name="ADDRESS")
public class Address implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;

@Id
@Column(name="ADD_ID")
@GeneratedValue
private Long id;
@Column(name="p_id")
private Long addId;

setter and getter

====================

I am creating Person object and Address objects and adding Address object to Person object like below

---------------------------------------------------
Person p1=new Person();
Address a1= new Address();
Address a1= new Address();
p1.getAddressList().add(a1);
p1.getAddressList().add(a1);

em.persist(a1);
em.flush();

Person person1 = em.find(Person.class, 1L);
System.out.println(person1.getAddressList(0).getAddId()); // getting addId value as null. here i am not closing entity manager and using same for find operation

// if close entity manager and open new entity manager then i am getting addId like 1,2

em.close();
em = entityManagerFactory().createEntityManager();

Person person1 = em.find(Person.class, 1L);

System.out.println(person1.getAddressList(0).getAddId()); // here i am getting 1
System.out.println(person1.getAddressList(1).getAddId()); // here i am getting 2

If I use same entitymanager to retrive address list from person entity addId is coming as null.
If close entity manager and open new entity manager then i am getting addId like 1,2...


Can you guys explain why jpa has implemented in this way?

Thanks in Advance... Sud


Top
 Profile  
 
 Post subject: Re: Facing issue with EntityManager
PostPosted: Thu Oct 13, 2011 3:38 am 
Newbie

Joined: Wed Oct 12, 2011 10:11 am
Posts: 7
Hi Friends...
Any help?


Top
 Profile  
 
 Post subject: Re: Facing issue with EntityManager
PostPosted: Thu Oct 13, 2011 7:52 am 
Newbie

Joined: Wed Oct 12, 2011 10:11 am
Posts: 7
Any help from this forum?


Top
 Profile  
 
 Post subject: Re: Facing issue with EntityManager
PostPosted: Mon Oct 17, 2011 7:40 am 
Newbie

Joined: Wed Oct 12, 2011 10:11 am
Posts: 7
Hi All,

I have fixed the above problem.
after persisting person object and called entityManager.refresh(personobject).
Thanks,
Sudharsana M


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