Home How to bootstrap Hibernate entites in J2EE envrionment
Reply: 0

How to bootstrap Hibernate entites in J2EE envrionment

user2961 Published in June 23, 2018, 4:27 am

I am working on Hibernate 5.1 and J2ee structure. Here I want to know how J2EE bootstrap hibernate entities. How it creates Persistence Context.

Following is my project structure. I have PersonEntity, one PersonDao singleTon Class and one LoginServlet that called Person Dao.

PersonEntity (Please ignore setter/getter)

@Table(name = "person")
public class PersonEntity {

    private Long id;

    @Column(name = "first_name")
    private String firstName;

PersonDao code

    private EntityManager em;

    public EntityManager getEm() {
        return em;

    private static PersonDao personDao;

    public static PersonDao getInstance() {
        if (personDao == null) {
            personDao = new PersonDao();

        return personDao;

    private PersonDao() {
        System.out.println("manager::" + em);


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());

Persistence.xml file. In this no mapped class has been mentioned.

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"

    <persistence-unit name="Services">
            Persistence unit for Hibernate User Guide


        <!-- <mapping-file>file:///etc/opt/app/mappings/orm.xml</mapping-file> -->

            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />

            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5433/test" />

            <property name="javax.persistence.jdbc.user" value="postgres" />

            <property name="javax.persistence.jdbc.password" value="postgres" />

            <property name="hibernate.show_sql" value="true" />

            <property name="hibernate.hbm2ddl.auto" value="update" />


Now my concern is, Whenever servlet get the request, it returns entityManage value as null. Could some one help to understand how this bootstrapping works.Do I need to mention classes in persistence.xml file as well.


You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.3332 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO