Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hello;
- import java.util.*;
- import org.hibernate.*;
- import persistence.HibernateUtil;
- public class Driver {
- public static void main(String[] args) {
- // First unit of work
- Session session = HibernateUtil.getSessionFactory().openSession();
- Transaction tx = session.beginTransaction();
- System.out.println("NEW OBJ");
- Message message = new Message("Hello World");
- Long msgId = (Long) session.save(message);
- tx.commit();
- session.close();
- // Second unit of work
- Session newSession = HibernateUtil.getSessionFactory().openSession();
- Transaction newTransaction = newSession.beginTransaction();
- List messages = newSession.createQuery("from Message m order by m.text asc").list();
- System.out.println(messages.size() + " message(s) found:");
- for (Iterator iter = messages.iterator(); iter.hasNext();) {
- Message loadedMsg = (Message) iter.next();
- System.out.println(loadedMsg.getText());
- }
- newTransaction.commit();
- newSession.close();
- // Shutting down the application
- HibernateUtil.shutdown();
- }
- }
- package hello;
- public class Message {
- private Long id;
- private String text;
- private Message nextMessage;
- Message() {
- }
- public Message(String text) {
- this.text = text;
- }
- public Long getId() {
- return id;
- }
- private void setId(Long id) {
- this.id = id;
- }
- public String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- public Message getNextMessage() {
- return nextMessage;
- }
- public void setNextMessage(Message nextMessage) {
- this.nextMessage = nextMessage;
- }
- }
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="accenture.agop.hibernate.Message" table="messages">
- <id name="id" column="id">
- <generator class="increment" />
- </id>
- <property name="text" column="msg_text" />
- <many-to-one name="nextMessage" cascade="all" column="next_msgId" foreign-key="FK_NEXT_MESSAGE" />
- </class>
- </hibernate-mapping>
- <project name="HelloHibernate" default="compile" basedir=".">
- <!-- Name of project and version -->
- <property name="proj.name" value="HelloHibernate" />
- <property name="proj.version" value="1.0" />
- <!-- Global properties for this build -->
- <property name="src.java.dir" value="src" />
- <property name="lib.dir" value="lib" />
- <property name="build.dir" value="bin" />
- <!-- Classpath declaration -->
- <path id="project.classpath">
- <fileset dir="${lib.dir}">
- <include name="**/*.jar" />
- <include name="**/*.zip" />
- </fileset>
- </path>
- <!-- Useful shortcuts -->
- <patternset id="meta.files">
- <include name="**/*.xml" />
- <include name="**/*.properties" />
- </patternset>
- <!-- Clean up -->
- <target name="clean">
- <delete dir="${build.dir}" />
- <mkdir dir="${build.dir}" />
- </target>
- <!-- Compile Java source -->
- <target name="compile" depends="clean">
- <mkdir dir="${build.dir}" />
- <javac srcdir="${src.java.dir}" destdir="${build.dir}" nowarn="on">
- <classpath refid="project.classpath" />
- </javac>
- </target>
- <!-- Copy metadata to build classpath -->
- <target name="copymetafiles">
- <copy todir="${build.dir}">
- <fileset dir="${src.java.dir}">
- <patternset refid="meta.files" />
- </fileset>
- </copy>
- </target>
- <!-- Run HelloWorld -->
- <target name="run" depends="compile, copymetafiles" description="Build and run HelloWorld">
- <java fork="true" classname="hello.Driver" classpathref="project.classpath">
- <classpath path="${build.dir}" />
- </java>
- </target>
- </project>
- <!DOCTYPE hibernate-configuration SYSTEM
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <!-- Database Connection Settings -->
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernateapp</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">abcd1234</property>
- <property name="hibernate.default_schema">hibernateapp</property>
- <!-- SQL dialect -->
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <!-- Use the C3P0 connection pool provider -->
- <property name="hibernate.c3p0.min_size">5</property>
- <property name="hibernate.c3p0.max_size">20</property>
- <property name="hibernate.c3p0.timeout">300</property>
- <property name="hibernate.c3p0.max_statements">50</property>
- <property name="hibernate.c3p0.idle_test_period">3000</property>
- <!-- Show and print nice SQL on stdout -->
- <property name="show_sql">true</property>
- <property name="format_sql">true</property>
- <!-- List of XML mapping files -->
- <mapping resource="accenture.agop.hibernate.message.hbm.xml" />
- </session-factory>
- </hibernate-configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement