Hibernate

From Resin 3.0

(Difference between revisions)
Jump to: navigation, search
(New page: Hibernate with the JPA (EntityManager) interface requires Resin 3.1.4 or later. You will need to copy the jars from both hibernate and hibernate-entitymanager. The persistence.xml in MET...)
 
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Hibernate with the JPA (EntityManager) interface requires Resin 3.1.4 or later.
+
== Hibernate and Resin ==
  
You will need to copy the jars from both hibernate and hibernate-entitymanager.  The persistence.xml in META-INF/persistence.xml will need to specify the HibernatePersistence provider, e.g.
+
This example shows how to use Hibernate EntityManager (Hibernate's JPA implementation) with Resin IoC.
  
  <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
+
You'll need a running database in order to run this exampleWe use MySQL here, but any database with a JDBC driver should work.
    <persistence-unit name="test">
+
   
+
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
+
+
    <jta-data-source>jdbc/database</jta-data-source>
+
+
    <properties>
+
        <property name="hibernate.transaction.manager_lookup_class"
+
            value="org.hibernate.transaction.ResinTransactionManagerLookup"/>
+
    </properties>
+
  </persistence>
+
  
Your servlet or WebBean can use @In EntityManagerFactory (or @Named("test") or @PersistenceUnit(unitName="test")) to get the EntityManager
+
(The following has been tested with Resin 3.1.5, Hibernate Core 3.2 )
  
import javax.persistence.*;
+
<ol>
import javax.transaction.*;
+
<li>Download Resin from http://www.caucho.com/download</li>
import javax.webbeans.*;
+
<li>Unzip Resin into <code>/usr/local/share/resin</code></li>
+
<li>Download the MySQL JDBC driver from http://www.mysql.com/products/connector/j/</li>
public class MyServlet extends GenericServlet {
+
<li>Copy the MySQL JDBC driver jar to <code>/usr/local/share/resin/lib</code></li>
    @Named("test") EntityManagerFactory _factory;
+
<li>Create the Hibernate webapp directory structure:
    @In UserTransaction _ut;
+
<pre>mkdir -p /usr/local/share/resin/webapps/hibernate/WEB-INF/lib/
+
mkdir -p /usr/local/share/resin/webapps/hibernate/WEB-INF/classes/example/
    ...
+
mkdir -p /usr/local/share/resin/webapps/hibernate/WEB-INF/classes/META-INF/
 +
</pre>
 +
</li>
 +
<li>Download Hibernate Core ''and'' EntityManager from http://www.hibernate.org/</li>
 +
<li>Unzip both Hibernate Core and EntityManager</li>
 +
<li>Copy all the jars included the Hibernate distributions to
 +
<br/><code>/usr/local/share/resin/webapps/hibernate/WEB-INF/lib/</code>
 +
</li>
 +
<li>Initialize the database by executing the following SQL statements:
 +
<pre>
 +
CREATE DATABASE Hibernate;
 +
CREATE TABLE Hibernate.basic_courses (id INTEGER PRIMARY KEY auto_increment, course VARCHAR(250), teacher VARCHAR(250));
 +
INSERT INTO Hibernate.basic_courses VALUES('1', 'Potions', 'Severus Snape');
 +
INSERT INTO Hibernate.basic_courses VALUES('2', 'Transfiguration', 'Minerva McGonagall');
 +
</pre>
 +
</li>
 +
<li>Create the configuration file <code>/usr/local/share/resin/webapps/hibernate/WEB-INF/resin-web.xml</code>
 +
<pre><?xml version="1.0" encoding="UTF-8"?>
 +
<web-app xmlns="http://caucho.com/ns/resin">
 +
  <!-- Make the database accessible to Hibernate -->
 +
  <database>
 +
    <jndi-name>jdbc/hibernate</jndi-name>
 +
    <driver type="org.gjt.mm.mysql.Driver">
 +
      <url>jdbc:mysql://localhost:3306/Hibernate</url>
 +
      <user>root</user>
 +
      <password/>
 +
    </driver>
 +
  </database>
 +
 
 +
  <!-- Set up the example servlet -->
 +
  <servlet servlet-name="course-servlet" servlet-class="example.CourseServlet"/>
 +
  <servlet-mapping url-pattern="/*" servlet-name="course-servlet"/>
 +
</web-app>
 +
</pre>
 +
</li>
 +
 
 +
<li>Create the JPA configuration file <code>/usr/local/share/resin/webapps/hibernate/WEB-INF/classes/META-INF/persistence.xml</code>
 +
<pre><?xml version="1.0" encoding="UTF-8"?>
 +
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
 +
  <persistence-unit name="example">
 +
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
 +
 
 +
    <jta-data-source>jdbc/hibernate</jta-data-source>
 +
 
 +
    <class>example.CourseBean</class>
 +
 
 +
    <exclude-unlisted-classes/>
 +
 
 +
    <properties>
 +
      <property name="hibernate.transaction.manager_lookup_class"
 +
          value="org.hibernate.transaction.ResinTransactionManagerLookup"/>
 +
    </properties>
 +
 
 +
  </persistence-unit>
 +
</persistence>
 +
</pre>
 +
</li>
 +
 
 +
<li>Create a JPA persisted data bean file <code>/usr/local/share/resin/webapps/hibernate/WEB-INF/classes/example/CourseBean.java</code>
 +
<pre>package example;
 +
 
 +
import javax.persistence.*;
 +
 
 +
@Entity@Table(name="basic_courses")
 +
public class CourseBean {
 +
  private int _id;
 +
  private String _course;
 +
  private String _teacher;
 +
 
 +
  @Id@Column(name="id")
 +
  @GeneratedValue
 +
  public int getId()
 +
  {
 +
    return _id;
 
   }
 
   }
 +
 +
  public void setId(int id)
 +
  {
 +
    _id = id;
 +
  }
 +
 +
  @Basic
 +
  public String getCourse()
 +
  {
 +
    return _course;
 +
  }
 +
 +
  public void setCourse(String course)
 +
  {
 +
    _course = course;
 +
  }
 +
 +
  @Basic
 +
  public String getTeacher()
 +
  {
 +
    return _teacher;
 +
  }
 +
 +
  public void setTeacher(String teacher)
 +
  {
 +
    _teacher = teacher;
 +
  }
 +
}
 +
</pre>
 +
</li>
 +
<li>Create the example servlet
 +
<code>/usr/local/share/resin/webapps/mule/WEB-INF/classes/example/CourseServlet.java</code>
 +
<pre>package example;
 +
 +
import java.io.*;
 +
import javax.servlet.*;
 +
import javax.servlet.http.*;
 +
import javax.persistence.*;
 +
 +
public class CourseServlet extends HttpServlet
 +
{
 +
  // Resin IoC will inject this
 +
  @PersistenceContext(unitName="example")
 +
  private EntityManager _manager;
 +
 +
  public void service(HttpServletRequest request, HttpServletResponse response)
 +
    throws IOException, ServletException
 +
  {
 +
    PrintWriter out = response.getWriter();
 +
    response.setContentType("text/html");
 +
 +
    out.println("EntityManager = " + _manager + "<br/>");
 +
 +
    CourseBean []course = new CourseBean[2];
 +
 +
    course[0] = _manager.find(CourseBean.class, new Integer(1));
 +
    course[1] = _manager.find(CourseBean.class, new Integer(2));
 +
 +
    out.println("Course Details<br/><br/>");
 +
 +
    for (int i = 0; i < course.length; i++) {
 +
      out.println("course: " + course[i].getCourse() + "<br/>");
 +
      out.println("teacher: " + course[i].getTeacher() + "<br/>");
 +
    }
 +
  }
 +
}
 +
</pre>
 +
</li>
 +
<li>Start Resin with <code>java -jar /usr/local/share/resin/lib/resin.jar</code></li>
 +
<li>Look at http://localhost:8080/hibernate
 +
<br/>It should show
 +
<pre>
 +
EntityManager = org.hibernate.ejb.EntityManagerImpl@113cf49
 +
Course Details
 +
 +
course: Potions
 +
teacher: Severus Snape
 +
 +
course: Transfiguration
 +
teacher: Minerva McGonagall
 +
</pre>
 +
</ol>
 +
Compare this example to http://www.caucho.com/resin/examples/amber-basic/index.xtp

Latest revision as of 17:50, 17 March 2010

Hibernate and Resin

This example shows how to use Hibernate EntityManager (Hibernate's JPA implementation) with Resin IoC.

You'll need a running database in order to run this example. We use MySQL here, but any database with a JDBC driver should work.

(The following has been tested with Resin 3.1.5, Hibernate Core 3.2 )

  1. Download Resin from http://www.caucho.com/download
  2. Unzip Resin into /usr/local/share/resin
  3. Download the MySQL JDBC driver from http://www.mysql.com/products/connector/j/
  4. Copy the MySQL JDBC driver jar to /usr/local/share/resin/lib
  5. Create the Hibernate webapp directory structure:
    mkdir -p /usr/local/share/resin/webapps/hibernate/WEB-INF/lib/
    mkdir -p /usr/local/share/resin/webapps/hibernate/WEB-INF/classes/example/
    mkdir -p /usr/local/share/resin/webapps/hibernate/WEB-INF/classes/META-INF/
    
  6. Download Hibernate Core and EntityManager from http://www.hibernate.org/
  7. Unzip both Hibernate Core and EntityManager
  8. Copy all the jars included the Hibernate distributions to
    /usr/local/share/resin/webapps/hibernate/WEB-INF/lib/
  9. Initialize the database by executing the following SQL statements:
    CREATE DATABASE Hibernate;
    CREATE TABLE Hibernate.basic_courses (id INTEGER PRIMARY KEY auto_increment, course VARCHAR(250), teacher VARCHAR(250));
    INSERT INTO Hibernate.basic_courses VALUES('1', 'Potions', 'Severus Snape');
    INSERT INTO Hibernate.basic_courses VALUES('2', 'Transfiguration', 'Minerva McGonagall');
    
  10. Create the configuration file /usr/local/share/resin/webapps/hibernate/WEB-INF/resin-web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://caucho.com/ns/resin">
      <!-- Make the database accessible to Hibernate -->
      <database>
        <jndi-name>jdbc/hibernate</jndi-name>
        <driver type="org.gjt.mm.mysql.Driver">
          <url>jdbc:mysql://localhost:3306/Hibernate</url>
          <user>root</user>
          <password/>
        </driver>
      </database>
    
      <!-- Set up the example servlet -->
      <servlet servlet-name="course-servlet" servlet-class="example.CourseServlet"/>
      <servlet-mapping url-pattern="/*" servlet-name="course-servlet"/>
    </web-app>
    
  11. Create the JPA configuration file /usr/local/share/resin/webapps/hibernate/WEB-INF/classes/META-INF/persistence.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
      <persistence-unit name="example">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
    
        <jta-data-source>jdbc/hibernate</jta-data-source>
    
        <class>example.CourseBean</class>
    
        <exclude-unlisted-classes/>
    
        <properties>
           <property name="hibernate.transaction.manager_lookup_class"
               value="org.hibernate.transaction.ResinTransactionManagerLookup"/>
        </properties>
      
      </persistence-unit>
    </persistence>
    
  12. Create a JPA persisted data bean file /usr/local/share/resin/webapps/hibernate/WEB-INF/classes/example/CourseBean.java
    package example;
    
    import javax.persistence.*;
    
    @Entity@Table(name="basic_courses")
    public class CourseBean {
      private int _id;
      private String _course;
      private String _teacher;
    
      @Id@Column(name="id")
      @GeneratedValue
      public int getId()
      {
        return _id;
      }
    
      public void setId(int id)
      {
        _id = id;
      }
    
      @Basic
      public String getCourse()
      {
        return _course;
      }
    
      public void setCourse(String course)
      {
        _course = course;
      }
    
      @Basic
      public String getTeacher()
      {
        return _teacher;
      }
    
      public void setTeacher(String teacher)
      {
        _teacher = teacher;
      }
    }
    
  13. Create the example servlet /usr/local/share/resin/webapps/mule/WEB-INF/classes/example/CourseServlet.java
    package example;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.persistence.*;
    
    public class CourseServlet extends HttpServlet
    {
      // Resin IoC will inject this
      @PersistenceContext(unitName="example")
      private EntityManager _manager;
    
      public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
      {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
    
        out.println("EntityManager = " + _manager + "<br/>");
    
        CourseBean []course = new CourseBean[2];
    
        course[0] = _manager.find(CourseBean.class, new Integer(1));
        course[1] = _manager.find(CourseBean.class, new Integer(2));
    
        out.println("Course Details<br/><br/>");
    
        for (int i = 0; i < course.length; i++) {
          out.println("course: " + course[i].getCourse() + "<br/>");
          out.println("teacher: " + course[i].getTeacher() + "<br/>");
        }
      }
    }
    
  14. Start Resin with java -jar /usr/local/share/resin/lib/resin.jar
  15. Look at http://localhost:8080/hibernate
    It should show
    EntityManager = org.hibernate.ejb.EntityManagerImpl@113cf49
    Course Details
    
    course: Potions
    teacher: Severus Snape
    
    course: Transfiguration
    teacher: Minerva McGonagall
    

Compare this example to http://www.caucho.com/resin/examples/amber-basic/index.xtp

Personal tools