在数据库领域中,数据表和数据表之间关系一般可以分为如下几种:
单对单:比如公民和身份证的关系,一个人只有一张身份证,同时每张身份证也仅仅对应一个人!
单对多:比如单个客户和订单之间的关系,每个客户可以同时下多张订单!
多对多:比如学生管理系统中,学生与课程,教师与学生之间的关系!
上面是简单的说了下数据库表与表之间的关系,那么我们现在要说的是Hibernate,来看看Hibernate中如何使用表与表的关系,首先来看看我数据库中二个示例表user和card分别表示用户和身份证!二个表中分别有二字段,创建表的DDL如下:
create table `study`.`card`(
`cardid` int default '' not null,--主键
`cardnum` int, --身份证号
primary key (`cardid`)
);
create unique index `PRIMARY` on `study`.`card`(`cardid`);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
create table `study`.`user`(
`userid` int not null auto_increment,--主键,自动递增
`username` varchar(20),--用户名
primary key (`userid`)
);
create unique index `PRIMARY` on `study`.`user`(`userid`);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这二张表简单说明人与身份证的关系,熟练点的朋友可能看到我们并没有在card类中为cardid指明外键,而它的外键应该就是user表中的userID,那么这些关系在哪儿声明呢,讲到Hibernate,当然就是在Hibernate中定义了,好了,开始看看如何做:
首先还是建立一个Project,加载Hibernate,并且由MYSQL,生成映射Bean,具体做方法参见Hibernate 初识(补充)
,我们看生成的Card和User类很普通,与先前我们讲到的没什么区别,但现在我们要在这二个Bean中添加属性,Card添加user属性,而User添加card属性,完整代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
User.java
package fengyan.hibernate;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class User implements java.io.Serializable
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Fields
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Integer userid;
private String username;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Card card;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Constructors
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Card getCard()
{
return card;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setCard(Card card)
{
this.card = card;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//** default constructor */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public User()
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//** full constructor */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public User(String username)
{
this.username = username;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Property accessors
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Integer getUserid()
{
return this.userid;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setUserid(Integer userid)
{
this.userid = userid;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getUsername()
{
return this.username;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setUsername(String username)
{
this.username = username;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Card.java
package fengyan.hibernate;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class Card implements java.io.Serializable
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Fields
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private Integer cardid;
private Integer cardnum;
private User user;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Constructors
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public User getUser()
{
return user;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setUser(User user)
{
this.user = user;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//** default constructor */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Card()
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//** full constructor */
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Card(Integer cardnum)
{
this.cardnum = cardnum;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// Property accessors
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Integer getCardid()
{
return this.cardid;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setCardid(Integer cardid)
{
this.cardid = cardid;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Integer getCardnum()
{
return this.cardnum;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setCardnum(Integer cardnum)
{
this.cardnum = cardnum;
}
}
我们现在就可以看到这样用户中有卡,卡也对应用户,一对一!在Bean中体现,当然现在我们已经要有概念,就是仅仅在Bean中这样声明是没用的,我们需要修改类与表的映射文件
先改User.hbm.xml:内容如下:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="fengyan.hibernate.User" table="user">
<id name="userid" type="java.lang.Integer">
<column name="userid" />
<generator class="native" /><!-- 主键为自动递增 -->
</id>
<property name="username" type="java.lang.String">
<column name="username" length="20" />
</property>
<!--
我们添加一个<ont-to-one>节点
里面的属性card 对应的calss
cascade="all"一个用户对应一张卡,一张卡对应一个用户,但还是有一个主的,也就是人
我们先出生人,然后才办卡,所以在这加入cascade由User主控
-->
<one-to-one name="card" class="fengyan.hibernate.Card" cascade="all"></one-to-one>
</class>
</hibernate-mapping>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
补充说明,cascade=all,说明当User删除或增加时Card也受影响!
Card.hbm.xml 内容如下:
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="fengyan.hibernate.Card" table="card">
<id name="cardid" type="java.lang.Integer">
<column name="cardid" />
<generator class="foreign"><!-- User是递增,那么卡的编号从哪来? -->
<param name="property">user</param><!--外键为user这个属性来确定-->
</generator>
</id>
<property name="cardnum" type="java.lang.Integer">
<column name="cardnum" />
</property>
<one-to-one name="user" class="fengyan.hibernate.User"></one-to-one>
</class>
</hibernate-mapping>
配置完成后,我们来一编写一个DAO类userDAO.java代码如下:
package fengyan.hibernate;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
import org.hibernate.Session;
import org.hibernate.Transaction;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class UserDAO
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void save(User user)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Session session = HibernateSessionFactory.getSession();
Transaction transaction = session.beginTransaction();
session.save(user);
transaction.commit();
session.close();
}
}
新建一个Servlet处理代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse response)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
throws ServletException, IOException
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
User user = new User();
user.setUsername("fengyan");//设置用户名
Card card = new Card();
card.setCardnum(420983);//设置身份证号
user.setCard(card);
card.setUser(user);
UserDAO ua = new UserDAO();
ua.save(user);//这样在添加用户的时候会自动添加一张卡
}
最后JSP代码:
<body>
This is my JSP page. <br>
<a href="servlet/addUser">add user</a>
</body>
当我们运行的时候,会发现User表中新增了一条记录
![](https://images.cnblogs.com/cnblogs_com/eflylab/20061228233610.jpg)
同时Card表中也新增了一条记录,但我们在doPost()方法中仅仅是save(user),而并没有save(card),为什么card也会添加进去,这就是我们在配置文件中写的表关系作用!