persistence@glassfish.java.net

Re: Persisting via cascading

From: Gordon Yorke <gordon.yorke_at_oracle.com>
Date: Thu, 26 Nov 2009 09:23:45 -0400

Identity type sequences are not assigned until a flush occurs or a
commit of the transaction as it requires an insert to the database for
the sequence value to be assigned. If you need the ID up front you can
change to table sequencing or issue a flush().
--Gordon

Gary Jacobson wrote:
> Gordon: the child's id is an int with GenerationType.IDENTITY (I am
> using MS SQL Server) which it inherits from a superclass with
> InheritanceType.JOINED.
>
> Chris: Yes, I can get the id of the child from the parent's childList,
> but I have quite a lot of code in my project that assumes that the
> "original" entities will have their id's set, so unless I can find a
> quick fix, I will have to stick with Toplink Essentials.
>
> Thanks
> Gary
>
>
>
> ---------- Forwarded message ----------
> From: christopher delahunt <christopher.delahunt_at_oracle.com
> <mailto:christopher.delahunt_at_oracle.com>>
> To: persistence_at_glassfish.dev.java.net
> <mailto:persistence_at_glassfish.dev.java.net>
> Date: Mon, 23 Nov 2009 10:05:57 -0500
> Subject: Re: Persisting via cascading
> Can you Check if parent.getChildList().get(x) where x is the index
> of the child you added has its Id set? Merge should return a
> managed instance of your child object, so if you are using the
> reference to the child you passed into merge, it will be different
> and may not have its id set. The parent object returned from
> merge though should reference the managed child which has its
> primary key set.
>
> Best Regards
> Chris
>
> Gordon Yorke wrote:
>
> This should work. Is the child's ID generated? What
> Generator Type is used? Is the ID a primitive value or an
> Object wrapper?
> --Gordon
>
> Gary Jacobson wrote:
>
> I recently attempted to switch from Toplink Essentials to
> Eclipselink and encountered a problem. I have some code
> which looks like this:
>
> // create new child entity
> Child child = new Child();
>
> // add it to the parent entity, which has @OneToMany(
> cascade = CascadeType.ALL ) on its childList
> child.setParent( parent );
> parent.getChildList().add( child );
>
> // now I hand off to a stateless session bean which merges
> the parent using em.merge()
> parent = myFacade.merge( parent );
>
> // retrieve child id
> log( child.getId() );
>
> At this point, using Toplink, I could get the new child's
> id. However, using Eclipselink, it returns 0, despite the
> fact that the child was successfully created and added to
> the parent. Is this a bug, or is there some sort of best
> practice I'm not using? I'm hoping to avoid using any
> Eclipselink-specific code.
>
> Thanks
> Gary
>
>
>