Hibernate Id Generator and Primary Key
Use automate id by hibernate:
If you want the tables' id be created automation. How to do it?
When use XML file, Just use the generator:
<id name="id"> <generator class="native"></generator> </id>
Now id which is named id will be created automation. The class native can automatically identify Database you use.
You can also use uuid or hilo to get a single number. uuid can generate a number unique all the universe.
When use annotation, Just use the @GeneratedValue under @Id if you want to automate id.
Use @GeneratedValue by default. All database can automate id.
When the Database which suport identity. @GeneratedValue(strategy = GenerationType.IDENTITY).
When use database which suport sequence. @GeneratedValue(strategy = GenerationType.SEQUENCE)
The best way to let your data and program can be used anywhere is use like this:
@TableGenerator @TableGenerator(
ame="DOCTOR_GEN",
table="GENERATOR_TABLE",
pkColumnName = "pk_key",
valueColumnName = "hi",
pkColumnValue="doctor",
allocationSize=1 ) /*Use TableGenerator * Create a table which is named GENERATOR_TABLE and includes two column * Column "Key" and column "hi" * set a value in column Key which is named teacher * then the table which is named teacher create id will get it form GENERATOR_TABLE * at the last the value of column hi do add by allocationSize * */
TableGennerator create a table to save number for other tables;
Use @GeneratedValue(strategy=GenerationType.TABLE, generator="DOCTOR_GEN") to use Generator table.
Sometimes we need more than one column to be primary key.
So we Create a class to provide object which object package primary key.
public class ProgramerPK implements Serializable{
private int id;
private String sid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
@Override
public boolean equals(Object o){
if(o instanceof ProgramerPK){
ProgramerPK sp = (ProgramerPK)o;
if(this.id == sp.getId() && this.sid == sp.getSid()){
return true;
}
}
return false;
}
@Override
public int hashCode(){
return this.sid.hashCode();
}
}
This class has two elements named id and sid. But we must implement the Serializable interface.
The Serializable interface can make this class be serialized.
Then we must also to override the equals method and hashCode method.
The equals which is overrode can assert objects real equal or not.
The hashCode which is overrode can get real hashCode for object.
And the hashCode will be used for serializeble.
If use XML file to configure hibernate do like this:
<composite-id class="StudentPK" name="stp">
<key-property name="id"></key-property>
<key-property name="sid"></key-property>
</composite-id>
If use annotation just write like this:
@EmbeddedId
//use many of columns be ID
public ProgramerPK getPp() {
return pp;
}
The source code : https://github.com/andy201401/hibernate_learn/tree/master/hibernat_0400_ID
Hibernate Id Generator and Primary Key的更多相关文章
- 1503 - A PRIMARY KEY must include all columns in the table's partitioning function
1503 - A PRIMARY KEY must include all columns in the table's partitioning function 错误的原因:表的主键字段必须包含分 ...
- Mysql中的primary key 与auto_increment
mysql> create table cc(id int auto_increment); ERROR (): Incorrect table definition; there can be ...
- postgresql数据库primary key约束/not null约束/unique约束及default值的添加与删除、列的新增/删除/重命名/数据类型的更改
如果在建表时没有加primary key约束.not null约束.unique约束.default值,而是创建完表之后在某个字段添加的话 1.primary key约束的添加与删除 给red_pac ...
- 在Hibernate中配置Hilo进行数据绑定测试时出错:org.hibernate.MappingException: Could not instantiate id generator
在进行学习具体类单表继承时使用hilo类型时总是在调度过程中提示如下信息,无法通过.留下记录备查. 在网上找相关信息, 未解决,详细如下: org.hibernate.MappingException ...
- 解决mybatis generator警告Cannot obtain primary key information from the database, generated objects may be incomplete
使用 mybatis generator 生成pojo.dao.mapper时 经常出现 Cannot obtain primary key information from the database ...
- Hibernate —— ID的各种生成器(转)
Hibernate中,<id>元素下的可选<generator>子元素是一个Java类的名字,用来为该持久化类的实例生成惟一标示,所有的生成器都实现net.sf.hiberna ...
- Hibernate的generator属性之意义
Hibernate的generator属性之意义 本文讲述Hibernate的generator属性的意义.Generator属性有7种class,本文简略描述了这7种class的意义和用法. Hib ...
- (转)Sqlite中INTEGER PRIMARY KEY AUTOINCREMENT和rowid的使用
原文:http://www.cnblogs.com/peida/archive/2008/11/29/1343832.html Sqlite中INTEGER PRIMARY KEY AUTOINCRE ...
- mysql中key 、primary key 、unique key 与index区别
一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id ) NOT NULL auto_increment, ) default NU ...
随机推荐
- MINA系列学习-mina整体介绍
今天的这一节,将从整体上对mina的源代码进行把握,网上已经有好多关于mina源码的阅读笔记,但好多都是列举了一下每个接口或者类的方法.我倒是想从mina源码的结构和功能上对这个框架进行剖析.源码的阅 ...
- JavaScript基础认知
此文只适用于初学者,大神们就不要看了,嘿嘿~ 一.定义变量 关键字 var,由此关键字定义变量,例如:var a =21:就把21这个数定义给了变量a 二.基本数据类型 1.Number类型 表示数字 ...
- Arcengine,C#获得FeatureClass的坐标系ISpatialReference以及所在数据集名称
1: /// <summary> 2: /// 获得坐标系统 3: /// </summary> 4: /// <param name="pFeatureCla ...
- des加密解密——java加密,php解密
最近在做项目中,遇到des加密解密的问题. 场景是安卓app端用des加密,php这边需要解密.之前没有接触过des这种加密解密算法,但想着肯定会有demo.因此百度,搜了代码来用.网上代码也是鱼龙混 ...
- mysql定时器Events
MySQL定时器Events 一.背景 我们MySQL的表A的数据量已经达到1.6亿,由于一些历史原因,需要把表A的数据转移到一个新表B,但是因为这是线上产品,所以宕机时间需要尽量的短,在不影响数据持 ...
- Python之Scrapy爬虫框架安装及简单使用
题记:早已听闻python爬虫框架的大名.近些天学习了下其中的Scrapy爬虫框架,将自己理解的跟大家分享.有表述不当之处,望大神们斧正. 一.初窥Scrapy Scrapy是一个为了爬取网站数据,提 ...
- In和Out指令
In和Out OUT 0FAH,AL(它是指从AL中输出一个字节到0FAH的一个端口吗?) OUT DX,AL OUT 0FAH,AX(AX是一个字为什么也能输出到0FAH所指的8位端口中呢?) ...
- List集合基于某个字段排序
using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Product { publ ...
- JavaWeb技术(一):JDBC简介
一. JDBC简介 1. Java Database Connectivity(JDBC) 使用JDBC可以对数据库进行访问 2. JDBC的核心接口 1)DriverManager 驱动管理器接口 ...
- Oracle 服务器端执行带参数的procedure
进入服务器后 su - oracle sqlplus schema/schemapass 连接上以后,输入以下,然后执行 declare vRet number(5) := 8; begin proc ...