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 ...
随机推荐
- easy ui 框架
Easy UI 准备工作(搭建) 1.在WebRoot 的目录下创建js 文件夹,在文件夹中倒入一下两个包 Jquery.easyui.min.js jquery.min.js 2.在WebRoot ...
- asp.net web api添加自定义认证
1.定义认证失败结果生成器 /// <summary> /// 认证失败结果生成器 /// </summary> public class AuthenticationFail ...
- 打造 html5 文件上传组件,实现进度显示及拖拽上传,支持秒传+分片上传+断点续传,兼容IE6+及其它标准浏览器
老早就注册了博客园帐号,昨天才发现,连博客都没开,Github也是一样,深觉惭愧,赶紧潜个水压压惊`(*∩_∩*)′ 言归正传.大概许多人都会用到文件上传的功能,上传的库貌似也不少,比如(jQuery ...
- Excel列名 字母和数字的转换
Excel的列名是由于字母组成的. A-Z 分别代表1-26 AA 是27 AB是28 以此类推. 以下是这种编码的转换方法,如果遇到需要用纯字母编号来表示数字的时候可以用到. /** * 类似EX ...
- WNDR3700V4 安装SVN Server
下文所用路由器型号为:WNDR3700V4 参考链接:http://dd-wrt.ca/phpBB2/viewtopic.php?t=86912&highlight=optware http: ...
- My安卓知识2--使用listview绑定sqlite中的数据
我想在我的安卓项目中实现一个这样的功能,读取sqlite数据库中的数据并显示到某个页面的listview控件中. 首先,我建立了一个Service类,来实现对数据库的各种操作,然后在这个类中添加对数据 ...
- 纯CSS实现Tooltip
DEMO: span{ position:relative; display:inline-block; height:3em; width:3em; margin:0 0.4em; line-hei ...
- Daily Scrum 12.15
今日完成任务: 完成关于主页右侧资源显示的算法优化:解决了下载资源时的异常. 遇到困难: 编译课设这周要检查,小组成员的大部分时间在完成编译课设,时间很紧. 明日任务: 孙思权 完成第二组提供的数据库 ...
- vim 查看文件二进制格式
用vim打开文件,vim -b file,选项-b是二进制模式打开 然后输入 :%!xxd,就可看到二进制编码 其实在linux下,直接输入xxd file 也是可以看到的文件二进制格式的
- RHEL7学习之NTP配置
一,安装NTP [root@localhost ~]# yum install ntp Loaded plugins: product-id, subscription-manager This sy ...