1 Reply Latest reply on Sep 5, 2008 3:39 AM by Benoît DEL BASSO

    About best practises & the use of EntityManager

    Benoît DEL BASSO Newbie


      I am currently working on a J2EE/EJB3/Seam project and I have several questions :

      1. Sometimes I need to perform queries in EJBQL such as "select s from Service s where s.index=?" to find entities using an unique index which is not the primary key. Such queries have to be performed several times so I have created a method named findServiceUsingIndex(String index). Where is the best place to put that method ? My boss tells me I should not put it into the code of the Service entity. Any pros/cons? Shall I put it into a stateless bean? What is the best practise?

      2. One of my tasks consists in exporting data from the entities to a text file (and to import data from text files and to update the entities). The entry point of the processing is a stateless bean that has an EntityManager annotated with @PersistenceContext. Because the processing it quite complex, I have also splitted the code that processes files into many classes, packages that are only POJOs, not entities neither beans (because they do not really need to be exposed as beans). But because they are not beans, these POJOs cannot use the @PersistenceContext annotation and thus need to get the EntityManager by another way. Therefore, I have passed the EntityManager as first argument to the methods that need to find or persist entities. My boss thinks passing an EntityManager as a parameter to methods (not constructors) of -plain old- java objects is not good and may interfere with JBoss (and wants me to convert all my POJOs to beans which means creating a lot of interfaces). I think there is no problem because the objects are just POJOs, and these methods do not cache the EntityManager (they just use the EntityManager to perform their job and then forget about it) and all the process is done within one thread. Do you have any opinion on that?

      Thanks in advance for your answers.