本节简介:
1 单一主键的两种赋值方式:手动赋值(assigned)和自动赋值(native)
2 mysql和oracle赋值的不同形式
3 demo
2 native由底层数据库生成标识符,如果是MySQL,就是increment,如果是oracle就是sequence.
注意:mysql的increment生成主键的形式不能用于集群。
3 demo:
hibernate.cft.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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/bendi</property>
<property name="connection.username">root</property>
<property name="connection.password">weidoudou</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource = "Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Student.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-12-20 0:42:12 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.ddwei.student.Student" table="STUDENT"> <id name="pid" type="int"> <column name="PID" /> <generator class="native" /> </id> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="sex" type="java.lang.String"> <column name="SEX" /> </property> <property name="birthday" type="java.util.Date"> <column name="BIRTHDAY" /> </property> <property name="address" type="java.lang.String"> <column name="ADDRESS" /> </property> </class> </hibernate-mapping>
StudentTest.java
package hibernate_001;
import java.util.Date;
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test;
import com.ddwei.student.Student;
public class StudentTest { private SessionFactory sessionFactory; private Session session; private Transaction trasaction; @Test public void testSaveStudent(){ // Student student =new Student(1,"周恩来","男",new Date(),"绍兴");//创建学生对象 Student student = new Student(); student.setName("秦始皇"); student.setSex("男"); student.setBirthday(new Date()); student.setAddress("阿房宫"); session.save(student);//会话保存学生对象进入数据库 } @Before public void init(){ //1 创建配置对象 Configuration config = new Configuration().configure(); //2 创建服务对象 ServiceRegistry serviceRe = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //3 创建会话工厂 sessionFactory = config.buildSessionFactory(serviceRe); //4 打开会话 session = sessionFactory.openSession(); //5 创建事务 trasaction = session.beginTransaction(); } @After public void destroy(){ trasaction.commit(); session.close(); sessionFactory.close(); }
}