文档章节

Hibernate主键生成策略

李茂冉
 李茂冉
发布于 2016/05/11 11:58
字数 467
阅读 12
收藏 0

1. ID生成策略

1.1.         XML方式

Xml生成Id

         Generator

         常用4个:

Native-int  identity (sqlserver,mysql)-int  sequence(oracle)-int    uuid-string

Increment(mysql) -int

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hiber">

    <class name="Student" table="student" >

       <id name="id">

           <!-- mysql中的auto_increment -->

<!--          <generator class="identity"/>-->

      

           <!-- oracle中的sequence -->

<!--          <generator class="sequence" />-->

      

           <!-- uuid唯一的值32 -->

           <generator class="uuid" />

      

           <!-- native(所有数据库的)自动递增 -->

<!--          <generator class="native" />-->

       </id>

       <property name="name" length="10"></property>

       <property name="sex" length="2" ></property>

       <property name="age" length="3" ></property>

    </class>

</hibernate-mapping>

 

 

1.2.         注解方式 Annotition

  例子:

@Id

  @GeneratedValue //此处默认为native

  public int getId() {

     return id;

  }

 

@Id

  @GeneratedValue(strategy=GenerationType.IDENTITY) //此处为 IDENTITY

  public int getId() {

     return id;

  }

 

 

1.3 联合主键的设置

         1.@Embeddable @Id

         2.@EmbeddedID

         3.@Id@IdClass

private Student_id_PK pk;

@EmbeddedId //联合主键的设置

  public Student_id_PK getPk() {

     return pk;

  }

例子:

package com.hiber;

import javax.persistence.EmbeddedId;

import javax.persistence.Entity;

@Entity

public class Student_id {

    private Student_id_PK pk;

    private String sex;

    @EmbeddedId   //联合主键的设置

    public Student_id_PK getPk() {

       return pk;

    }

    public void setPk(Student_id_PK pk) {

       this.pk = pk;

    }

    public String getSex() {

       return sex;

    }

    public void setSex(String sex) {

       this.sex = sex;

    }

    public int getAge() {

       return age;

    }

    public void setAge(int age) {

       this.age = age;

    }

    private int age;

   

}

 

package com.hiber;

//把当前类序列化

public class Student_id_PK implements java.io.Serializable{

    //联合主键

    private int id;

    private String name;

    public int getId() {

       return id;

    }

    public void setId(int id) {

       this.id = id;

    }

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

   

    @Override

    public boolean equals(Object obj) {

       if(obj instanceof Student_id_PK){

           Student_id_PK p = (Student_id_PK)obj;

           if(p.id==this.id && p.name.equals(this.name)){

              return true;

           }

       }

       return false;

    }

    @Override

    public int hashCode() {

       return this.name.hashCode();

    }

}

 

package com.hiber;

import org.hibernate.HibernateException;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.AnnotationConfiguration;

import org.hibernate.cfg.Configuration;

import org.hibernate.classic.Session;

 

public class Student_id_Test {

         public static void main(String[] args) {

                  //创建连接

                  //建立Configuration

                  Configuration cfg = null;

                  //xml方式

                  //cfg = new Configuration();

                 

                  //Annotition方式

                  cfg = new AnnotationConfiguration();

                 

                  cfg.configure();

                  //打开SessionFactory

                  SessionFactory sf = cfg.buildSessionFactory();

                  //创建Session

                  Session session = sf.openSession();

                 

                 

                  Student_id_PK pk=new Student_id_PK();

                  pk.setId(1);

                  pk.setName("_11");

                  Student_id stu = new Student_id();

                  stu.setPk(pk);

                  stu.setSex("");

                  stu.setAge(19);

                 

                  //打开事务

                  session.beginTransaction();

                  //保存对象

                  try{

                          session.save(stu);

                          //提交事务

                          session.getTransaction().commit();

                          System.out.println("保存数据OK");

                  }catch(HibernateException e){

                          //回滚事务

                          session.getTransaction().rollback();

                          System.out.println("保存数据失败!");

                  }finally{

                          //关闭连接

                          session.close();

                  }

         }

}

 

 

© 著作权归作者所有

共有 人打赏支持
李茂冉
粉丝 5
博文 103
码字总数 59256
作品 0
东城
高级程序员

暂无文章

Java IO类库之PrintStreamWriter

* A <code>PrintStream</code> adds functionality to another output stream, * namely the ability to print representations of various data values * conveniently. Two other fea......

老韭菜
54分钟前
0
0
qduoj~前端~二次开发~笔记

青岛大学qdu的onlinejudge是js的写的前端,框架是vue.js,在nodejs上部署运行,其实整体运行还是建立在docker的容器虚拟环境里,这里暂时不需要docker。安装环境是Ubuntu14-64bit 1.安装一大...

虚拟世界的懒猫
58分钟前
6
0
ConcurrentHashMap源码解读

部分内容转自:http://jiabinyuan.xyz/#/app/archive/detail/25 内部结构 内部采用了segment结构,每一个segment相当于一个hashtable。看下面的结构图: 从图的结构我们可以了解到,Concurr...

edwardGe
今天
1
0
Ubuntu终端Tab键自动补全

打开 /etc/bash.bashrc,找到下列代码,取消注释。 #enable bash completion in interactive shells#if ! shopt -oq posix; then# if [-f /usr/share/bash-completion/bash_compl......

大熊猫
今天
0
0
polipo socks5代理转http代理

天朝的网络,哎~ 装个 yarn 都时而会卡 假设在SSlocal 已经装好运行的前提下,来安装设置 polipo sudo apt-get install polipo sudo vim /etc/polipo/config 追加下列配置内容,并保存 socksP...

纯洁徐
今天
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部