SEQUENCE primary key generated by Hibernate

- Start
For Oracle databases, through our SEQUENCE generated by the primary key.

package shangbo.hibernate.demo007;

public class App {
	public static void main(String[] args) throws Exception {
		DataService dataService = new DataService();
		dataService.saveCustomer(new Customer("test"));
	}
}

package shangbo.hibernate.demo007;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

@Entity
@Table(name = "CUSTOMER")
public class Customer {
	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="customerId-generator")
	@SequenceGenerator(name="customerId-generator", sequenceName="CUSTOMER_ID_SEQ")
	@Column(name = "CUSTOMER_ID")
//	@Type(type = "int")
	private Integer customerId;

	@Column(name = "CUSTOMER_NAME")
//	@Type(type = "string")
	private String customerName;

	public Customer() {
	}

	public Customer(String customerName) {
		this.customerName = customerName;
	}

	public Integer getCustomerId() {
		return customerId;
	}

	// 主键自动生成,无需 setter 方法
//	public void setCustomerId(Integer customerId) {
//		this.customerId = customerId;
//	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
}

package shangbo.hibernate.demo007;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class DataService {
	public void saveCustomer(Customer customer) {
		// 第一步:构造 ServiceRegistry
		StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure(ClassLoader.getSystemResource("shangbo/hibernate/demo007/hibernate.cfg.xml")).build();

		// 第二步:构造 Metadata
		Metadata metadata = new MetadataSources(registry).buildMetadata();
		
		// 第三步:构造 SessionFactory
		SessionFactory sessionFactory = metadata.buildSessionFactory();;
		
		// 操作数据库
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(customer);
		session.getTransaction().commit();
		session.close();
		
		// 关闭 SessionFactory
		sessionFactory.close();
	}
}

<?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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">hr</property>
        <property name="connection.password">456789</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle12cDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping class="shangbo.hibernate.demo007.Customer"/>

    </session-factory>

</hibernate-configuration>

- more see: Hibernate essence
- Disclaimer: please indicate the source
- Last Updated ON 2019-06-15
- Written by ShangBo ON 2019-06-15
- End

Guess you like

Origin blog.csdn.net/shangboerds/article/details/92072989