Advertisement
Guest User

Untitled

a guest
Nov 19th, 2011
1,744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.16 KB | None | 0 0
  1. package hello;
  2.  
  3. import java.util.*;
  4. import org.hibernate.*;
  5.  
  6. import persistence.HibernateUtil;
  7.  
  8.  
  9. public class Driver {
  10.     public static void main(String[] args) {
  11.         // First unit of work
  12.         Session session = HibernateUtil.getSessionFactory().openSession();
  13.         Transaction tx = session.beginTransaction();
  14.        
  15.         System.out.println("NEW OBJ");
  16.         Message message = new Message("Hello World");
  17.         Long msgId = (Long) session.save(message);
  18.        
  19.         tx.commit();
  20.         session.close();
  21.        
  22.         // Second unit of work
  23.         Session newSession = HibernateUtil.getSessionFactory().openSession();
  24.         Transaction newTransaction = newSession.beginTransaction();
  25.        
  26.         List messages = newSession.createQuery("from Message m order by m.text asc").list();
  27.         System.out.println(messages.size() + " message(s) found:");
  28.  
  29.         for (Iterator iter = messages.iterator(); iter.hasNext();) {
  30.             Message loadedMsg = (Message) iter.next();
  31.             System.out.println(loadedMsg.getText());
  32.         }
  33.         newTransaction.commit();
  34.         newSession.close();
  35.        
  36.         // Shutting down the application
  37.         HibernateUtil.shutdown();
  38.     }
  39. }
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. package hello;
  61.  
  62. public class Message {
  63.     private Long id;
  64.     private String text;
  65.     private Message nextMessage;
  66.  
  67.     Message() {
  68.     }
  69.  
  70.     public Message(String text) {
  71.         this.text = text;
  72.     }
  73.  
  74.     public Long getId() {
  75.         return id;
  76.     }
  77.  
  78.     private void setId(Long id) {
  79.         this.id = id;
  80.     }
  81.  
  82.     public String getText() {
  83.         return text;
  84.     }
  85.  
  86.     public void setText(String text) {
  87.         this.text = text;
  88.     }
  89.  
  90.     public Message getNextMessage() {
  91.         return nextMessage;
  92.     }
  93.  
  94.     public void setNextMessage(Message nextMessage) {
  95.         this.nextMessage = nextMessage;
  96.     }
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. <?xml version="1.0"?>
  117. <!DOCTYPE hibernate-mapping PUBLIC
  118. "-//Hibernate/Hibernate Mapping DTD//EN"
  119. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  120. <hibernate-mapping>
  121.     <class name="accenture.agop.hibernate.Message" table="messages">
  122.         <id name="id" column="id">
  123.             <generator class="increment" />
  124.         </id>
  125.         <property name="text" column="msg_text" />
  126.         <many-to-one name="nextMessage" cascade="all" column="next_msgId" foreign-key="FK_NEXT_MESSAGE" />
  127.     </class>
  128. </hibernate-mapping>
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135. <project name="HelloHibernate" default="compile" basedir=".">
  136.    
  137.     <!-- Name of project and version -->
  138.     <property name="proj.name" value="HelloHibernate" />
  139.     <property name="proj.version" value="1.0" />
  140.    
  141.     <!-- Global properties for this build -->
  142.     <property name="src.java.dir" value="src" />
  143.     <property name="lib.dir" value="lib" />
  144.     <property name="build.dir" value="bin" />
  145.    
  146.     <!-- Classpath declaration -->
  147.     <path id="project.classpath">
  148.         <fileset dir="${lib.dir}">
  149.             <include name="**/*.jar" />
  150.             <include name="**/*.zip" />
  151.         </fileset>
  152.     </path>
  153.    
  154.     <!-- Useful shortcuts -->
  155.     <patternset id="meta.files">
  156.         <include name="**/*.xml" />
  157.         <include name="**/*.properties" />
  158.     </patternset>
  159.    
  160.     <!-- Clean up -->
  161.     <target name="clean">
  162.         <delete dir="${build.dir}" />
  163.         <mkdir dir="${build.dir}" />
  164.     </target>
  165.    
  166.     <!-- Compile Java source -->
  167.     <target name="compile" depends="clean">
  168.         <mkdir dir="${build.dir}" />
  169.         <javac srcdir="${src.java.dir}" destdir="${build.dir}" nowarn="on">
  170.             <classpath refid="project.classpath" />
  171.         </javac>
  172.     </target>
  173.    
  174.     <!-- Copy metadata to build classpath -->
  175.     <target name="copymetafiles">
  176.         <copy todir="${build.dir}">
  177.             <fileset dir="${src.java.dir}">
  178.                 <patternset refid="meta.files" />
  179.             </fileset>
  180.         </copy>
  181.     </target>
  182.    
  183.    
  184.     <!-- Run HelloWorld -->
  185.     <target name="run" depends="compile, copymetafiles" description="Build and run HelloWorld">
  186.         <java fork="true" classname="hello.Driver" classpathref="project.classpath">
  187.             <classpath path="${build.dir}" />
  188.         </java>
  189.     </target>
  190.  
  191.  
  192.  
  193. </project>
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. <!DOCTYPE hibernate-configuration SYSTEM
  202. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  203. <hibernate-configuration>
  204.     <session-factory>
  205.         <!--  Database Connection Settings -->    
  206.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  207.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateapp</property>
  208.         <property name="hibernate.connection.username">root</property>
  209.         <property name="hibernate.connection.password">abcd1234</property>
  210.         <property name="hibernate.default_schema">hibernateapp</property>
  211.  
  212.         <!-- SQL dialect -->        
  213.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  214.        
  215.         <!-- Use the C3P0 connection pool provider -->
  216.         <property name="hibernate.c3p0.min_size">5</property>
  217.         <property name="hibernate.c3p0.max_size">20</property>
  218.         <property name="hibernate.c3p0.timeout">300</property>
  219.         <property name="hibernate.c3p0.max_statements">50</property>
  220.         <property name="hibernate.c3p0.idle_test_period">3000</property>
  221.        
  222.         <!-- Show and print nice SQL on stdout -->
  223.         <property name="show_sql">true</property>
  224.         <property name="format_sql">true</property>
  225.        
  226.         <!-- List of XML mapping files -->
  227.         <mapping resource="accenture.agop.hibernate.message.hbm.xml" />
  228.     </session-factory>
  229. </hibernate-configuration>
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement