new的Object是一个VO,不是被Hibernate管理的PO,而load来的就是;
1:
Parent p = new Parent ();
Child c = new Child();
c.setParent(p);
p.getChildren().add(c);
session.save(p);
session.flush();
照你这么写的,根本就只会插入parent的数据而已.child是否从session中load的?
1:
Parent p = new Parent ();
Child c = new Child();
c.setParent(p);
p.getChildren().add(c);
session.save(p);
session.flush();
照你这么写的,根本就只会插入parent的数据而已.child是否从session中load的?
解决方案 »
- 关于Struts的问题
- struts2验证码与validate验证问题
- 工作需要,求免费OA源码.
- 问了很多Java 牛人, 都回答不了的问题,不知道是太菜,还是太难
- spring声明式事务的问题????????
- spring+hibernate+mysql 急
- hibernate-4.2.3 @OneToOne 不能用了么?
- jsp调用ejb的问题
- SpringMVC注解事物管理
- web程序用comm2.0.jar取232串口信息,无法解析。请大神帮忙分析。
- 关于tomcat连接池问题(高手回答,要实质性的回答,高分)
- 哪位大虾买了 《J2EE应用与BEA WebLogic Server(第二版)》
一、session.save(p),这里应该是save many的对象还是one的对象?
如果是save one的对象,那么hibernate的那份reference有错?
如果是save many的对象,如果Parent的Child一下子增加多个,该怎么Save??书上的两种做法让我糊涂了!!二、你说的不是PO,但是经过session.save(p)处理它不是由VO变成PO了么?问题多多~~~~~~~~~~
many的xml配置文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1 http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
--><class
name="com.digipower.demo.vo.PCgCodeGVO"
table="P_CGCODE_G"
dynamic-update="true"
dynamic-insert="true"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="P_CGCODE_G"
dynamic-update="true"
dynamic-insert="true"
</meta> <id
name="pcgcodeGId"
type="int"
column="P_CGCODE_G_ID"
unsaved-value="0"
>
<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
type="int"
column="P_CGCODE_G_ID"
</meta>
<generator class="assigned" />
</id> <property
name="adClientId"
type="int"
column="AD_CLIENT_ID"
not-null="true"
length="10"
>
<meta attribute="field-description">
@hibernate.property
column="AD_CLIENT_ID"
length="10"
not-null="true"
</meta>
</property>
<property
name="adOrgId"
type="int"
column="AD_ORG_ID"
not-null="true"
length="10"
>
<meta attribute="field-description">
@hibernate.property
column="AD_ORG_ID"
length="10"
not-null="true"
</meta>
</property>
<property
name="isactive"
type="java.lang.String"
column="ISACTIVE"
not-null="true"
length="1"
>
<meta attribute="field-description">
@hibernate.property
column="ISACTIVE"
length="1"
not-null="true"
</meta>
</property>
<property
name="created"
type="java.sql.Timestamp"
column="CREATED"
not-null="true"
length="7"
>
<meta attribute="field-description">
@hibernate.property
column="CREATED"
length="7"
not-null="true"
</meta>
</property>
<property
name="createdby"
type="int"
column="CREATEDBY"
not-null="true"
length="10"
>
<meta attribute="field-description">
@hibernate.property
column="CREATEDBY"
length="10"
not-null="true"
</meta>
</property>
<property
name="updated"
type="java.sql.Timestamp"
column="UPDATED"
not-null="true"
length="7"
>
<meta attribute="field-description">
@hibernate.property
column="UPDATED"
length="7"
not-null="true"
</meta>
</property>
<property
name="updatedby"
type="int"
column="UPDATEDBY"
not-null="true"
length="10"
>
<meta attribute="field-description">
@hibernate.property
column="UPDATEDBY"
length="10"
not-null="true"
</meta>
</property>
<property
name="cgvalue"
type="java.lang.String"
column="CGVALUE"
not-null="true"
length="80"
>
<meta attribute="field-description">
@hibernate.property
column="CGVALUE"
length="80"
not-null="true"
</meta>
</property>
<property
name="name"
type="java.lang.String"
column="NAME"
not-null="true"
length="120"
>
<meta attribute="field-description">
@hibernate.property
column="NAME"
length="120"
not-null="true"
</meta>
</property>
<property
name="location"
type="java.lang.String"
column="LOCATION"
not-null="true"
length="20"
>
<meta attribute="field-description">
@hibernate.property
column="LOCATION"
length="20"
not-null="true"
</meta>
</property>
<property
name="description"
type="java.lang.String"
column="DESCRIPTION"
length="510"
>
<meta attribute="field-description">
@hibernate.property
column="DESCRIPTION"
length="510"
</meta>
</property>
<property
name="help"
type="java.lang.String"
column="HELP"
length="4000"
>
<meta attribute="field-description">
@hibernate.property
column="HELP"
length="4000"
</meta>
</property> <!-- Associations -->
<!-- bi-directional one-to-many association to PcgcodeK -->
<set
name="pcgcodeKs"
lazy="false"
inverse="false"
cascade="all"
>
<meta attribute="field-description">
@hibernate.set
lazy="false"
inverse="false"
cascade="all" @hibernate.collection-key
column="P_CGCODE_G_ID" @hibernate.collection-one-to-many
class="com.digipower.demo.vo.PCgCodeKVO"
</meta>
<key>
<column name="P_CGCODE_G_ID" />
</key>
<one-to-many
class="com.digipower.demo.vo.PCgCodeKVO"
/>
</set></class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1 http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
--><class
name="com.digipower.demo.vo.PCgCodeKVO"
table="P_CGCODE_K"
dynamic-update="false"
dynamic-insert="false"
batch-size="100"
>
<meta attribute="class-description" inherit="false">
@hibernate.class
table="P_CGCODE_K"
dynamic-update="false"
dynamic-insert="false"
batch-size="100"
</meta> <id
name="pcgcodeKId"
type="int"
column="P_CGCODE_K_ID"
unsaved-value="0"
>
<meta attribute="field-description">
@hibernate.id
generator-class="assigned"
type="int"
column="P_CGCODE_K_ID"
</meta>
<generator class="assigned" />
</id> <property
name="adClientId"
type="int"
column="AD_CLIENT_ID"
not-null="true"
length="10"
>
<meta attribute="field-description">
@hibernate.property
column="AD_CLIENT_ID"
length="10"
not-null="true"
</meta>
</property>
<property
name="adOrgId"
type="int"
column="AD_ORG_ID"
not-null="true"
length="10"
>
<meta attribute="field-description">
@hibernate.property
column="AD_ORG_ID"
length="10"
not-null="true"
</meta>
</property>
<property
name="isactive"
type="java.lang.String"
column="ISACTIVE"
not-null="true"
length="1"
>
<meta attribute="field-description">
@hibernate.property
column="ISACTIVE"
length="1"
not-null="true"
</meta>
</property>
<property
name="created"
type="java.sql.Timestamp"
column="CREATED"
not-null="true"
length="7"
>
<meta attribute="field-description">
@hibernate.property
column="CREATED"
length="7"
not-null="true"
</meta>
</property>
<property
name="createdby"
type="int"
column="CREATEDBY"
not-null="true"
length="10"
>
<meta attribute="field-description">
@hibernate.property
column="CREATEDBY"
length="10"
not-null="true"
</meta>
</property>
<property
name="updated"
type="java.sql.Timestamp"
column="UPDATED"
not-null="true"
length="7"
>
<meta attribute="field-description">
@hibernate.property
column="UPDATED"
length="7"
not-null="true"
</meta>
</property>
<property
name="updatedby"
type="int"
column="UPDATEDBY"
not-null="true"
length="10"
>
<meta attribute="field-description">
@hibernate.property
column="UPDATEDBY"
length="10"
not-null="true"
</meta>
</property>
<property
name="cgvalue"
type="java.lang.String"
column="CGVALUE"
not-null="true"
length="80"
>
<meta attribute="field-description">
@hibernate.property
column="CGVALUE"
length="80"
not-null="true"
</meta>
</property>
<property
name="name"
type="java.lang.String"
column="NAME"
not-null="true"
length="120"
>
<meta attribute="field-description">
@hibernate.property
column="NAME"
length="120"
not-null="true"
</meta>
</property>
<property
name="location"
type="java.lang.String"
column="LOCATION"
not-null="true"
length="20"
>
<meta attribute="field-description">
@hibernate.property
column="LOCATION"
length="20"
not-null="true"
</meta>
</property>
<property
name="description"
type="java.lang.String"
column="DESCRIPTION"
length="510"
>
<meta attribute="field-description">
@hibernate.property
column="DESCRIPTION"
length="510"
</meta>
</property>
<property
name="help"
type="java.lang.String"
column="HELP"
length="4000"
>
<meta attribute="field-description">
@hibernate.property
column="HELP"
length="4000"
</meta>
</property> <!-- Associations -->
<!-- bi-directional many-to-one association to PcgcodeG -->
<many-to-one
name="pCgCodeGVO"
class="com.digipower.demo.vo.PCgCodeGVO"
not-null="true"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="P_CGCODE_G_ID"
>
<meta attribute="field-description">
@hibernate.many-to-one
not-null="true"
update="true"
insert="true"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
@hibernate.column name="P_CGCODE_G_ID"
</meta>
<column name="P_CGCODE_G_ID" />
</many-to-one></class>
</hibernate-mapping>
存储代码:
PCgCodeGVO gvo = new PCgCodeGVO(); /** 设置VO对象的主键值 */
gvo.setPcgcodeGId(Integer.valueOf("1000"7)); PCgCodeKVO kvo = new PCgCodeKVO();
kvo.setPcgcodeKId(Integer.valueOf("111")); kvo.setpCgCodeGVO(gvo);
gvo.getPcgcodeKs().add(kvo); session.save(gvo); //这里写成save(kvo)也不成两天了,郁闷死我了
Parent p = new Parent ();
p.setId()
Child c = new Child();
c.setParent(p);
p.getChildren().add(c);
session.save(p);
session.flush();
像这样写是正确的,会产生两条Insert语句,需要设置one这端cascade=all.
呵呵,没这样用过.
Parent p = new Parent ();
p.setId("...")
Child c1 = new Child();
c1.setId("...");
c1.setParent(p);
Child c2 = new Child();
c2.setId("...");
c2.setParent(p);
p.getChildren().add(c1);
p.getChildren().add(c2);
session.save(p);
session.flush();
而你的key策略是assigned,由parent主控,cascade=all,这样当你sessison.save(parent)时,hibernate发现其子的id不为null,于是认为这是一个PO,于是调用update语句更新所有child的状态.
事实上,应该让child主控的<set inverse="true" ....
然后:
//从其它层得到parent
session.save(parent);
//从其它层得到children
List children=....;
for (Iterator it = children.iterator(); it.hasNext();) {
Child child=(Child)it.next();
//这里的parent已经是一个po了,但事实上使用一个VO也成,cascade=none时,hibernate不会关心parent的状态的
child.setParent(parent);
session.save(child);
}
//这样作,应该是没有任何问题的,我一直都是这样用的.但你采用assigned时还是要小心一点,千万不要使用saveOrUpdate()