2013年11月24日 星期日

hibernate example

inf : http://www.tutorialspoint.com/hibernate/hibernate_examples.htm (good)

inf : http://www.mkyong.com/hibernate/hibernate-component-mapping-example/


Customer.java

package com.mkyong.customer;

import java.util.Date;

public class Customer implements java.io.Serializable {

private Integer custId;
private String custName;
private int age;
private Address address;
private Date createdDate;
private String createdBy;

//setters and getters
}


Address.java

package com.mkyong.customer;

public class Address implements java.io.Serializable {

private String address1;
private String address2;
private String address3;

//setters and getters
}


Customer.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="com.mkyong.customer.Customer" table="customer"
catalog="mkyongdb">

<id name="custId" type="java.lang.Integer">
<column name="CUST_ID" />
<generator class="identity" />
</id>
<property name="custName" type="string">
<column name="CUST_NAME" length="10" not-null="true" />
</property>
<property name="age" type="int">
<column name="AGE" not-null="true" />
</property>

<component name="Address" class="com.mkyong.customer.Address">
<property name="address1" type="string">
<column name="ADDRESS1" not-null="true" />
</property>
<property name="address2" type="string">
<column name="ADDRESS2" not-null="true" />
</property>
<property name="address3" type="string">
<column name="ADDRESS3" not-null="true" />
</property>
</component>

<property name="createdDate" type="date">
<column name="CREATED_DATE" length="10" not-null="true" />
</property>
<property name="createdBy" type="string">
<column name="CREATED_BY" length="10" not-null="true" />
</property>
</class>
</hibernate-mapping>


App.java 

import java.util.Date;
import org.hibernate.Session;
import com.mkyong.customer.Address;
import com.mkyong.customer.Customer;
import com.mkyong.util.HibernateUtil;

public class App {
public static void main(String[] args) {

System.out.println("Hibernate component mapping");
Session session = HibernateUtil.getSessionFactory().openSession();

session.beginTransaction();

Address address = new Address();
address.setAddress1("Address 1");
address.setAddress2("Address 2");
address.setAddress3("Address 3");

        Customer cust = new Customer();
        cust.setCustName("mkyong");
        cust.setAge(30);
        cust.setAddress(address);
        cust.setCreatedDate(new Date());
        cust.setCreatedBy("system");

        session.save(cust);

session.getTransaction().commit();
System.out.println("Done");
}
}


HibernateUtil.java

package com.mkyong.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}

}



hibernate.cfg.xml 

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mkyongdb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
    <mapping resource="com/mkyong/customer/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong.common</groupId>
<artifactId>HibernateExample</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>HibernateExample</name>
<url>http://maven.apache.org</url>

<repositories>
<repository>
<id>JBoss repository</id>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>

<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>

<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>

<!-- logback logging framework-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>0.9.28</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>0.9.28</version>
</dependency>

</dependencies>
</project>



沒有留言:

張貼留言