Hibernate 通过 SEQUENCE 生成主键

– Start
对于 Oracle 数据库来说,通过我们通过 SEQUENCE 生成主键。

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>

– 更多参见:Hibernate 精萃
– 声 明:转载请注明出处
– Last Updated on 2019-06-15
– Written by ShangBo on 2019-06-15
– End

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值