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

How to bootstrap Hibernate entites in J2EE envrionment

user2961
1#
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)

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

    @Id
    private Long id;

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

PersonDao code

    @PersistenceContext
    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);
    }
}

LoginServlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println(PersonDao.getInstance().getEm());
        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"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
    version="2.1">

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

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

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

        <properties>
            <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" />
        </properties>

    </persistence-unit>

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.

Thanks

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