博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate主键生成策略
阅读量:5938 次
发布时间:2019-06-19

本文共 3248 字,大约阅读时间需要 10 分钟。

  hot3.png

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 联合主键的设置

         @Id

        

        

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();

                  }

         }

}

 

 

转载于:https://my.oschina.net/csmw00/blog/673322

你可能感兴趣的文章
UItableview section和cell的局部刷新
查看>>
字符串连接的效率问题
查看>>
紫书 例题 11-12 UVa 1515 (最大流最小割)
查看>>
紫书 习题 11-17 UVa 1670 (图论构造)
查看>>
洛谷P1108 低价购买 (最长下降子序列方案数)(int,long long等 范围)
查看>>
大道至简-第五章-心得体会
查看>>
Python编程从入门到实践,个人笔记
查看>>
哈尔滨理工大学第七届程序设计竞赛初赛(高年级组)F - 苦逼的单身狗
查看>>
oracle数据迁移
查看>>
ArchLinux and LXDE and LXDM
查看>>
对拍--from Altf4
查看>>
JavaScript基础6——全选示例
查看>>
JavaScript基础知识总结(三)
查看>>
【python3】爬取简书评论生成词云
查看>>
HttpApplication、HttpContext、HttpModule、HttpHandler
查看>>
Android Service学习
查看>>
【数据库】sql2008卸载和默认实例的删除 ...
查看>>
SDNU 1086.迷宫问题(bfs标记路径)
查看>>
类的高内聚低耦合
查看>>
怎么下载geventwebsocket
查看>>