finshope
6/25/2019 - 3:06 AM

Hibernate basic usage

<?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.cj.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sshdb?serverTimezone=UTC</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">1qazxsw2</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
		<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
		<property name="hibernate.current_session_context_class">thread</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		<property name="hibernate.format_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<mapping resource="com/finshope/entity/City.hbm.xml"/>
	</session-factory>
</hibernate-configuration>
package com.finshope.util;

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

public class HbnUtil {
	private static volatile SessionFactory factory;
	
	public static SessionFactory getFactory() {
		if(factory == null) {
			synchronized (HbnUtil.class) {
				if(factory == null)
					factory = new Configuration().configure().buildSessionFactory();
			}
		}
		return factory;
	}
	public static Session getSession() {
		return getFactory().getCurrentSession();
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<!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.finshope.entity.City" table="city">
		<id name="cid" column="cid">
			<generator class="native"></generator>
		</id>
		<property name="cname" column="name"></property>
	</class>
</hibernate-mapping>
package com.finshope.test;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.Before;
import org.junit.Test;

import com.finshope.entity.City;
import com.finshope.util.HbnUtil;

public class TestCity {

	Session session;

	@Before
	public void init() {
		session = HbnUtil.getSession();
	}

	@Test
	public void testInsert() {

		try {
			System.out.println("before");
			session.beginTransaction();

			session.save(new City(0, "绵阳", null));
			session.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
	}
	@Test
	public void testGet() {
		try {
			session.beginTransaction();
			City city = session.get(City.class, 7);
			System.out.println(city);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
	}
	@Test
	public void testLoad() {
		try {
			session.beginTransaction();
			//org.hibernate.ObjectNotFoundException
			City city = session.load(City.class, 7);
			System.out.println(city);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
	}
	@Test
	public void testDelete() {
		try {
			session.beginTransaction();
			City city = session.get(City.class, 3);
			System.out.println(city);
			if(city != null) {
				session.delete(city);
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
	}
	@Test
	public void testUpdate() {
		try {
			session.beginTransaction();
			City city = session.get(City.class, 4);
			
			city.setCname("自贡");
			city.setCid(2);
			System.out.println(city);
			if(city != null) {
				session.update(city);
			}
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
	}
	@Test
	public void testOpenSession() {
		try {
			SessionFactory factory = HbnUtil.getFactory();
			System.out.println(factory.openSession());
			System.out.println(factory.openSession());
			Session session = factory.openSession();
			System.out.println(session.get(City.class, 4));
			System.out.println(session.get(City.class, 5));
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testCurrentSession() {
		try {
			SessionFactory factory = HbnUtil.getFactory();
			System.out.println(factory.getCurrentSession());
			System.out.println(factory.getCurrentSession());
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	@Test
	public void testFinal() {
		final Map<String, Object> map = new HashMap<>();
		map.put("a", 1);
		System.out.println(map);
	}
	
	//C U D
	@Test
	public void testExecutionOrder() {
		try {
			session.beginTransaction();
			City city = new City();
			city.setCid(2);
			session.delete(city);
			city = new City(5, "yibing", null);
			city.setCname("yibing");
			session.update(city);
			city = new City(10, "cd", null);
			session.save(city);
			session.getTransaction().commit();
		}catch(Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
	}
}