native算法生成随机id:
native
根据底层数据库的能力选择 identity、sequence 或者 hilo 中的一个。
native算法指定的字段类型:
用mysql,你需要设置主键bigint, autoincreament (大数,自增)
用oracle,你需要设置主键bigint, sequence
这样才能在hibernate中指定native
Student:
package cn.edu.hpu.model; public class Student { private int id; private String name; private int age; 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; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.edu.hpu.model"> <class name="Student" table="stu"> <id name="id"> <!--指定uuid算法来让hibernate自动生成id值放入数据库中--> <generator class="native"></generator> </id> <property name="name"></property> <property name="age"></property> </class> </hibernate-mapping>
package cn.edu.hpu.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import cn.edu.hpu.model.Student; public class StudentTest { public static void main(String[] args) { Student s=new Student(); s.setId(12); s.setName("s1"); s.setAge(1); Configuration cfg=new Configuration(); SessionFactory sf=cfg.configure().buildSessionFactory(); Session session=sf.openSession();//得到session session.beginTransaction(); session.save(s); session.getTransaction().commit(); session.close(); sf.close(); } }
uuid算法生成随机id:
uuid算法指定的字段类型必须是String类型。
Student:
package cn.edu.hpu.model; public class Student { private String id; private String name; private int age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.edu.hpu.model"> <class name="Student" table="stu"> <id name="id"> <!--指定uuid算法来让hibernate自动生成id值放入数据库中--> <generator class="uuid"></generator> </id> <property name="name"></property> <property name="age"></property> </class> </hibernate-mapping>
package cn.edu.hpu.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import cn.edu.hpu.model.Student; public class StudentTest { public static void main(String[] args) { Student s=new Student(); s.setId("10"); s.setName("s1"); s.setAge(1); Configuration cfg=new Configuration(); SessionFactory sf=cfg.configure().buildSessionFactory(); Session session=sf.openSession();//得到session session.beginTransaction(); session.save(s); session.getTransaction().commit(); session.close(); sf.close(); } }
转载请注明出处:http://blog.csdn.net/acmman