- 浏览: 623902 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
xianzi_2008:
xianzi_2008 写道楼主请教下,我缓存了一个List& ...
Ehcache -
xianzi_2008:
楼主请教下,我缓存了一个List<Bean>对象, ...
Ehcache -
jsdsh:
收藏好.五个字
hibernate之one-to-many详细 -
Tlife:
好!!!!
hibernate------HQL总结 -
yanqingluo:
例子很恰当。
观察者模式
java 代码
- package com.test;
- import java.util.HashSet;
- import java.util.Set;
- import com.test.domain.Student;
- import com.test.domain.Teacher;
- public class Test {
- public static void main(String[] args){
- Student student1 = new Student();
- Student student2 = new Student();
- Teacher teacher1 = new Teacher();
- Teacher teacher2 = new Teacher();
- student1.setStudentname("student1");
- student2.setStudentname("student2");
- teacher1.setTeachername("teachername1");
- teacher2.setTeachername("teachername2");
- Set set1 = new HashSet();
- set1.add(teacher1);
- set1.add(teacher2);
- student1.setTeachers(set1);
- student2.setTeachers(set1);
- OperatorDb od = new OperatorDb();
- od.saveUser(student1);
- od.saveUser(student2);
- }
- }
- 输出结果为:
- Hibernate: insert into hibernate.student (studentname) values (?)
- Hibernate: insert into hibernate.teacher (teachername) values (?)
- Hibernate: insert into hibernate.teacher (teachername) values (?)
- Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
- Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
- Hibernate: insert into hibernate.student (studentname) values (?)
- Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
- Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
建表语句
sql 代码
- --
- -- Host: localhost Database: hibernate
- -- ------------------------------------------------------
- -- Server version 5.0.24a-community-nt
- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
- /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
- /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
- /*!40101 SET NAMES utf8 */;
- /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
- /*!40103 SET TIME_ZONE='+00:00' */;
- /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
- /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
- /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
- /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
- --
- -- Current Database: `hibernate`
- --
- CREATE DATABASE /*!32312 IF NOT EXISTS*/ `hibernate` /*!40100 DEFAULT CHARACTER SET utf8 */;
- USE `hibernate`;
- --
- -- Table structure for table `detail`
- --
- DROP TABLE IF EXISTS `user`;
- CREATE TABLE `user` (
- `id` int(11) NOT NULL auto_increment,
- `username` varchar(255) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- DROP TABLE IF EXISTS `card`;
- CREATE TABLE `card` (
- `userId` int(11) NOT NULL default '0',
- `cardNum` varchar(255) NOT NULL,
- PRIMARY KEY (`userId`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- DROP TABLE IF EXISTS `item`;
- CREATE TABLE `item` (
- `id` int(11) NOT NULL auto_increment,
- `userId` int(11) NOT NULL default '0',
- PRIMARY KEY (`id`),
- foreign key(userId) references outTable(id) on delete cascade on update cascade
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
1. one-to-one(用户和卡片)
user.hbm.xml
xml 代码
- <!---->xml version="1.0" encoding="utf-8"?>
- <!---->
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!---->
- <hibernate-mapping>
- <class name="yahaitt.bobo.domain.User" table="user">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native" />
- id>
- <property name="username" type="java.lang.String">
- <column name="username" not-null="true" />
- property>
- <one-to-one name="card" class="yahaitt.bobo.domain.Card" cascade="all">one-to-one>
- class>
- hibernate-mapping>
Card.hbm.xml:
xml 代码
- <!---->xml version="1.0" encoding="utf-8"?>
- <!---->
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!---->
- <hibernate-mapping>
- <class name="yahaitt.bobo.domain.Card" table="card">
- <id name="userId" type="java.lang.Integer">
- <column name="userId" />
- <generator class="foreign" ><!---->
- <param name="property">userparam>
- generator>
- id>
- <property name="cardNum" type="java.lang.String">
- <column name="cardNum" not-null="true" />
- property>
- <one-to-one name="user" class="yahaitt.bobo.domain.User" >
- one-to-one>
- class>
- hibernate-mapping>
2. many-to-one (用户与Item)
注:两边都定义外键,而且不能像one-to-one里那样定义外键
如:这样定义是错误的:
xml 代码
- <generator class="foreign" >
- <param name="property">userparam>
- generator>
user.hbm.xml:
xml 代码
- <!---->xml version="1.0" encoding="utf-8"?>
- <!---->
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!---->
- <hibernate-mapping>
- <class name="ttitfly.domain.User" table="user" >
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native" />
- id>
- <property name="username" type="java.lang.String">
- <column name="username" not-null="true" />
- property>
- <set name="items" inverse="true" cascade="all" >
- <key><!---->
- <column name="userId" not-null="true" >
- column>
- key>
- <one-to-many class="ttitfly.domain.Item"/>
- set>
- class>
- hibernate-mapping>
item.hbm.xml
xml 代码
- <!---->xml version="1.0" encoding="utf-8"?>
- <!---->
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!---->
- <hibernate-mapping>
- <class name="ttitfly.domain.Item" table="item" >
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native" />
- id>
- <many-to-one name="user" class="ttitfly.domain.User">
- <!---->
- <column name="userId" not-null="true">column>
- many-to-one>
- class>
- hibernate-mapping>
测试类:Test.java
java 代码
- public class Test {
- public static void main(String[] args){
- User user = new User();
- user.setUsername("testonetomany");
- Item item1 = new Item();
- Item item2 = new Item();
- Set set = new HashSet();
- set.add(item1);
- set.add(item2);
- user.setItems(set);
- item2.setUser(user);
- item1.setUser(user);
- OperateDb operator = new OperateDb();
- operator.saveUser(user);
- }
- }
- 输出结果为:
- Hibernate: insert into user (username) values (?)
- Hibernate: insert into item (userId) values (?)
- Hibernate: insert into item (userId) values (?)
当把user.hbm.xml里的
xml 代码
- <set name="items" inverse="true" cascade="all" >
改为:
xml 代码
- <set name="items" inverse="false" cascade="all" >
测试类输出的结果为:
java 代码
- Hibernate: insert into user (username) values (?)
- Hibernate: insert into item (userId) values (?)
- Hibernate: insert into item (userId) values (?)
- Hibernate: update item set userId=? where id=?
- Hibernate: update item set userId=? where id=?
inverse的作用就可以很明显看出来了
3.many-to-many(学生和老师)
student.hbm.xml
xml 代码
- <!---->xml version="1.0" encoding="utf-8"?>
- <!---->
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!---->
- <hibernate-mapping>
- <class name="com.test.domain.Student" table="student" catalog="hibernate">
- <id name="id" type="java.lang.Integer">
- <column name="id" />
- <generator class="native" />
- id>
- <property name="studentname" type="java.lang.String">
- <column name="studentname" not-null="true" />
- property>
- <!---->
- <set name="teachers" table="teacher_student" inverse="false" cascade="all">
- <!---->
- <key column="student_id">key>
- <many-to-many class="com.test.domain.Teacher" column="teacher_id">many-to-many>
- set>
- class>
- hibernate-mapping>
teacher.hbm.xml
java 代码
- <!---->"1.0" encoding="utf-8"?>
- <!---->"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!---->
- <hibernate-mapping></hibernate-mapping>
- <class name="com.test.domain.Teacher" table="teacher" catalog="hibernate">
- "id" type="java.lang.Integer">
- "id" />
- class="native" />
- "teachername" type="java.lang.String">
- "teachername" not-null="true" />
- "students" table="teacher_student" inverse="true">
- "teacher_id">
- "student_id" class="com.test.domain.Student">
- class>
测试类:
java 代码
- package com.test;
- import java.util.HashSet;
- import java.util.Set;
- import com.test.domain.Student;
- import com.test.domain.Teacher;
- public class Test {
- public static void main(String[] args){
- Student student1 = new Student();
- Student student2 = new Student();
- Teacher teacher1 = new Teacher();
- Teacher teacher2 = new Teacher();
- student1.setStudentname("student1");
- student2.setStudentname("student2");
- teacher1.setTeachername("teachername1");
- teacher2.setTeachername("teachername2");
- Set set1 = new HashSet();
- set1.add(teacher1);
- set1.add(teacher2);
- student1.setTeachers(set1);
- student2.setTeachers(set1);
- OperatorDb od = new OperatorDb();
- od.saveUser(student1);
- od.saveUser(student2);
- }
- }
- 输出结果为:
- Hibernate: insert into hibernate.student (studentname) values (?)
- Hibernate: insert into hibernate.teacher (teachername) values (?)
- Hibernate: insert into hibernate.teacher (teachername) values (?)
- Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
- Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
- Hibernate: insert into hibernate.student (studentname) values (?)
- Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
- Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
发表评论
-
hibernate之one-to-many详细
2008-10-01 12:06 61341. 拿Classes和Student为例。 packag ... -
Hibernate中discriminator
2008-02-17 12:41 5980tbl_user 是一张表。User是它一个po,Normal ... -
hibernate总结二
2008-01-29 18:08 13951. get和load区别 load加载方法: Users ... -
hibernate总结
2008-01-28 18:07 20431.key是指外键 比如: <hibernate-m ... -
hibernate------one-to-one
2008-01-28 17:16 8449hibernate 关系映射 one-to-one主要有三种实 ... -
Hibernate-------集合
2008-01-28 13:47 2239Hibernate中的集合 Hibernate中的集合分为四类 ... -
hibernate------HQL总结
2008-01-25 15:07 9961以以个普通Users表为例: 建表语句: CREATE D ... -
hibernate-----many-to-many
2008-01-25 11:25 4365hibernate:many-to-many 以学生和老师为 ... -
hibernate----one-to-many
2008-01-21 17:58 4003hibernate里的一对多one-to-many(以学生和班 ... -
hibernate知识小结
2008-01-05 23:24 17651.hibernate中java对象的三种状态,三种状态都是对 ... -
hibernate text
2007-10-22 17:40 2653mysql数据库的一个表里的字段logins是text类型的。 ... -
hibernate 批量插入
2007-07-24 11:27 6691在企业应用开发中,经常用到批量插入的功能。对于Hibernat ...
相关推荐
**标题:“Hibernate关系映射”** 在Java世界中,Hibernate是一个强大的对象关系映射(ORM)框架,它允许开发者将数据库操作转化为面向对象的方式,极大地简化了数据持久化的复杂性。"多对一"关系映射是Hibernate...
综上所述,这个压缩包提供了关于Hibernate关系映射的全面教程,包括多对多、一对一和一对多的关系配置,以及如何在Spring+Hibernate+Struts环境中应用这些关系。对于想要深入理解Hibernate并提升开发效率的Java...
Hibernate对象关系映射一对多 很基础等文档
hibernate关系映射,1V1,1vN,nVn,单相双相
hibernate关系映射系列1,单向1对1映射,意向Nto1 程序说明:生成mysql数据库,建立DB Browser连接后,用户可在程序中自动生成mysql表,非常简单,适合初学者了解hibernate映射机制,有问题可联系flyallen5@hotmail.com
Hibernate是一款强大的对象关系映射(ORM)框架,它允许开发者用Java对象来操作数据库,而无需直接编写SQL语句。关系映射XML生成工具,如"Middlegen-Hibernate-r5",则是为了简化这一过程而设计的。这些工具能够自动...
**二、Hibernate关系映射的类型** 1. **一对一(OneToOne)**: 表示一个实体对应另一个实体的唯一实例。这可以通过在两个类中都定义`@OneToOne`注解来实现,并通过`mappedBy`属性指定被引用的一方。 2. **一对多...
Hibernate_关联关系映射配置详解,希望能帮助广大java爱好者
Hibernate4中映射关系图解。
<hibernate-mapping> <class name="com.bjsxt.hibernate.User" table="t_user"> <!-- --> </hibernate-mapping> <hibernate-mapping> <class name="com.bjsxt.hibernate.Group" ...
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate关联映射的关联关系,...
现在我也正在学hibernate,于是我认为这东西对你也有用吧