所有项目导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java
第一节:Hibernate 用对象标识符(OID)来区分对象
例子:
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.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/wishwzp/model/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
Student.java
package com.wishwzp.model; public class Student { private long id;
private String name; public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
} }
Student.hbm.xml
<?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="com.wishwzp.model"> <class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id> <property name="name"></property>
</class> </hibernate-mapping>
StudentTest.java
package com.wishwzp.service; import org.hibernate.Session;
import org.hibernate.SessionFactory; import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest { public static void main(String[] args) {
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务 //获取主键为1的学生
Student s1=(Student)session.get(Student.class, Long.valueOf(1));//当执行到这里的时候Session缓冲中会有OID为1的Studnet对象
//获取主键为2的学生
Student s2=(Student)session.get(Student.class, Long.valueOf(2));//当执行到这里的时候Session缓冲中会有OID为2的Studnet对象
//获取主键为1的学生
Student s3=(Student)session.get(Student.class, Long.valueOf(1));//在Session缓冲中找到了UID为1,所以指向了UID为1的对象
System.out.println(s1==s2);//false
System.out.println(s1==s3);//true
session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
}
}
这是数据库的信息:
结果显示:
这就表明S1和S3是指向一个地址的
第二节:Hibernate 对象标识符生成策略
主键的分类
业务主键VS 代理主键
代理主键是不具有业务性的;
1,increment 由Hibernate 自动以递增的方式生成标识符,适用代理主键;
2,identity 由底层数据库生成标识符;适用代理主键;
3,sequcence 由Hibernate 根据底层数据库的序列来生成标识符;适用代理主键;
4,hilo Hibernate 根据high/low 算法来生成标识符。适用代理主键
5,native 根据底层数据库对自动生成标识符的支持能力, 来选择identity,sequence 或hilo;适用代理主键;
increment 例子:
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.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property> <!-- 方言 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL -->
<property name="show_sql">true</property> <!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property> <mapping resource="com/wishwzp/model/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
Student.java
package com.wishwzp.model; public class Student { private long id;
private String name; public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
} }
Student.hbm.xml
<?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="com.wishwzp.model"> <class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="increment"></generator>
</id> <property name="name"></property>
</class> </hibernate-mapping>
StudentTest2.java
package com.wishwzp.service; import org.hibernate.Session;
import org.hibernate.SessionFactory; import com.wishwzp.model.Student;
import com.wishwzp.util.HibernateUtil; public class StudentTest2 { public static void main(String[] args) {
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
Session session=sessionFactory.openSession(); // 生成一个session
session.beginTransaction(); // 开启事务 Student s=new Student();
s.setName("张三");
session.save(s); session.getTransaction().commit(); // 提交事务
session.close(); // 关闭session
}
}
结果显示:
当我继续在执行一下代码。。。。。
结果显示:
identity 例子:
increment 例子的代码只改变一下Student.hbm.xml,其他什么代码都不改变。
Student.hbm.xml
<?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="com.wishwzp.model"> <class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="identity"></generator>
</id> <property name="name"></property>
</class> </hibernate-mapping>
结果显示:
当我继续在执行一下代码。。。。。
结果显示:
native 例子:
increment 例子的代码只改变一下Student.hbm.xml,其他什么代码都不改变。
Student.hbm.xml
<?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="com.wishwzp.model"> <class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id> <property name="name"></property>
</class> </hibernate-mapping>
结果显示:
当我继续在执行一下代码。。。。。
结果显示:
主要native用的最多。