使用的数据库是MSSQL,库名hibernate,预建立的表有3张。
分别是Student(学生)表,字段名:id、team_di、name、cardId、age。
team(班级)表,字段名:id、team_id。
Certificate(身份证)表,字段名:id、describe。
Student与Certificate是一对一的关系,team与Student是一对多的关系。
1.建立工程->加入Hibernate能力(自动生成.cfg.xml文件)代码如下:
2 <! DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
5
6 <!-- Generated by MyEclipse Hibernate Tools. -->
7 < hibernate-configuration >
8
9 < session-factory >
10 < property name ="dialect" >
11 org.hibernate.dialect.SQLServerDialect
12 </ property >
13 < property name ="connection.url" >
14 jdbc:microsoft:sqlserver://localhost:1433;databasename=hibernate
15 </ property >
16 < property name ="connection.username" > sa </ property >
17 < property name ="connection.password" > sa </ property >
18 < property name ="connection.driver_class" >
19 com.microsoft.jdbc.sqlserver.SQLServerDriver
20 </ property >
21 < property name ="myeclipse.connection.profile" > MsSQL </ property >
22
23 < property name ="hibernate.hbm2ddl.auto" > create-drop </ property >
24 < property name ="show_sql" > true </ property >
25
26
27 < mapping resource ="com/zzn/hibernate1/Certificate.hbm.xml" />
28 < mapping resource ="com/zzn/hibernate1/Student.hbm.xml" />
29 < mapping resource ="com/zzn/hibernate1/Team.hbm.xml" />
30
31
32 </ session-factory >
33
34 </ hibernate-configuration >
我们小分析一下要注意的代码。
14行的databasename=hibernate是你要让hibernate把数据库表建立到那个数据库里,需要你来指定。如果不指定就加到默认的库中。
23行的<property name="hibernate.hbm2dll.auro"></property>千万不要忘写。中间的参数可以是create-drop或update,如果你建立的表名在数据库中已有,那么create-drop是将它删然后建立你用hibernate要建立的表,而update则是更新你已有的数据库表,原有的列都不会被删除。
24行是在console中显示执行的SQL语句。
27-29行是即将要建立的映射文件。
2.下面我们要编写映射文件。
Student.hbm.xml
2 <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
4 < hibernate-mapping >
5 < class name ="com.zzn.hibernate1.Student" table ="student" lazy ="true" >
6 < id name ="id" unsaved-value ="null" >
7 < generator class ="uuid.hex" ></ generator >
8 </ id >
9 < property name ="cardid" type ="string" ></ property >
10 < property name ="name" type ="string" ></ property >
11 < property name ="age" type ="int" ></ property >
12 < one-to-one name ="cer" class ="com.zzn.hibernate1.Certificate"
13 fetch ="join" cascade ="all" >
14 </ one-to-one >
15 < many-to-one name ="team" class ="com.zzn.hibernate1.Team" column ="team_id" fetch ="join"
2 <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
4 < hibernate-mapping >
5 < class name ="com.zzn.hibernate1.Student" table ="student" lazy ="true" >
6 < id name ="id" unsaved-value ="null" >
7 < generator class ="uuid.hex" ></ generator >
8 </ id >
9 < property name ="cardid" type ="string" ></ property >
10 < property name ="name" type ="string" ></ property >
11 < property name ="age" type ="int" ></ property >
12 < one-to-one name ="cer" class ="com.zzn.hibernate1.Certificate"
13 fetch ="join" cascade ="all" >
14 </ one-to-one >
15 < many-to-one name ="team" class ="com.zzn.hibernate1.Team" column ="team_id" fetch ="join" cascade="all >
16 </ many-to-one >
17 </ class >
18 </ hibernate-mapping >
16 </ many-to-one >
17 </ class >
18 </ hibernate-mapping >
Certificate.hbm.xml
2 <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
4 < hibernate-mapping >
5 < class name ="com.zzn.hibernate1.Certificate" table ="certificate" lazy ="true" >
6 < id name ="id" >
7 < generator class ="foreign" >
8 < param name ="property" > stu </ param >
9 </ generator >
10 </ id >
11 < one-to-one name ="stu" class ="com.zzn.hibernate1.Student" fetch ="join" constrained ="true" >
12 </ one-to-one >
13 < property name ="describe" column ="describes" type ="string" ></ property >
14 </ class >
15 </ hibernate-mapping >
Team.hbm.xml
2 <! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
4 < hibernate-mapping >
5 < class name ="com.zzn.hibernate1.Team" table ="team" lazy ="true" >
6 < id name ="id" unsaved-value ="null" >
7 < generator class ="uuid.hex" >
8 </ generator >
9 </ id >
10 < property name ="teamName" type ="string" ></ property >
11 < set name ="students" inverse ="true" fetch ="select" lazy ="true" >
12 < key column ="team_id" ></ key >
13 < one-to-many class ="com.zzn.hibernate1.Student" />
14 </ set >
15 </ class >
16 </ hibernate-mapping >
编写持久化类
Student.java
public class Student {
private String id;
private String name;
private String cardid;
private int age;
private Certificate cer;
private Team team;
//省略getter和settet方法
Certificate.java
public class Certificate {
private String id;
private String describe;
private Student stu;
//省略getter和settet方法
Team.java
import java.util.HashSet;
import java.util.Set;
public class Team {
private String id;
private Set students=new HashSet();
private String teamName;
//省略getter和setter方法
以上都完成就可以写个测试文件测试一下了。
Test.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
public static void main(String[] args) {
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
Student stu= new Student();
Certificate cer=new Certificate();
Team t=new Team();
stu.setId("1");
stu.setAge(22);
stu.setCardid("10001");
stu.setName("xiaoming");
stu.setTeam(t);
stu.setCer(cer);
t.setId("1001");
t.setTeamName("3.2");
cer.setDescribe("asdf");
cer.setStu(stu);
sess.save(stu);
tx.commit();
sess.close();
}
}