users@glassfish.java.net

Re: JPA Id fields???

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Fri, 17 Aug 2007 13:49:30 -0700

Without the annotation the id will be null, because it's not set. With the
annotation (depending on the strategy), you might need to call em.flush() after
persist() before asking for the generated id.

Regards,
-marina

Daniel Cavalcanti wrote:
> I tried with and without that annotation...
> The result was the same... the Id field had null!
>
> On 8/17/07, *Marina Vatkina* < Marina.Vatkina_at_sun.com
> <mailto:Marina.Vatkina_at_sun.com>> wrote:
>
> Daniel,
>
> You do not have @GeneratedValue set on the @Id field. Who is
> expected to
> populate the value?
>
> thanks,
> -marina
>
> Daniel Cavalcanti wrote:
> >
> > I have an EJB that persists, updates, removes, etc an entity...
> (create
> > using NetBeans).
> > From another EJB, BeanB, I create an entity and use the BeanA to
> persist.
> >
> > @Entity
> > @Table(name = "resource_requests")
> > public class ResourceRequests implements Serializable {
> >
> > @Id()
> > @Column(name = "id", nullable = false)
> > private BigInteger id;
> >
> > @Column(name = "start_date", nullable = false)
> > @Temporal(TemporalType.DATE)
> > private Date startDate;
> >
> > ...
> >
> > }
> >
> > @Stateless()
> > public class ResourceRequestsBean
> > implements ... {
> >
> > @PersistenceContext()
> > private EntityManager em;
> >
> > public ResourceRequestsFacade() {
> > }
> >
> > public void create(ResourceRequests entity) {
> > em.persist(entity);
> > }
> >
> > public ResourceRequests edit(ResourceRequests entity) {
> > return em.merge(entity);
> > }
> >
> > public void destroy(ResourceRequests entity) {
> > entity = em.merge(entity);
> > em.remove(entity);
> > }
> >
> > ...
> >
> > }
> >
> >
> > @Stateful()
> > public class SomeBean
> > implements ... {
> >
> > @EJB()
> > private ResourceRequestsFacadeLocal resourceRequestsFacade;
> >
> > @EJB()
> > private ResourcesFacadeLocal resourcesFacade;
> >
> > @EJB()
> > private UsersFacadeLocal usersFacade;
> >
> > public void requestValet(Users user, String ticket) {
> >
> > //
> > ******************************************************************
> > // Data tier
> > //
> > ******************************************************************
> >
> > ResourceRequests request = new ResourceRequests();
> >
> > request.setStartDate (new Date(System.currentTimeMillis()));
> > request.setResourceId(valet);
> > request.setUserId (user);
> >
> > valet.getResourceRequestsCollection().add(request);
> > user.getResourceRequestsCollection ().add(request);
> >
> > resourceRequestsFacade.create(request);
> > resourcesFacade.edit(valet);
> > usersFacade.edit(user);
> >
> > request = resourceRequestsFacade.edit(request);
> >
> > //
> > ******************************************************************
> > // Messaging tier
> > //
> > ******************************************************************
> >
> > ...
> >
> > }
> >
> > }
> >
> > Now, in the code after the Messaging tier comments, I need the
> request
> > entity id... Which is still NULL.
> > I've tried many different things, but I can't figure out how to
> get the
> > value persisted in the DB. Furthermore, I can't look that entity up
> > because I don't have the PK?!?
> >
> > How can I do that?
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> <mailto:users-unsubscribe_at_glassfish.dev.java.net>
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
> <mailto:users-help_at_glassfish.dev.java.net>
>
>