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的更多相关文章

  1. 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 错误的原因:表的主键字段必须包含分 ...

  2. Mysql中的primary key 与auto_increment

    mysql> create table cc(id int auto_increment); ERROR (): Incorrect table definition; there can be ...

  3. postgresql数据库primary key约束/not null约束/unique约束及default值的添加与删除、列的新增/删除/重命名/数据类型的更改

    如果在建表时没有加primary key约束.not null约束.unique约束.default值,而是创建完表之后在某个字段添加的话 1.primary key约束的添加与删除 给red_pac ...

  4. 在Hibernate中配置Hilo进行数据绑定测试时出错:org.hibernate.MappingException: Could not instantiate id generator

    在进行学习具体类单表继承时使用hilo类型时总是在调度过程中提示如下信息,无法通过.留下记录备查. 在网上找相关信息, 未解决,详细如下: org.hibernate.MappingException ...

  5. 解决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 ...

  6. Hibernate —— ID的各种生成器(转)

    Hibernate中,<id>元素下的可选<generator>子元素是一个Java类的名字,用来为该持久化类的实例生成惟一标示,所有的生成器都实现net.sf.hiberna ...

  7. Hibernate的generator属性之意义

    Hibernate的generator属性之意义 本文讲述Hibernate的generator属性的意义.Generator属性有7种class,本文简略描述了这7种class的意义和用法. Hib ...

  8. (转)Sqlite中INTEGER PRIMARY KEY AUTOINCREMENT和rowid的使用

    原文:http://www.cnblogs.com/peida/archive/2008/11/29/1343832.html Sqlite中INTEGER PRIMARY KEY AUTOINCRE ...

  9. mysql中key 、primary key 、unique key 与index区别

    一.key与primary key区别 CREATE TABLE wh_logrecord ( logrecord_id ) NOT NULL auto_increment, ) default NU ...

随机推荐

  1. oracle exp imp 导入 正在跳过表 plsql 导入表 成功终止 数据 被导入

    http://blog.csdn.net/agileclipse/article/details/12968011 .导入过程中,所有表导入都出现提示, 正在跳过表...某某表名 最后提示成功终止导入 ...

  2. @RequestBody接收ajax的json字符串

    在使用ajax进行请求,并传递参数时,偶尔需要把数组作为传递参数,这是就要使用@RequestBody来解决这个问题 在页面端的处理: (1)利用JSON.stringify(arr)需要把json对 ...

  3. mac 端口转发方案

    mac 端口映射 好坑 osx10.10mac移除了ipfw改用pfpf以前没用过 查文档mac的 pf语法,没有找到.bsd的倒是很全,心喜拿来一试.http://www.openbsd.org/f ...

  4. [修改后]html+css 做成一个可浏览的表格

    现在表格内容需要显示的要求如下: 1, 表格很大,界面放不小,需要放到div中. 2, 在div中可以用scroll滑动查看. 3, td中的内容保持在一行中. 4, 可以点击tr,然后可以选中并了解 ...

  5. springMVC 相对于 Structs 的优势

    智者说,没有经过自己的思考和估量,就不能接受别人的东西.资料只能是一个参考,至于是否正确,还得自己去分辨 SpringMVC相对于Structs的几个优势: 1.springMVC安全性更高,stru ...

  6. C# 读取Excel文件里面的内容到DataSet

    摘要:读取Excel文件里面的内容到DataSet 代码: /// <summary> /// 读取Excel文件里面的内容到DataSet /// </summary> // ...

  7. 其他浏览器(firefox,chrome)可以上网 ie(Internet Explorer)无法上网 解决方法

    http://blog.csdn.net/andywangcn/article/details/8945366

  8. 健忘vs总结

    上周入职新公司,报道之前自己也曾想过要从头开始,用一个新的精神面貌来迎接新的起点,培养一些新的习惯. 周四是15日,新公司的发薪日(当然还没有我的份~),小组群内一个刚毕业的新人兴冲冲的说终于领到第一 ...

  9. Good-Bye

    嘛……以一种奇怪的姿势滚粗了…… 如果这个Blog能给未来的OIer们一些帮助的话,它也不枉存在了…… 我的OI之路也能以另一种形式延续下去吧…… 也许能搞ACM的话会再开?…… 不管怎么说,各位再见 ...

  10. Python回调函数用法实例详解

    本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...