- 浏览: 84844 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
ssy341:
说的简单了点,和官网的文档差不多,多文件上传也不是很好,不过可 ...
jQuery插件AjaxFileUpload文件上传实现Javascript多文件上传功能 -
蓝凝幽偌:
有没有jar包?
org.apache.commons.net.ftp.FTPClient 的简单用法 -
tinguo002:
在这里缓缓走过,很为楼主钻研精神感动,也感谢楼主分享。
关于Struts2资源文件的配置
Hibernate 一对一外键双向关联
一对一外键关联是一对多外键关联的特例,只是在多的一方加了个唯一性约束。
一、模型
一个人对应一个地址。
/*==============================================================*/
/* DBMS name: MySQL 5.0 */
/* Created on: 2008-12-9 0:12:54 */
/*==============================================================*/
drop table if exists address;
drop table if exists person;
/*==============================================================*/
/* Table: address */
/*==============================================================*/
create table address
(
id bigint not null auto_increment comment 'ID',
detail varchar(120) not null comment '详细地址',
personid bigint comment '人的ID',
primary key (id)
)
type = InnoDB;
alter table address comment '地址';
/*==============================================================*/
/* Table: person */
/*==============================================================*/
create table person
(
id bigint not null auto_increment comment 'ID',
name varchar(24) not null comment '姓名',
primary key (id)
)
type = InnoDB;
alter table person comment '人';
alter table address add constraint FK_Reference_4 foreign key (personid)
references person (id) on delete restrict on update restrict;
二、对象模型
public class Person implements java.io.Serializable {
private Long id;
private String name;
private Address address;
public class Address implements java.io.Serializable {
private Long id;
private Person person;
private String detail;
三、映射文件
<?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>
<class name="entity.Person" table="person">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="24" not-null="true">
<comment>姓名</comment>
</column>
</property>
<one-to-one name="address" 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>
<class name="entity.Address" table="address" catalog="testdb">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="detail" type="java.lang.String">
<column name="detail" length="120" not-null="true">
<comment>详细地址</comment>
</column>
</property>
<many-to-one name="person" class="entity.Person"
fetch="select" unique="true">
<column name="personid">
<comment>人的ID</comment>
</column>
</many-to-one>
</class>
</hibernate-mapping>
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/testdb
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.password">xiaohui</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="entity/Person.hbm.xml" />
<mapping resource="entity/Address.hbm.xml" />
</session-factory>
</hibernate-configuration>
四、测试
import org.hibernate.Transaction;
import entity.Address;
import entity.Person;
import utils.HibernateSessionFactory;
public class Test {
public static void main(String[] args) {
savePerson();
}
public static void savePerson() {
Person person = new Person("张三");
Address address = new Address("XX街X号");
person.setAddress(address);
address.setPerson(person);
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
session.save(person);
tx.commit();
}
}
运行日志:
Hibernate:
insert
into
person
(name)
values
(?)
Hibernate:
insert
into
testdb.address
(detail, personid)
values
(?, ?)
本文出自 “熔 岩” 博客,转载请与作者联系!
一对一外键关联是一对多外键关联的特例,只是在多的一方加了个唯一性约束。
一、模型
一个人对应一个地址。
/*==============================================================*/
/* DBMS name: MySQL 5.0 */
/* Created on: 2008-12-9 0:12:54 */
/*==============================================================*/
drop table if exists address;
drop table if exists person;
/*==============================================================*/
/* Table: address */
/*==============================================================*/
create table address
(
id bigint not null auto_increment comment 'ID',
detail varchar(120) not null comment '详细地址',
personid bigint comment '人的ID',
primary key (id)
)
type = InnoDB;
alter table address comment '地址';
/*==============================================================*/
/* Table: person */
/*==============================================================*/
create table person
(
id bigint not null auto_increment comment 'ID',
name varchar(24) not null comment '姓名',
primary key (id)
)
type = InnoDB;
alter table person comment '人';
alter table address add constraint FK_Reference_4 foreign key (personid)
references person (id) on delete restrict on update restrict;
二、对象模型
public class Person implements java.io.Serializable {
private Long id;
private String name;
private Address address;
public class Address implements java.io.Serializable {
private Long id;
private Person person;
private String detail;
三、映射文件
<?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>
<class name="entity.Person" table="person">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="24" not-null="true">
<comment>姓名</comment>
</column>
</property>
<one-to-one name="address" 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>
<class name="entity.Address" table="address" catalog="testdb">
<id name="id" type="java.lang.Long">
<column name="id" />
<generator class="identity" />
</id>
<property name="detail" type="java.lang.String">
<column name="detail" length="120" not-null="true">
<comment>详细地址</comment>
</column>
</property>
<many-to-one name="person" class="entity.Person"
fetch="select" unique="true">
<column name="personid">
<comment>人的ID</comment>
</column>
</many-to-one>
</class>
</hibernate-mapping>
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/testdb
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.password">xiaohui</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="entity/Person.hbm.xml" />
<mapping resource="entity/Address.hbm.xml" />
</session-factory>
</hibernate-configuration>
四、测试
import org.hibernate.Transaction;
import entity.Address;
import entity.Person;
import utils.HibernateSessionFactory;
public class Test {
public static void main(String[] args) {
savePerson();
}
public static void savePerson() {
Person person = new Person("张三");
Address address = new Address("XX街X号");
person.setAddress(address);
address.setPerson(person);
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
session.save(person);
tx.commit();
}
}
运行日志:
Hibernate:
insert
into
person
(name)
values
(?)
Hibernate:
insert
into
testdb.address
(detail, personid)
values
(?, ?)
本文出自 “熔 岩” 博客,转载请与作者联系!
发表评论
-
java编程思想final的理解
2015-03-06 13:38 317根据上下文环境,java的关键字final也存在着细微的区别, ... -
PDF2SWF中文字符集解决方案
2014-02-26 15:12 781转自:http://www.cnblogs.com/liver ... -
Runtime.getRuntime().exec() 输出流阻塞的解决方法
2014-02-26 09:40 1802转自:http://blog.csdn.net/xiaoani ... -
笔记(不用老搜索了)
2013-07-17 16:16 0下面代码则执行了subgo()函数, <a href ... -
利用LinkedBlockingQueue实现生产者-消费者模式
2013-06-05 16:09 660由于LinkedBlockingQueue 实现是线程安全的, ... -
加密123
2013-05-08 22:35 780/** * 加密传输时的密码 * @author weirhp ... -
jsoup解析
2013-04-28 15:31 635http://www.ibm.com/developerwor ... -
java线程管理利器:java.util.current的用法举例
2012-12-05 15:42 963DK5中增加了Doug Lea的并发库,这一引进给java线程 ... -
Tomcat - 解决which must be escaped when used within the value错误
2012-11-19 15:17 618http://www.cnblogs.com/javadu/a ... -
Java POI Word 写文档
2012-11-09 09:19 7791 package apache.poi; 2 3 ... -
使用COS组件实现文件上传
2012-06-15 11:25 1434转自:http://blog.csdn.net/jadyer/ ... -
Java Annotation(1)
2011-06-19 09:48 622作者:曾巧(numenzq) 摘要 Annotation( ... -
关于request得到路径的几种方法
2011-06-08 15:43 908request.getRequestURI() /jque ... -
JAVA EXCEL API 简介
2011-04-18 16:32 684转自:http://www.ibm.com/dev ... -
Hibernate 多对多双向关联
2011-03-21 11:09 580Hibernate 多对多双向关联 一、模型介绍 多个 ... -
Hibernate 一对多连接表双向关联
2011-03-21 11:09 731Hibernate 一对多连接表双向关联 一、模型介绍 ... -
Hibernate 一对多外键双向关联
2011-03-21 11:08 742Hibernate 一对多外键双向关联 一、模型介绍 ... -
Hibernate 一对一主键双向关联
2011-03-21 11:07 674Hibernate 一对一主键双向关联 一对一主键映射在一 ... -
Hibernate 多对多单向关联
2011-03-21 11:06 603Hibernate 多对多单向关联 一、模型介绍 多个人( ... -
Hibernate 多对一连接表单向关联
2011-03-21 11:05 627Hibernate 多对一连接表单向关联 一、模型介绍 ...
相关推荐
Hibernate 一对一外键单向...Hibernate 一对一外键双向关联 Hibernate 一对一主键双向关联 Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联
博文链接:https://shaqiang32.iteye.com/blog/201312
hibernate外键实现一对一双向关联关系源码
NULL 博文链接:https://huihai.iteye.com/blog/1243558
博文链接:https://llying.iteye.com/blog/220807
NULL 博文链接:https://cdxs2.iteye.com/blog/1930749
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联...├─ 一对一外键双向关联 ├─ 一对一主键双向关联 ├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对多连接表双向关联 └─ 多对多双向关联
hibernate one-to-one 一对一唯一外键关联映射_单向 and 双向
NULL 博文链接:https://share.iteye.com/blog/1088452
NULL 博文链接:https://paladin1988.iteye.com/blog/1657564
http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
NULL 博文链接:https://lijunabc.iteye.com/blog/438972
NULL 博文链接:https://paladin1988.iteye.com/blog/1640340
hibernate一对一唯一外键关联映射(双向关联Person<---->IdCard) 11 session flush测试(hibernate_session_flush) 12 hihernate一对多关联映射(单向Classes----->Student) 13 hihernate一对多关联映射(双向...
012 一对一 唯一外键关联映射_双向 013 session_flush 014 一对多关联映射 单向 015 一对多关联映射 双向 016 多对多关联映射 单向 017 多对多关联映射 双向 018 关联映射文件中标签中的 lazy(懒加载)属性 019 关联...
一对一关联可能是双向的,在双向关联中,有且仅有一端作为主体(owner)端存在:主体端负责维护联接列(即更新),对于不需要维护这种关系的从表则通过mappedNy属性进行声明。mappedBy的值指向主体的关联属性。例子...
自己记录的学习笔记,内容很详细,有代码,包括:一对多双向关联关系、一对多双向自身关联关系、一对一关联关系(主键关联、外键关联)、一对多与一对一结合、多对多关联关系、Map、Set、List与Bag映射关系、查询...
Hibernate 实体关联关系映射 学习总结 把一对一 一对多 单向 双向 主键 外键 链接表等讲的比较清楚