Wu Yuxiong - natural born flyweight JAVA EE enterprise application development Struts2Sping4Hibernate Integrated Development Study Notes: Hibernate_EventFrame

<?xml version="1.0" encoding="GBK"?>
<project name="hibernate" basedir="." default="">
    <property name="src" value="src"/>
    <property name="dest" value="classes"/>

    <path id="classpath">
        <fileset dir="../../lib">
            <include name="**/*.jar"/>
        </fileset>
        <pathelement path="${dest}"/>
    </path>

    <target name="compile" description="Compile all source code">
        <delete dir="${dest}"/>
        <mkdir dir="${dest}"/>
        <copy todir="${dest}">
            <fileset dir="${src}">
                <exclude name="**/*.java"/>
            </fileset>        
        </copy>
        <javac destdir="${dest}" debug="true" includeantruntime="yes"
            deprecation="false" optimize="false" failonerror="true">
            <src path="${src}"/>
            <classpath refid="classpath"/>
            <compilerarg value="-Xlint:deprecation"/>
        </javac>
    </target>

    <target name="run" description="Run the main class" depends="compile">
        <java classname="lee.UserManager" fork="yes" failonerror="true">
            <classpath refid="classpath"/>
        </java>
    </target>

</project>
<?xml version="1.0" encoding="GBK"?>
<! - Specifies the Hibernate configuration file DTD information ->
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<-! Hibernate-configuration profile is the root element ->
<hibernate-configuration>
    <session-factory>
        <! - Specifies the database connection for driving ->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <! - url specified connection database, wherein the database is the name of the present application hibernate connection ->
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <! - Specifies the user name connected to the database ->
        <property name="connection.username">root</property>
        <! - Specifies the password for connecting to the database ->
        <property name="connection.password">32147</property>
        <! - Specifies the maximum number of connections the connection pool ->
        <property name="hibernate.c3p0.max_size">20</property>
        <! - specifies the minimum number of connections connecting pool ->
        <property name="hibernate.c3p0.min_size">1</property>
        <! - Specifies the time-out connection connected to the pool length ->
        <property name="hibernate.c3p0.timeout">5000</property>
        <! - Specifies the number of connection pool maximum cache Statement object ->
        <property name="hibernate.c3p0.max_statements">100</property>
        <property name="hibernate.c3p0.idle_test_period">3000</property>
        <property name="hibernate.c3p0.acquire_increment">2</property>
        <property name="hibernate.c3p0.validate">true</property>
        <! - Specify the database dialect ->
        <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <-! Required automatically create the database ->
        <property name="hbm2ddl.auto">update</property>
        <! - Display Hibernate persistence operations generated SQL ->
        <property name="show_sql">true</property>
        <! - SQL script will be formatted and then output ->
        <property name="hibernate.format_sql">true</property>
        <! - Session specified to define context sensitive based on the current thread ->
        <property name="hibernate.current_session_context_class">thread</property>
        <! - class lists names of all persistent classes ->
        <mapping class="org.crazyit.app.domain.User"/>
    </session-factory>
</hibernate-configuration>
package lee;

import org.hibernate.*;
import org.hibernate.internal.*;
import org.hibernate.cfg.*;
import org.hibernate.service.*;
import org.hibernate.event.spi.*;
import org.hibernate.event.service.spi.*;
import org.hibernate.boot.registry.*;

import java.util. * ;

import org.crazyit.app.domain. * ;
import org.crazyit.common.hibernate. * ;
/ **
 * Description:
 * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * Author   YeekuHLee kongyeeku163com
 * @version  1.0
 */
public class UserManager
{
    static Configuration CFG = new new Configuration ()
         // load the configuration file hibernate.cfg.xml 
        .configure ();
     // to Configuration instance to create SessionFactory instances 
    static a ServiceRegistry ServiceRegistry = new new StandardServiceRegistryBuilder ()
        .applySettings(cfg.getProperties()).build();
    static SessionFactory SF = cfg.buildSessionFactory (ServiceRegistry);
     static {
         // get the SessionFactory event listener registry 
        EventListenerRegistry the ELR = ((SessionFactoryImpl) SF)
            .getServiceRegistry () The getService (EventListenerRegistry.. class );
         // user-specified sequence in place of the interceptor interceptor save the default sequence 
        elr.setListeners (EventType.SAVE, MySaveListener. class );
         // user-specified interceptors sequence in place of the default load blocker sequence 
        elr.setListeners (EventType.LOAD, MyLoadListener. class );
    }
    public static void main(String[] args)
    {
        UserManager mgr = new UserManager();
        mgr.testUser();
        sf.close();
    }
    private void testUser()
    {
        Session session = sf.getCurrentSession();
        TX the Transaction = session.beginTransaction ();
         // Create a User object 
        User U1 = new new User ();
         // Set the basic attributes 
        u1.setName ( "crazyit.org" );
        u1.setAge(30);
        u1.setNationality ( "China" );
         // save a User object 
        Session.save (U1);
         // load a User object existing 
        User = U (User) Session.get (. User class ,. 1 );
         // change the properties 
        u.setName ( "crazy Java Alliance" );
        tx.commit();
    }
}
package org.crazyit.app.domain;

import java.util. * ;

import javax.persistence.*;
/**
 * Description:
 * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * Author   YeekuHLee kongyeeku163com
 * @version  1.0
 */
@Entity
@Table(name="user_inf")

public class User
{
    @Id @Column(name="user_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private int age;
    private String nationality;

    // no argument constructor 
    public the User ()
    {
    }
    // constructor to initialize all the member variables 
    public the User (the above mentioned id Integer, String name, int Age, String nationality)
    {
        this.id = id;
        this.name = name;
        this.age = age;
        this.nationality = nationality;
    }

    // the above mentioned id setter and getter methods 
    public  void setId (Integer the above mentioned id)
    {
        this.id = id;
    }
    public Integer getId()
    {
        return this.id;
    }

    // setter and getter method name of 
    public  void setName (String name)
    {
        this.name = name;
    }
    public String getName()
    {
        return this.name;
    }

    // Age of setter and getter methods 
    public  void setAge ( int Age)
    {
        this.age = age;
    }
    public int getAge()
    {
        return this.age;
    }

    // nationality of setter and getter methods 
    public  void setNationality (String nationality)
    {
        this.nationality = nationality;
    }
    public String getNationality()
    {
        return this.nationality;
    }

}
package org.crazyit.common.hibernate;

import org.hibernate.event.internal.DefaultLoadEventListener;
import org.hibernate.HibernateException;
import org.hibernate.event.spi.LoadEvent;
import org.hibernate.event.spi.LoadEventListener;

/**
 * Description:
 * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * Author   YeekuHLee kongyeeku163com
 * @version  1.0
 */
public class MyLoadListener extends DefaultLoadEventListener
{
    // In LoadEventListener interface just defines the method 
    public  void onLoad (LoadEvent Event,
        LoadEventListener.LoadType loadType)
        throws HibernateException
    {
        System.out.println ( "the Load event custom" );
        System.out.println(event.getEntityClassName()
            + "==========" + event.getEntityId());
        super.onLoad(event, loadType);
    }
}
package org.crazyit.common.hibernate;

import java.io.Serializable;

import org.hibernate.event.internal.DefaultSaveEventListener;
import org.hibernate.HibernateException;
import org.hibernate.event.spi.SaveOrUpdateEvent;
import org.hibernate.event.spi.LoadEventListener;

/**
 * Description:
 * <br/> website: <a href=" http://www.crazyit.org "> crazy Java league </a>
 * <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * Author   YeekuHLee kongyeeku163com
 * @Version   1.0
  * / 
// Hibernate default event listener classes are declared non-final of the. 
public  class MySaveListener the extends DefaultSaveEventListener
{
    public Serializable performSaveOrUpdate(SaveOrUpdateEvent event)
    {
        System.out.println ( "the Save custom events" );
        System.out.println(event.getObject());
        return super.performSaveOrUpdate(event);
    }
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/12370085.html