Hi Greg,
Does the person have its relationship to the address instantiated
at any point of time?
Also, what is the purpose of finding the address and detaching it
right away by closing the EM?
thanks,
-marina
Greg Ederer wrote On 01/06/07 23:00,:
> Hi,
>
> I have an entity called Person that has a list of Address entities. In
> Person, I have:
>
> @OneToMany(cascade=CascadeType.ALL, mappedBy="person")
> private List<Address> addresses = new ArrayList<Address>();
>
> public void addAddress(Address address)
> {
> if(this.getAddresses().contains(address))
> {
> return;
> }
>
> getAddresses().add(address);
> address.setPerson(this);
> }
>
> In Address, I have:
>
> @ManyToOne
> @JoinColumn(name="person", nullable = false)
> private Person person;
>
> In another class, I modify a Person instance, and also modify its owned
> Address instances, and then merge the Person instance. Afterward, the
> changes to the Person instance are reflected in the database, as
> expected; however, changes to the Address instances do not appear in the
> database.
>
> I get a reference to each Address via EntitManager.find(), like so:
>
> EntityManager em = getEntityManager();
> Object o = em.find(javaType, id);
> em.close();
>
> (where javaType is a Person Class instance) rather than retrieving from
> the Person object graph. Does this matter?
>
> I then merge the Person by calling the following method:
>
> private Object saveObject(Object o)
> {
> EntityManager em = getEntityManager();
> try
> {
> utx.begin();
> if(PropertyUtils.getProperty(o, "id") == null)
> {
> em.persist(o);
> }
> else
> {
> o = em.merge(o);
> }
> utx.commit();
> }
> catch (Exception ex)
> {
> //logger.severe("save failed.");
> ex.printStackTrace();
>
> try
> {
> utx.rollback();
> }
> catch (Exception e)
> {
> //logger.severe("rollback falied.");
> e.printStackTrace();
> }
> }
> finally
> {
> em.close();
> }
>
> return o;
> }
>
> My Person instance does have an id, so em.merge(o) should get executed.
> Also, utx is a UserTransaction obtained via @Resource.
>
> Can anyone tell me how to get this working?
>
> Thanks!
>
> Greg
>