7.7 Hibernate:内置生成器 – native
来源:互联网 发布:宝马刷隐藏软件 编辑:程序博客网 时间:2024/05/22 07:09
Hibernate 根据使用的数据库自行判断采用 identity
,hilo
或 sequence
其中一种作为主键生成方式,非常灵活。
如果使用的是 Oracle 数据库,Hibernate 需要使用一个叫 hibernate_sequence
的 sequence 。
除 Oracle 数据库外,针对大部分其他数据库,native
泛指自增。
注意:如果 Hibernate 自动选择 sequence 或者 hilo,则所有表的主键都会从 Hibernate 默认的 sequence 或 hilo 表中取。并且有的数据库对于默认情况主键生成测试的支持,效率并不高。
使用 sequence 或 hilo 时可以加入参数,指定 sequence 名称或 hi 值表名称,如
<param name="sequence">hibernate_id</param>
特点:根据数据库自动选择,项目中如果用到多个数据库时可以使用此方式,使用时需要设置表的自增字段或建立序列、建表等。
使用 MySQL 演示:
1 使用 XML
1.1 持久化类定义:
package hibernate;import java.util.Date;public class Person { private Integer id; private String account; private String name; private Date birth; public Person() {} public Person(String account, String name, Date birth) { this.account = account; this.name = name; this.birth = birth; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "Person [id=" + id + ", account=" + account + ", name=" + name + ", birth=" + birth + "]"; }}
1.2 定义映射:
<?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="hibernate.Person" table="PERSON"> <id name="id" type="int"> <column name="ID" /> <generator class="native" /> </id> <property name="account" type="java.lang.String"> <column name="ACCOUNT" /> </property> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="birth" type="java.util.Date"> <column name="BIRTH" /> </property> </class></hibernate-mapping>
1.3 单元测试:
package hibernate;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;public class HibernateTest { @Test public void test() { Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Person person = new Person("admin", "Nick", new Date(System.currentTimeMillis())); session.save(person); transaction.commit(); session.close(); sessionFactory.close(); }}
单元测试通过,查询数据库新插入一条数据,ID
为 1
。
2 使用注解(annotation)
使用注解定义持久化类:
package hibernate;import java.util.Date;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;@Entity@Tablepublic class Person { @Id @GeneratedValue(generator = "assignedGenerator") @GenericGenerator(name = "assignedGenerator", strategy = "native") private Integer id; private String account; private String name; private Date birth; public Person() {} public Person(String account, String name, Date birth) { this.account = account; this.name = name; this.birth = birth; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "Person [id=" + id + ", account=" + account + ", name=" + name + ", birth=" + birth + "]"; }}
运行【1.3 单元测试】,测试结果相同。
阅读全文
0 0
- 7.7 Hibernate:内置生成器 – native
- 7.4 Hibernate:内置生成器 – hilo
- 7.5 Hibernate:内置生成器 – identity
- 7.6 Hibernate:内置生成器 – increment
- 7.8 Hibernate:内置生成器 – select
- 7.9 Hibernate:内置生成器 – seqhilo
- 7.10 Hibernate:内置生成器 – sequence
- 7.11 Hibernate:内置生成器 – uuid
- hibernate内置生成器
- Hibernate内置标识符生成器
- Hibernate内置标识符生成器
- Hibernate内置标识符生成器
- 7.1 Hibernate:内置生成器 -- assigned
- 7.2 Hibernate:内置生成器 -- foreign
- 7.3 Hibernate:内置生成器 -- guid
- Hibernate提供的内置标识符生成器
- Hibernate内置对象标识符(OID)生成器
- Hibernate内置的持久化标识符生成器
- JSP 数据交换
- Java中的内存结构
- 卷积神经网络(CNN)
- 关于跨域
- Linux 系统定时任务
- 7.7 Hibernate:内置生成器 – native
- POJ1182 食物链【并查集】
- MVC模式和URL访问
- Glide 缓存策略 内存缓存和磁盘缓存
- Android Glide获取缓存大小与清除缓存
- 微信通知没声音原因
- Servlet基础
- BZOJ 1047 [HAOI2007]理想的正方形 二维ST表+压维
- Glide图片发请求+缓存简单实用解析