Getting started with hibernate

1: hibernate.cfg.xml configuration file

<?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>
<!-- The name of the property can be found in the Hibernate distribution project\etc\hibernate.properties. The hibernate prefix of the parameter name can be omitted -->
<!-- database connection configuration -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection. url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="

<property name="connection.pool_size">10</property>
<!-- database dialect-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- second level cache Configuration: Temporarily close -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- console prints the SQL statement executed by Hibernate -->
<property name="show_sql" >true</property>
<!-- Display the sql statement in an easy-to-read form-->
<property name="hibernate.format_sql">false</property>
<!-- Automatically generate DDL:Data Definition Language to define table structure etc -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/anrongtec/domain/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

2:Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- dtd文件在:hibernate3.jar\org\hibernate\hibernate-mapping-3.0.dtd -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

 

<hibernate-mapping>
<class name="com.anrongtec.domain.Customer" table="CUSTOMERS">
<!-- mapping primary key-->
<id name="id" column="ID">
<!-- Primary key generation strategy: For now, remember to use native -->
<generator class="native"></generator>
</id>
<!-- Map the relationship between attributes in the class and database table fields -->
<property name ="name" column="NAME" type="string" length="100"></property>
<property name="gender" column="GENDER" type="boolean"></property>
<property name= "birthday" column="BIRTHDAY"></property>
</class>
</hibernate-mapping>

3: Test class.

package com.anrongtec.test;

import java.util.Date;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import org.junit.Test;

import com.anrongtec.domain.Customer;

public class CustomerTest {
//The way to load the configuration file.
@Test
public void saveCustomer() {
Customer cs = new Customer(01, "Cao Xiaoyang", true, new Date());
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory( );
Session openSession = sessionFactory.openSession();
Transaction beginTransaction = openSession.beginTransaction();
openSession.save(cs);
beginTransaction.commit();
openSession.close();
sessionFactory.close();
// insert into CUSTOMERS (NAME, GENDER, BIRTHDAY) values ​​(?, ?, ?)
}
//Configuration loads the configuration file,
public void saveTest() {
//Create an object
Customer cs = new Customer(01, "caoxiaoyang", true, new Date());
//Customer c = new Customer("caoxiaoyang", false, new Date());
// Initialize Hibernate's configuration file
Configuration cfg = new Configuration();
cfg.configure();// Load hibernate.cfg.xml configuration file
// Create SessionFactory factory
SessionFactory sessionFactory = cfg.buildSessionFactory();
// Get Session object: core.Hibernate operations are based on session
Session session = sessionFactory.openSession();
// Open transaction
Transaction tx = session.beginTransaction();// start transaction
session.save(cs);// Save entity object
tx.commit();// Commit transaction
// Release occupied resources
session.close();
sessionFactory.close();
}
public void save10(){
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");// Load hibernate.cfg.xml configuration file
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324482534&siteId=291194637