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