Search code examples
javajsfjpajakarta-eeentitymanager

issue in understanding how to create entity manager or if I can generate them in JAVA EE


I'm creating a JAVA EE project with JPA and JSF primefaces on a glassfish server.


Development environment is ECLIPSE IDE

Here is what I've done so far:

  • I Created the database in SQLServer (3tables, not that complicated structure)
  • I generated the entities from my table using JPA (eclipse offer this option...)

Here is what I want to do:

  • Generate the entity managers (session beans) for my entities so I can manage and create records in my database
  • finally I will create a UI using primefaces to display and edit and manage these records..

Question: Am I on the right path ? (conception level, or am I missing something out) + How to do my next step and that is generate the entity managers for my entities, Thanks in advance!


Solution

  • You're on the right path, but entity managers and session beans are not the same. You will use an EntityManager inside your session beans, like so:

    @Stateless
    public class MyService {
    
        @PersistenceContext
        private EntityManager em;
    }
    

    Make sure you have your persistence.xml file present. Further reading and examples can be found here.

    Example persistence.xml file that uses a container managed datasource located through JNDI at jdbc/MyOrderDB:

    <persistence>
        <persistence-unit name="OrderManagement">
            <jta-data-source>jdbc/MyOrderDB</jta-data-source>
        </persistence-unit>
    </persistence>