开头说两句:
Hibernate一对一关系分为两种:
1.主键关联一对一映射:例如在两个表a,b中,一个字段在a表中是主键,a表中的该字段级联b表中的字段,该字段在b表中作为主键,也就是当a表中该字段修改时,b表中的字段随之改变;通过<one-to-one>
元素配置,
2.唯一外键关联一对一映射:一对多关系的一种特例,主键表对应实体类的配置文件中通过one-to-one元素配置。
在这里我说一下第一种情况,第二种情况就不再说明了,好了,我们开始做demo.
demo介绍:有两个表,分别是register表和userInfo表,关系:当一个用户注册时,在register表中出现这条信息,同样会在userInfo表出现这条信息。这体现了register同userInfo表的关联
1.新建工程,工程目录如下:
2.新建Register.java
和UserInfo.java
两个java文件,以下依次为代码:
/*************Register.java***************/
package com.feng.dao;
public class Register {
private String name;
private String pwd;
private String email;
private UserInfo userInfo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public UserInfo getUserInfo() {
return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
}
/****************UserInfo.java********************/
package com.feng.dao;
public class UserInfo {
private String name;
private String realName;
private Register register;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public Register getRegister() {
return register;
}
public void setRegister(Register register) {
this.register = register;
}
}
3.数据库分为两个表,分别为:register
和userInfo
4.创建java类映射文件register.hbm.xml
和userInfo.hbm.xml
,以下依次为两个文件:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.feng.dao">
<class name="Register" table="register">
<id name="name" type="java.lang.String" column="name" >
</id>
<property name="pwd" type="java.lang.String" column="pwd"/>
<property name="email" column="email"></property>
<one-to-one name="userInfo" class="UserInfo" cascade="all"/>
</class>
</hibernate-mapping>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.feng.dao">
<class name="UserInfo" table="userinfo">
<id name="name" type="java.lang.String" column="name" >
<generator class="foreign">
<param name="property">register</param>
</generator>
</id>
<property name="realName" type="java.lang.String" column="realName"/>
<one-to-one name="register" class="Register" constrained="true"/>
</class>
</hibernate-mapping>
5.创建整个工程的映射文件hibernate.cfg.xml
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ License:
GNU Lesser General Public License (LGPL), version 2.1 or later. ~ See the
lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. -->
<!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/hibernatedemo</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.characterEncoding">utf8</property>
<property name="connection.pool_siez">10</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/feng/dao/register.hbm.xml" />
<mapping resource="com/feng/dao/userInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6.编写测试类,Test.java
package com.feng.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import com.feng.dao.Register;
import com.feng.dao.UserInfo;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1.进行配置文件的读取
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder();
StandardServiceRegistry sr = ssrb.configure().build();
// 2.由config得到一个会话工厂//得到sessionFactory
SessionFactory sf = new MetadataSources(sr).buildMetadata().buildSessionFactory();
// 3.由sessionFacatory得到session
Session session = sf.openSession();
Transaction tran = session.beginTransaction();
Register login =new Register();
login.setEmail("132@456789.com");
login.setName("lisi");
login.setPwd("456");
UserInfo ui =new UserInfo();
ui.setRealName("hehhe");
login.setUserInfo(ui);
ui.setRegister(login);
session.save(login);
tran.commit(); // 一定要提交事务!!!!!!!
// 4.记得关闭session
session.close();
sf.close();
}
}
执行成功,结果就不展示了,就是在register表插入数据的同时,也向userInfo表插入了数据。
还是最后那句话:
点击查看更多内容
14人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦