hibernate之主键生成策略
作者:互联网
1.hibernate的主键生成器:
generator元素:表示了一个主键生成器,它用来为持久化类实例生成唯一的标识 。
准备工作:
hibernate.cfg.xml核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">123</property>
<property name="connection.url">jdbc:mysql://localhost:3306/t226?useUnicode=true&characterEncoding=UTF-8
</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置本地事务(No CurrentSessionContext configured!) -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 2. 调试相关 -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 3. 添加实体映射文件 -->
<mapping resource="com/qukang/one/entity/User.hbm.xml" />
<!-- 4. 主键生成策略-->
<mapping resource="com/qukang/two/entity/Student.hbm.xml" />
<mapping resource="com/qukang/two/entity/Worker.hbm.xml" />
</session-factory>
</hibernate-configuration>
Student.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.qukang.two.entity.Student" table="t_hibernate_student">
<id name="sid" type="java.lang.Integer" column="sid">
<generator class="assigned" />
<!-- <generator class="increment" /> -->
<!-- <generator class="sequence" /> -->
<!-- <generator class="sequence" > <param name="sequence_name">aaa</param>
</generator> -->
<!-- <generator class="com.javaxl.two.id.Myts" /> -->
</id>
<property name="sname" type="java.lang.String" column="sname">
</property>
</class>
</hibernate-mapping>
对应的实体类
package com.qukang.two.entity;
public class Student {
private Integer sid;
private String sname;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + "]";
}
public Student(Integer sid,String sname) {
this.sid=sid;
this.sname=sname;
}
public Student() {}
}
Worker.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.qukang.two.entity.Worker" table="t_hibernate_worker">
<id name="wid" type="java.lang.String" column="wid">
<generator class="assigned" />
<!-- <generator class="sequence" /> -->
<!-- <generator class="sequence" > <param name="sequence_name">aaa</param>
</generator> -->
<!-- <generator class="com.javaxl.two.id.Myts" /> -->
</id>
<property name="wname" type="java.lang.String" column="wname">
</property>
</class>
</hibernate-mapping>
对应的实体类
package com.qukang.two.entity;
public class Worker {
private String wid;
private String wname;
public String getWid() {
return wid;
}
public void setWid(String wid) {
this.wid = wid;
}
public String getWname() {
return wname;
}
public void setWname(String wname) {
this.wname = wname;
}
@Override
public String toString() {
return "Worker [wid=" + wid + ", wname=" + wname + "]";
}
public Worker(String wid, String wname) {
this.wid = wid;
this.wname = wname;
}
public Worker() {}
}
1.1 程序员自己控制:assigned
当你的主键ID引用的类是assigned时,不管你的数据库有没有设置自增长,都需要自己手动添加ID
不然就会报错
public static void main(String[] args) {
DemoDao dao=new DemoDao();
Student stu=new Student();
stu.setSname("哈哈哈");
dao.addStudent(stu);
}
1.2 数据库控制: identity(标识列/自动增长) sequence
1.3 hibernate控制:increment uuid/uuid.hex
1.4 其它:native
标签:hibernate,String,wname,wid,主键,sname,sid,生成,public 来源: https://blog.csdn.net/weixin_45176922/article/details/98226767