Professional Documents
Culture Documents
Hibernate Presentation
Hibernate Presentation
Hibernate
Hibernate is a powerful, high performance object/relational persistence and query service. Hibernate lets you develop persistent classes following object-oriented idiom - including association, inheritance, polymorphism, composition, and collections. Hibernate allows you to express queries in its own portable SQL extension (HQL), as well as in native SQL, or with an object-oriented Criteria and Example API. Unlike many other persistence solutions, Hibernate does not hide the power of SQL from you and guarantees that your investment in relational technology and knowledge is as valid as always. The LGPL open source license allows the use of Hibernate and NHibernate in open source and commercial projects.
Hibernate Annotations
Project Lead: Emmanuel BernardLatest release: 3.4.0 GA (Changelog) (Road Map)Release date: 20.08.2008Requirements: Hibernate Core 3.3.x, JDK 5.0Hibernate, like all other object/relational mapping tools, requires metadata that governs the transformation of data from one representation to the other (and vice versa). As an option, you can now use JDK 5.0 annotations for object/relational mapping with Hibernate 3.2. You can use annotations in addition to or as a replacement of XML mapping metadata. The Hibernate Annotations package includes: Standardized Java Persistence and EJB 3.0 (JSR 220) object/relational mapping annotations Hibernate-specific extension annotations for performance optimization and special mappings You can use Hibernate extension annotations on top of standardized Java Persistence annotations to utilize all native Hibernate features.
Hibernate tutorials
Various modified/new tutorials appear here. I used mysql, but that is not a requirement. Hibernate standardizes the db interface. A config file specifies the actual db service and the dialect to use, and other config files, called hbm, map the individual tables. Instead of providerspecific connection and query details, the program uses the hibernate interface to interact with the database.
Directory structure
Appdir
bin src
log4j.properties hibernate.cfg.xml Event.hbm.xml hibernate.properties --- not included in this example events(dir)
Event.java EventManager.java HibernateUtil.java
util(dir)
lib
My lib directory
<project name="hibernate-tutorial" default="compile"> <property name="sourcedir" value="${basedir}/src"/> <property name="targetdir" value="${basedir}/bin"/> <property name="librarydir" value="${basedir}/lib"/> <path id="libraries"> <fileset dir="${librarydir}"> <include name="*.jar"/> </fileset> </path> <target name="clean"> <delete dir="${targetdir}"/> <mkdir dir="${targetdir}"/> </target> <target name="compile" depends="clean, copy-resources"> <javac srcdir="${sourcedir}" destdir="${targetdir}" classpathref="libraries"/> </target> <target name="copy-resources"> <copy todir="${targetdir}"> <fileset dir="${sourcedir}"> <exclude name="**/*.java"/> </fileset> </copy> </target> <target name="run" depends="compile"> <java fork="true" classname="events.EventManager" classpathref="libraries"> <classpath path="${targetdir}"/> <arg value="${action}"/> </java> </target> </project>
build.xml
HibernateUtil.java
This class gets the hibernate session factory or throws an exception With minor variations it appears in many hibernate applications
HibernateUtil.java
package util; import org.hibernate.*; import org.hibernate.cfg.*; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { System.out.println("in try for build session factory"); // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory(); System.out.println("back from build session factory"); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); System.out.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
EventManager.java continued
private List listEvents() { System.out.println("Event Manager... in list events method"); SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session =sessionFactory.openSession(); session.beginTransaction(); List result = session.createQuery("from Event").list(); session.getTransaction().commit(); return result; } private void createAndStoreEvent(String title, Date theDate) { System.out.println("Event Manager... create and store method"); SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session =sessionFactory.openSession(); System.out.println(".. before begin transaction"); session.beginTransaction(); System.out.println(".. transaction started"); Event theEvent = new Event(); theEvent.setTitle(title); theEvent.setDate(theDate); session.save(theEvent); System.out.println("session save completed"); session.getTransaction().commit(); System.out.println("after commit in create and save"); } }
Event.java
package events; import java.util.Date; public class Event { private Long id; private String title; private Date date; //Eclipse or Netbeans will build the rest default constructor and getter/setters public Event() {} public Long getId() { return id; } private void setId(Long id) { this.id = id; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
Event.hbm.xml --such a file (and the corresponding java class) is needed for each table mappednote how primary key autoincrement primary key appears
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="events.Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <generator class="native"/> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title"/> </class> </hibernate-mapping>
hibernate.cfg.xml contains connection info, db name, user name & pw and other information
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/mydb</property> <property name="connection.username">root</property> <property name="connection.password"></property> <!-- JDBC connection pool (use the built-in) --> <property name="hibernate.connection.pool_size">10</property> <!-- MySQL dialect//different for different Database --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="Event.hbm.xml"/> </session-factory> </hibernate-configuration>
Log4j.properties
log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n log4j.rootLogger=info, stdout log4j.logger.org.hibernate.test=info log4j.logger.org.hibernate.tool.hbm2ddl=debug
remark
Youll need to create a mysql database and table for this example and you need to start the database server before running the example. Run the example in the root directory on the commandline either with ant run Daction=store Or ant run Daction=list
notes
First, of course, get the tutorial working. Modify the EventManager so it opens a JFrame with two button, list and store. Should have another button to delete * from db/table) Display stored events in a textarea. Enter data for the event in a textfield. The tutorial code uses a timestamp, but maybe something else like a string with MM/DD/YY format would be nicer.
A webapp dir
WEB-INF
web.xml
WEB-INF\web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>Event Manager</servlet-name> <servlet-class>events.EventManagerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Event Manager</servlet-name> <url-pattern>/eventmanager</url-pattern> </servlet-mapping> </web-app>
Servlet continued
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); SimpleDateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy"); PrintWriter out = response.getWriter(); out.println("<html><head><title>Event Manager</title></head><body>"); try { sessionFactory = HibernateUtil.getSessionFactory(); session =sessionFactory.openSession(); session.beginTransaction(); if ("store".equals(request.getParameter("action"))) { String eventTitle = request.getParameter("eventTitle"); String eventDate = request.getParameter("eventDate"); if ("".equals(eventTitle) || "".equals(eventDate)) { out.println("<b><i>Please enter event title and date.</i></b>"); } else { System.out.println("before call to create and store"); createAndStoreEvent(eventTitle, dateFormatter.parse(eventDate)); out.println("<b><i>Added event.</i></b>"); } } printEventForm(out); listEvents(out, dateFormatter); // End unit of work //HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit(); session.getTransaction().commit(); System.out.println("commit after save"); } catch (Exception ex) { HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback(); throw new ServletException(ex); } }
Servlet continued
private void printEventForm(PrintWriter out) { out.println("<h2>Add new event:</h2>"); out.println("<form>"); out.println("Title: <input name='eventTitle' length='50'/><br/>"); out.println("Date (e.g. 24.12.2009): <input name='eventDate' length='10'/><br/>"); out.println("<input type='submit' name='action' value='store'/>"); out.println("</form>"); } private void listEvents(PrintWriter out, SimpleDateFormat dateFormatter) { List result = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(Event.class).list(); if (result.size() > 0) { out.println("<h2>Events in database:</h2>"); out.println("<table border='1'>"); out.println("<tr>"); out.println("<th>Event title</th>"); out.println("<th>Event date</th>"); out.println("</tr>"); for (Iterator it = result.iterator(); it.hasNext();) { Event event = (Event) it.next(); out.println("<tr>"); out.println("<td>" + event.getTitle() + "</td>"); out.println("<td>" + dateFormatter.format(event.getDate()) + "</td>"); out.println("</tr>"); } out.println("</table>"); } }
Servlet continued
protected void createAndStoreEvent(String title, Date theDate) { Event theEvent = new Event(); theEvent.setTitle(title); theEvent.setDate(theDate); HibernateUtil.getSessionFactory().getCurrentSession().save(theEvent); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }
build.xml (an app located at c:\myapp would have a war called C:\myapp.war)
<project name="hibernate-tutorial" default="compile"> <property name="sourcedir" value="${basedir}/src"/> <property name="targetdir" value="${basedir}/bin"/> <property name="librarydir" value="${basedir}/lib"/> <path id="libraries"> <fileset dir="${librarydir}"> <include name="*.jar"/> </fileset> </path> <target name="clean"> <delete dir="${targetdir}"/> <mkdir dir="${targetdir}"/> </target> <target name="compile" depends="clean, copy-resources"> <javac srcdir="${sourcedir}" destdir="${targetdir}" classpathref="libraries"/> </target> <target name="copy-resources"> <copy todir="${targetdir}"> <fileset dir="${sourcedir}"> <exclude name="**/*.java"/> </fileset> </copy> </target>
Build.xml continued
<target name="run" depends="compile"> <java fork="true" classname="events.EventManager2" classpathref="libraries"> <classpath path="${targetdir}"/> <arg value="${action}"/> </java> </target> <target name="war" depends="compile"> <war destfile="hibernate-tutorial.war" webxml="web.xml"> <lib dir="${librarydir}"> <exclude name="jsdk*.jar"/> </lib> <classes dir="${targetdir}"/> </war> </target> </project>
util(dir)
HibernateUtil.class (this file same as in previous project)
Hibernate.cfg.xml
lib(dir) (no changes to this) log4j.properties(no changes to this) web.xml (shown above)
Jar
I used jar instead of ant build: jar cvf appname.war WEB-INF This creates a jar called appname.war with a dir in it named WEB-INF and all WEB-INFs contents. General syntax of jar is jar switch name-of.war list_of_files list_of_dirs Youll need to move files and directories as per web app structure before creating a war file.
Whats next?
CRUD against a database using one or more servlets, like your servlet database project, except use a hibernate interface.
My tables
My People table and my Event table both have ids as primary key, auto-incremented. In the hbm, these are identified as <id name=some_field" column=table_col"> <generator class="native"/> </id>
Dir structure
WEB-INF
log4j.properties web.xml lib (contents shown later) classes
hibernate.cfg.xml util
Same HibernateUtil.java as previous
events
Same Event.java as previous People.java EventManagerServlet.java People.hbm.xml Event.hbm.xml (unchanged from prev example)
}
// Print page printEventForm(out); System.out.println("back in process ...about to call listevents"); listEvents(out, dateFormatter); // End unit of work //HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit(); session.getTransaction().commit(); System.out.println("commit after save"); } catch (Exception ex) { System.out.println(ex.toString()); HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback(); throw new ServletException(ex); } }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("in do post"); doGet(request, response); }
}
events.People.java
public class People{ private String name; private String phone; private long id; public People() {} public long getId(){return id;} public void setId(long id){this.id=id;} public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
The primary key may be generated, native or assigned. The strategy must be specified in the hbm.xml. This is People.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="events.People" table="people"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name"/> <property name="phone"/> </class> </hibernate-mapping>
ant build still needs to be modified to build WEB-INF and classes dirs, and copy files to there, then jar
<project name="hibernate-tutorial" default="compile"> <property name="sourcedir" value="${basedir}/src"/> <property name="targetdir" value="${basedir}/bin"/> <property name="librarydir" value="${basedir}/lib"/> <path id="libraries"> <fileset dir="${librarydir}"> <include name="*.jar"/> </fileset> </path> <target name="clean"> <delete dir="${targetdir}"/> <mkdir dir="${targetdir}"/> </target> <target name="compile" depends="clean, copy-resources"> <javac srcdir="${sourcedir}" destdir="${targetdir}" classpathref="libraries"/> </target> <target name="copy-resources"> <copy todir="${targetdir}"> <fileset dir="${sourcedir}"> <exclude name="**/*.java"/> </fileset> </copy> </target> <target name="war" depends="compile"> <war destfile="HibernateApp2.war" webxml="web.xml"> <lib dir="${librarydir}"> </lib> <classes dir="${targetdir}"/> </war> </target> </project>
CRUD example
The student table
remarks
Just one table marked Link above gives good information on how to conduct queries. I used radiobuttons to indicate function desired. I used the same ant/build and jar commands as before. Same directory structure. Servlet in slide notes
Student.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="data.Student" table="Students"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name"/> <property name="age"/> <property name="gpa"/> <property name="sex"/> <property name="year"/> </class> </hibernate-mapping>
Delete action
else if ("delete".equals(request.getParameter("action"))) { String name = request.getParameter("name"); List marked=session.createQuery("from Student as student where student.name=?").setString(0,name).list(); if(marked.size()>0){ Student student=(Student)marked.get(0); out.println("deleting...."+ student.getName()); session.delete(student); } else {out.println("no match found");}
}else{//update String name = request.getParameter("name"); String gpa = request.getParameter("gpa"); String year = request.getParameter("year"); String age = request.getParameter("age"); String sex = request.getParameter("sex"); List marked=session.createQuery("from Student as student where student.name=?").setString(0,name).list();
if(marked.size()>0&&!"".equals(sex)&&!year.equals("")&&!gpa.equals("")&&!age.equal s("")) { Student student=(Student)marked.get(0); student.setAge(Integer.parseInt(age)); student.setYear(year); student.setGpa(Double.parseDouble(gpa)); student.setSex(sex); session.update(student); } else {out.println("no match found");} } /*else a getinfo choice could look about the same */
I used radiobuttons
private void printStudentForm(PrintWriter out) { out.println("<h2>Student info form:</h2>"); out.println("<form>"); out.println("Name: <input name='name' length='40'/><br/>"); out.println("Age: <input name='age' length='10'/><br/>"); out.println("Sex(Male/Female): <input name='sex' length='10'/><br/>"); out.println("Year: <input name='year' length='15'/><br/>"); out.println("GPA: <input name='gpa' length='10'/><br/>"); out.println("<input type='radio' name='action' value='list'> List<br>"); out.println(" <input type='radio' name='action' value='store' checked> Store<br>"); out.println("<input type='radio' name='action' value='update'> Update<br>"); out.println(" <input type='radio' name='action' value='delete'> Delete<br>");
List students
private void listStudents(PrintWriter out) { result1 = session.createCriteria(Student.class).list(); if (result1.size() > 0) { out.println("<h2>Student Info:</h2>"); out.println("<table border='1'>"); out.println("<tr>"); out.println("<th>Student name</th>"); out.println("<th>Student Age</th>"); out.println("<th>GPA</th>"); out.println("<th>Sex</th>"); out.println("<th>Year</th>"); out.println("</tr>"); for (Iterator it = result1.iterator(); it.hasNext();) { Student student = (Student) it.next(); out.println("<tr>"); out.println("<td>" + student.getName()+ "</td>"); out.println("<td>" + student.getAge() + "</td>"); out.println("<td>" + student.getGpa() + "</td>"); out.println("<td>" + student.getSex() + "</td>"); out.println("<td>" + student.getYear() + "</td>"); out.println("</tr>"); } out.println("</table>"); }//if }
Hibernate configuration
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/mydb</property> <property name="connection.username">root</property> <property name="connection.password"></property> <!-- JDBC connection pool (use the built-in) --> <property name="hibernate.connection.pool_size">10</property> <!-- MySQL dialect//different for different Database --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="Student.hbm.xml"/> </session-factory> </hibernate-configuration>
web-xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>StudentInfo</servlet-name> <servlet-class>data.StudentInfoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>StudentInfo</servlet-name> <url-pattern>/studentinfo</url-pattern> </servlet-mapping> </web-app>