想飞就别怕摔

大爷的并TM骂人

Hibernate创建数据库表(一对一and一对多)

使用的数据库是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文件)代码如下:
 1<?xml version='1.0' encoding='UTF-8'?>
 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
 1<?xml version="1.0" encoding="UTF-8"?>
 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>
Certificate.hbm.xml
 1<?xml version="1.0" encoding="UTF-8"?>
 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
 1<?xml version="1.0" encoding="UTF-8"?>
 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
package com.zzn.hibernate1;

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
package com.zzn.hibernate1;

public class Certificate {
    
private String id;
    
private String describe;
    
private Student stu;
//省略getter和settet方法
Team.java
package com.zzn.hibernate1;

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
package com.zzn.hibernate1;

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


}


posted on 2008-07-25 11:02 生命的绽放 阅读(3235) 评论(2)  编辑  收藏 所属分类: Hibernate

评论

# re: Hibernate创建数据库表(一对一and一对多) 2008-07-26 23:01 张景如

看不懂,呵呵。太高深了。  回复  更多评论   

# re: Hibernate创建数据库表(一对一and一对多) 2009-11-04 04:16 youke

感谢,看了你的,才知道我的外键关联写错了!浪费我好多时间  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年11月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

常用链接

留言簿(5)

随笔分类(94)

随笔档案(93)

文章分类(5)

文章档案(5)

相册

JAVA之桥

SQL之音

兄弟之窗

常用工具下载

积分与排名

最新评论

阅读排行榜