父亲(一)对儿子(多)
Hibernate3刚接触,以前的XML文件也不熟,其他大侠的文章都是先来个多对一,再来个一对多;
我暂时还没配成过多对一,然后再一对多,实在郁闷。
这里只写关于 一对多 的注释配置
希望对拥有同样问题的初学者有帮助(代码能跑起来)。
最简单的一对多关系,
只需在父亲(一)这一端配置关系,并不需要在(多)端做任何额外的配置。
这种情况下,查询父亲(一)时可以找出儿子并放到父亲类相应的集合中(本例子的children变量),
但是查询儿子(多)时,并不会找到父亲(稍微思考一下:找到也没有,儿子并没有容纳父亲的变量存在).
我使用的是Oracle 10g,生成表的主键是靠Oracle的序列,其他向SQL Server,MySQL有自增属性的,可以参考其他大侠,关于@Id的配置
父亲类(一)
package com.security.bean;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="TBL_FATHER")
public class Father {
@Id
@SequenceGenerator(name="SEQ_FATHER", sequenceName="SEQ_FATHER", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_FATHER")
private Long Father_ID;
@Column(name="fatherName")
private String fatherName;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="fatherID") //对应儿子类的哪个字段
private Set<Child> children;
public Father() {
}
public Long getFather_ID() {
return Father_ID;
}
public void setFather_ID(Long fatherID) {
Father_ID = fatherID;
}
public String getFatherName() {
return fatherName;
}
public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}
public Set<Child> getChildren() {
return children;
}
public void setChildren(Set<Child> children) {
this.children = children;
}
}
儿子类(多)
package com.security.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="TBL_Child")
public class Child {
@Id
@SequenceGenerator(name="SEQ_CHILD", sequenceName="SEQ_CHILD", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_CHILD")
private Long Child_ID;
@Column(name="childName")
private String childName;
@Column(name="fatherID")
private Long fatherID;
public Child() {
}
public Long getChild_ID() {
return Child_ID;
}
public void setChild_ID(Long childID) {
Child_ID = childID;
}
public String getChildName() {
return childName;
}
public void setChildName(String childName) {
this.childName = childName;
}
public Long getFatherID() {
return fatherID;
}
public void setFatherID(Long fatherID) {
this.fatherID = fatherID;
}
}
main方法
Father father = new Father();
//father.setFather_ID(fatherID); //ID不需要设置
father.setFatherName("Father");
Child child1 = new Child();
child1.setChildName("child1111");
Child child2 = new Child();
child2.setChildName("child2222");
Set<Child> childSet = new HashSet<Child>();
childSet.add(child1);
childSet.add(child2);
father.setChildren(childSet);
session.save(father);
建表语句也有讲究(在儿子这一端)
declare num number;
begin
select count(1) into num from user_tables where table_name='TBL_FATHER';
if num>0
then execute immediate 'drop table TBL_FATHER cascade constraints';
end if;
end;
/
create table TBL_FATHER --就2个字段,没有特别的
(
Father_ID number(20),
fatherName varchar2(100)
)
tablespace BSSG
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64
minextents 1
maxextents unlimited
);
alter table TBL_FATHER
add constraints PK_Father_ID primary key(Father_ID)
儿子
1.fatherID是与Father表的关联字段,fatherID是外键但我没有把它声明为外键
2.不要将fatherID加上not null的约束,否则插入的时候会报错!
declare num number;
begin
select count(1) into num from user_tables where table_name='TBL_CHILD';
if num>0
then execute immediate 'drop table TBL_CHILD cascade constraints';
end if;
end;
/
create table TBL_CHILD
(
Child_ID number(20),
childName varchar2(100),
fatherID number(20) --不要加not null约束
)
tablespace BSSG
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64
minextents 1
maxextents unlimited
);
alter table TBL_CHILD
add constraints PK_Child_ID primary key(Child_ID)
不要加not null约束的原因是:
Hibernate的执行语句是:
insert into tbl_Father(Father_ID, fatherName) values(?,?);
insert into tbl_Child(Child_ID,childName,fatherID) values(?,?,?);
...(n条插入儿子的语句)
update tbl_Child set fatherID=? where Child_ID=?
...(n条更新儿子fatherID的语句)
在插入一条记录到tbl_Child的时候,fatherID是空的,
如果你加了非空约束,就等着报错吧!
如果不报错,请告诉你是怎么配的,谢谢。(这是我准备研究的下一个问题!)
分享到:
相关推荐
NULL 博文链接:https://profound-accumulation.iteye.com/blog/2243079
Hibernate双向一对一关联映射(注解版)
在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式以后,发现使用...
使用hibernate的注解配置可以很好的完成跟实体的映射。而且还可以通过注解配置,可以完成多对一,一对多等关系的配置,非常好。
于是我花了几天时间搭建出来的hibernate+springmvc的框架,这个框架去除hibernate配置, 将配置全部整合到spring中,开发中使用注解开发即可,很方便,以后我还会持续更新的。 这是一个maven项目,放在eclipse中,改...
在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,这个是xml配置的例子
hibernate中一对一,一对多,多对多关系的配置,延迟加载,cascade,inverse hibernate查询方式概述,HQL查询,QBC查询,分页,结果集封装方式 ,高级查询 查询的优化,一级缓存,二级缓存,批量查询,注解方式
ssh全注解项目(一对一,一对多,多对多),hibernate的注解配置及映射关系
* @OneToMany 设置一对多关联。cascade属性指定关联级别,参考@OneToOne中的说明。fetch指定是否延迟加载,值为FetchType.LAZY表示延迟,为FetchType.EAGER表示立即加载 * 方法一 使用这种配置,在为“一端”添加“多...
一个完成了hibernate的注解和配置的简单案例;这是一个一对多【多对一】的案例,其实一对多的案例就可以解决项目中的大部分需求。因为多对多可以一般我们转化为两个多对一的案例;
重点包括一对一,一对多及多对多关系的配置代码实例。 myeclipse项目工程导入即可运行(需要自行导入hibernat4.1.4包依赖(myeclipse中自带此包))。每个实例均附说明及mysql的库表sql。另,如果要测试代码中的hbm2...
一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...
这种策略支持双向的一对多关联,但不支持 IDENTIFY 生成器策略,因为ID必须在多个表间共享。一旦使用就不能使用AUTO和IDENTIFY生成器。 每个类层次结构一张表 @Entity @Inheritance(strategy=InheritanceType....
案例目录: 01 01Hibernate_Handwork : 手工配置使用Hibernate,其中详细标了Hibernate进行持久化的一些过程,因为是Hibernate的入门实例,所以注释很详细,其中有... 一对一、多对一、一对多、多对多等几种情况。
7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many ...
一对一(One-to-one) 2.2.5.2. 多对一(Many-to-one) 2.2.5.3. 集合类型 2.2.5.4. 用cascading实现传播性持久化(Transitive persistence) 2.2.5.5. 关联关系获取 2.2.6. 映射复合主键与外键 2.2.7. 映射二级表...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...