ssh 一对多保存问题

mmkkuoi 2011-04-11 10:58:21
sql 表

--部门表
create table Tdepartement(
FdId int identity(1,1) primary key , --部门id
FdNo nvarchar(50) not null, --部门编号
FdName nvarchar(50) not null, --部门名
)


--员工表
create table Temployee
(
FeId int identity(1,1) primary key, --员工id
FdId int not null, --部门id
FeNo nvarchar(50), --员工编号
FeName nvarchar(50) not null, --员工名
Festatus int not null default 0, --员工状态 0在职,1离职
Fremarks nvarchar(200) --备注
)

--员工表关联部门表一个部门有多个员工
alter table Temployee
add constraint FK_FdId foreign key(FdId) references Tdepartement(FdId)


--类别表
create table Ttype
(
FtId int identity(1,1) primary key,
FtName nvarchar(50)
)

--设备表(主机,显示器,笔记本,打印机等类别)
create table Tfacility
(
FfId int identity(1,1) primary key,
FfNo nvarchar(50) not null, --设备编码唯一的
FtId int not null, --类别
Fyjpz nvarchar(50), --设备配置内容如:联想
FrkDate nvarchar(50), --入库日期
FbfDate nvarchar(50), --报废日期
Fbxdate nvarchar(50), --保修时间
Fcj nvarchar(50), --厂家
Fjg int , --价格
Fcstatus int not null, --设备状态 0空闲,1使用中,2报废,3维修(升级),
Fremarks nvarchar(200) --备注(维修记录)
)

--设备表关联类别表,一种类别有多个设备
alter table Tfacility
add constraint FK_FtId foreign key(FtId) references Ttype(FtId)

--电脑配件
create table Thardware
(
FhId int identity(1,1) primary key, --配件id
FhNo nvarchar(50) not null, --配件编码唯一
FxmName nvarchar(50) not null, --项目名如:内存
Fyjpz nvarchar(50), --配件配置内容如:金士顿2g DDR3
FrkDate nvarchar(50), --入库日期
FbfDate nvarchar(50), --报废日期
Fbxdate nvarchar(50), --保修时间
Fcj nvarchar(50), --厂家
Fjg int , --价格
Fcstatus int not null, --配件状态 0空闲,1使用中,2报废,3维修(升级),
Fremarks nvarchar(200), --备注(维修记录)
FfId int --设备id
)

--配件表关联设备(主要是配件组成主机),一台主机有多个配件
alter table Thardware
add constraint FK_FfId foreign key(FfId) references Tfacility(FfId)

--设备维修升级记录表
create table TServicing
(
FsId int identity(1,1) primary key,
FsNo nvarchar(50) not null, --设备编码唯一的
FtName nvarchar(50), --类别
Fyjpz nvarchar(50), --设备配置内容如:联想 (显示配件) 如果要显示配件详细则配件表添加外键FsId
FrkDate nvarchar(50), --入库日期
FbfDate nvarchar(50), --报废日期
Fbxdate nvarchar(50), --保修时间
Fcj nvarchar(50), --厂家
Fjg int , --价格
Fcstatus int not null, --设备状态 0空闲,1使用中,2报废,3维修(升级),
Fremarks nvarchar(200) --备注(维修记录)
)


--分配表
create table TAllot
(
FaId int identity(1,1) primary key,
FfId int , --设备表id
FpcId int, --主机id
FdpId int, --显示器id
FdId int not null, --部门id
FeId int , --员工id
FfpDate nvarchar(50), --分配日期
Fremarks nvarchar(200) --备注
)

--一件设备对应一个分配(部门或员工)
alter table Tallot
add constraint FK_Tallot_FfId foreign key(FfId) references Tfacility(FfId) ,
constraint FK_Tallot_FeId foreign key(FeId) references Temployee(FeId),
constraint FK_Tallot_FdId foreign key(FdId) references Tdepartement(FdId),
constraint FK_Tallot_FpcId foreign key(FpcId) references Tfacility(FfId) ,
constraint FK_Tallot_FdpId foreign key(FdpId) references Tfacility(FfId)

--分配记录表
create table TallotAnnal
(
FaAnnalId int identity(1,1) primary key,
FfId int , --设备表id
FpcId int, --主机id
FdpId int, --显示器id
FdId int not null, --部门id
FeId int , --员工id
FfpDate nvarchar(50), --分配日期
Fremarks nvarchar(200) --备注
)


alter table TallotAnnal
add constraint FK_TallotAnnal_FfId foreign key(FfId) references Tfacility(FfId),
constraint FK_TallotAnnal_FeId foreign key(FeId) references Temployee(FeId),
constraint FK_TallotAnnal_FdId foreign key(FdId) references Tdepartement(FdId),
constraint FK_TallotAnnal_FpcId foreign key(FpcId) references Tfacility(FfId) ,
constraint FK_TallotAnnal_FdpId foreign key(FdpId) references Tfacility(FfId)
...全文
267 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmkkuoi 2011-04-11
  • 打赏
  • 举报
回复
Tfacility.hbm.xml
<hibernate-mapping>
<class name="com.qy.entity.Tfacility" table="Tfacility" schema="dbo" catalog="zcpd">
<id name="ffId" type="java.lang.Integer">
<column name="FfId" />
<generator class="native" />
</id>
<many-to-one name="ttype" class="com.qy.entity.Ttype" fetch="select" lazy="false">
<column name="FtId" not-null="true" />
</many-to-one>
大壹哥 2011-04-11
  • 打赏
  • 举报
回复

<many-to-one name="ttype" class="com.qy.entity.Ttype" fetch="select" lazy="false">

这一行似乎有问题,里面的ttype关联到哪里了?
mmkkuoi 2011-04-11
  • 打赏
  • 举报
回复
上面的部门和员工可以忽略,
问题是我保存TAllot表的时候会提是Ttype not null
TAllot 表有3个字段 FpcId ,FfId,FdpId 分别关联 Tfacility 表
Tfacility 是 one TAllot 是many
Ttype 是 one Tfacility 是 manay
lyhmy 2011-04-11
  • 打赏
  • 举报
回复
你是不是在保存TAllot的时候没有给他选择部门啊,你设置的部门id是不能为空的
mmkkuoi 2011-04-11
  • 打赏
  • 举报
回复
在线等,很急,麻烦各位了
mmkkuoi 2011-04-11
  • 打赏
  • 举报
回复
sql表
-类别表
create table Ttype
(
FtId int identity(1,1) primary key,
FtName nvarchar(50)
)

--设备表(主机,显示器,笔记本,打印机等类别)
create table Tfacility
(
FfId int identity(1,1) primary key,
FfNo nvarchar(50) not null, --设备编码唯一的
FtId int not null, --类别
Fyjpz nvarchar(50), --设备配置内容如:联想
FrkDate nvarchar(50), --入库日期
FbfDate nvarchar(50), --报废日期
Fbxdate nvarchar(50), --保修时间
Fcj nvarchar(50), --厂家
Fjg int , --价格
Fcstatus int not null, --设备状态 0空闲,1使用中,2报废,3维修(升级),
Fremarks nvarchar(200) --备注(维修记录)
)
--设备表关联类别表,一种类别有多个设备
alter table Tfacility
add constraint FK_FtId foreign key(FtId) references Ttype(FtId)


--分配表
create table TAllot
(
FaId int identity(1,1) primary key,
FfId int , --设备表id
FpcId int, --主机id
FdpId int, --显示器id
FdId int not null, --部门id
FeId int , --员工id
FfpDate nvarchar(50), --分配日期
Fremarks nvarchar(200) --备注
)

--一件设备对应一个分配(部门或员工)
alter table Tallot
add constraint FK_Tallot_FdId foreign key(FdId) references Tdepartement(FdId),
constraint FK_Tallot_FpcId foreign key(FpcId) references Tfacility(FfId) ,
constraint FK_Tallot_FdpId foreign key(FdpId) references Tfacility(FfId)

hbm.xml
Tfacility.hbm.xml
<hibernate-mapping>
<class name="com.qy.entity.Tfacility" table="Tfacility" schema="dbo" catalog="zcpd">
<id name="ffId" type="java.lang.Integer">
<column name="FfId" />
<generator class="native" />
</id>
<many-to-one name="ttype" class="com.qy.entity.Ttype" fetch="select" lazy="false">
<column name="FtId" not-null="true" />
</many-to-one>
<property name="ffNo" type="java.lang.String">
<column name="FfNo" length="50" not-null="true" />
</property>
<property name="fyjpz" type="java.lang.String">
<column name="Fyjpz" length="50" />
</property>
<property name="frkDate" type="java.lang.String">
<column name="FrkDate" length="50" />
</property>
<property name="fbfDate" type="java.lang.String">
<column name="FbfDate" length="50" />
</property>
<property name="fbxdate" type="java.lang.String">
<column name="Fbxdate" length="50" />
</property>
<property name="fcj" type="java.lang.String">
<column name="Fcj" length="50" />
</property>
<property name="fjg" type="java.lang.Integer">
<column name="Fjg" />
</property>
<property name="fcstatus" type="java.lang.Integer">
<column name="Fcstatus" not-null="true" />
</property>
<property name="fremarks" type="java.lang.String">
<column name="Fremarks" length="200" />
</property>
<set name="tallotsForFpcId" inverse="true">
<key>
<column name="FpcId" />
</key>
<one-to-many class="com.qy.entity.Tallot" />
</set>
<set name="tallotsForFfId" inverse="true">
<key>
<column name="FfId" />
</key>
<one-to-many class="com.qy.entity.Tallot" />
</set>
<set name="tallotsForFdpId" inverse="true">
<key>
<column name="FdpId" />
</key>
<one-to-many class="com.qy.entity.Tallot" />
</set>
</class>
</hibernate-mapping>

Ttype.hbm.xml
<hibernate-mapping>
<class name="com.qy.entity.Ttype" table="Ttype" schema="dbo" catalog="zcpd">
<id name="ftId" type="java.lang.Integer">
<column name="FtId" />
<generator class="native" />
</id>
<property name="ftName" type="java.lang.String">
<column name="FtName" length="50" />
</property>
<set name="tfacilities" inverse="true">
<key>
<column name="FtId" not-null="true" />
</key>
<one-to-many class="com.qy.entity.Tfacility" />
</set>
</class>
</hibernate-mapping>

TAllot.hbm.xml
<hibernate-mapping>
<class name="com.qy.entity.Tallot" table="TAllot" schema="dbo" catalog="zcpd">
<id name="faId" type="java.lang.Integer">
<column name="FaId" />
<generator class="native" />
</id>
<many-to-one name="tfacilityByFpcId" class="com.qy.entity.Tfacility" fetch="select" cascade="all">
<column name="FpcId"/>
</many-to-one>
<many-to-one name="tfacilityByFdpId" class="com.qy.entity.Tfacility" fetch="select" cascade="all">
<column name="FdpId" />
</many-to-one>
<many-to-one name="tfacilityByFfId" class="com.qy.entity.Tfacility" fetch="select" cascade="all">
<column name="FfId" />
</many-to-one>
<property name="fremarks" type="java.lang.String">
<column name="Fremarks" length="50" />
</property>
<property name="ffpDate" type="java.lang.String">
<column name="FfpDate" length="50" />
</property>
</class>
</hibernate-mapping>

我先根据Tfacility 的id得到Tfacility对象,和TAllot表的其他属性, 当我想保存TAllot的时候
异常:org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: com.qy.entity.Tfacility.ttype; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.qy.entity.Tfacility.ttype
mmkkuoi 2011-04-11
  • 打赏
  • 举报
回复
不好意思,刚才太多,我重复下
  • 打赏
  • 举报
回复
楼主想分享? 还是要问问题?
先保存子类,再父类。。。
amos1989 2011-04-11
  • 打赏
  • 举报
回复
恩。。然后列? 楼主想分享? 还是要问问题?

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧