2 Replies Latest reply on May 13, 2005 4:11 AM by epbernard

    Entity persisting itself

    messi

      Hi all!

      I have the problem that I have an object model with not-so-many entities but they have quite complex relationships with each other and a change to one entity can result in the auto-creation of quite a few others... at least that's how its supposed to work.
      Now the problem occurs that it is difficult saving the entities (because its difficult to know which ones got created).
      Generally, I have two options now: Either cascading a lot, which I fear it makes persisting slow, or entities save "themselves", probably even in the constructor.
      The question is: Can an EJB3 entity access the EntityManager? With @Inject? Can it then persist "itself"? in the constructor?
      Any hints very welcome!

      kind regards,

      Messi

        • 1. Re: Entity persisting itself
          dkrizic

          I don't think that this is a good idea.

          Why don't you simply write a Home-Interface like PersonHome for an entity called Person which has all the logic:

          @Stateless
          public class PersonHome {
           private @Inject EntityManager em;
          
           public Person persist( Person p ) {
           // logic before persist
           em.persist( p );
           // logic after persist
           return p;
           }
          }
          




          • 2. Re: Entity persisting itself
            epbernard

            EJB3 entities cannot receive injection so @Inject is forbidden in them.
            Do not assume cascading is slow until actually see it is