Hibernate
From Resin 3.0
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 )
- Download Resin from http://www.caucho.com/download
- Unzip Resin into
/usr/local/share/resin
- Download the MySQL JDBC driver from http://www.mysql.com/products/connector/j/
- Copy the MySQL JDBC driver jar to
/usr/local/share/resin/lib
- 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/
- Download Hibernate Core and EntityManager from http://www.hibernate.org/
- Unzip both Hibernate Core and EntityManager
- Copy all the jars included the Hibernate distributions to
/usr/local/share/resin/webapps/hibernate/WEB-INF/lib/
- 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');
- 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>
- 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>
- 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; } }
- 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(name="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/>"); } } }
- Start Resin with
java -jar /usr/local/share/resin/lib/resin.jar
- Look at http://localhost:8080/hibernate
It should showEntityManager = 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