Hibernate
From Resin 3.0
(Difference between revisions)
(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...) |
(Complete rewrite) |
||
| Line 1: | Line 1: | ||
| − | Hibernate | + | == 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 ) | |
| − | + | <ol> | |
| − | + | <li>Download Resin from http://www.caucho.com/download</li> | |
| − | + | <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> | |
| − | + | <li>Copy the MySQL JDBC driver jar to <code>/usr/local/share/resin/lib</code></li> | |
| − | + | <li>Create the Hibernate webapp directory structure: | |
| − | + | <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(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/>"); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </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> | ||
| + | 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 | ||
Revision as of 17:50, 19 February 2008
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.javapackage 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.javapackage 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 showCourse 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