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

How to bootstrap Hibernate entites in J2EE envrionment

user7326
1#
user7326 Published in September 20, 2018, 12:51 pm

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

share|improve this question
  • What are you using for a runtime environment? If it is just a servlet container (Jetty or Tomcat for example) then none of the above will work without a bunch of other pieces, such as Weld CDI or Spring Framework. If you're using a full Java EE stack such as WildFly, WebSphere Liberty or GlassFish then there should be no bootstrapping required. – Steve C Feb 14 at 1:43

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO