论坛首页 Java企业应用论坛

几个0ne to one ,hibernate的问题?

浏览 10970 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2003-09-24  
1.在one to one关系中,如何删除与所有父对象都无关联的子对象的集合?我用
session.delete("from "+Address.class.getName());<Address为子对象〉
时候报错,告诉说不可以删除与父对象有关联的子对象。请问用什么语句才可以把所有没有与父对象都无关联的子对象的集合都删除?

2.
      <id
            name="userID"
            column="LogonID"
            type="string"
            unsaved-value="any"
        >中的 unsaved-value怎么用,我看了一下资料,不懂?

3。        <property
            name="userName"
            type="string"
            update="true"
            insert="true"
            column="Name"
        />中的update   、insert默认为true,但具体怎么用?

4。thanks
   发表时间:2003-09-24  
1 贴出代码和映射

2 unsaved-value用于区分是新对象还是老对象(已经保存到数据库中了),
  即flush()时是insert还是update

3
update, insert (optional - defaults to true); specifies that the mapped columns should be included in SQL UPDATE and/or INSERT statements. Setting both to false allows a pure "derived" association whose value is initialized from some other property that maps to the same colum(s); or by a trigger or other application
0 请登录后投票
   发表时间:2003-09-24  
1的代码和映射:
<?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>
    <class
        name="dbdemo.User"
        table="Users"
        dynamic-update="false"
    >

        <id
            name="userID"
            column="LogonID"
            type="string"
            unsaved-value="none"
        >
            <generator class="assigned">
            </generator>
        </id>

        <property
            name="emailAddress"
            type="string"
            update="true"
            insert="true"
            column="EmailAddress"
        />

        <property
            name="lastLogon"
            type="date"
            update="true"
            insert="true"
            column="LastLogon"
        />

        <property
            name="password"
            type="string"
            update="true"
            insert="true"
            column="Password"
        />

        <property
            name="userName"
            type="string"
            update="true"
            insert="true"
            column="Name"
        />


        <one-to-one
            name="address"
            class="dbdemo.Address"
            cascade="all"
            outer-join="true"
            constrained="false"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-User.xml
            containing the additional properties and place it in your merge dir.
        -->

    </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>
    <class
        name="dbdemo.Address"
        table="Address2"
        dynamic-update="false"
    >

        <id
            name="id"
            column="ID"
            type="string"
            unsaved-value="any"
        >
            <generator class="assigned">
            </generator>
        </id>

        <property
            name="city"
            type="string"
            update="true"
            insert="true"
            column="City"
        />

        <property
            name="state"
            type="string"
            update="true"
            insert="true"
            column="State"
        />

        <property
            name="zip"
            type="string"
            update="true"
            insert="true"
            column="Zip"
        />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Address.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>

关键代码:
session.delete("from "+Address.class.getName());////删除所有子对象
0 请登录后投票
   发表时间:2003-09-24  
试过了,如果不设置User到Address的外键的话,删除Address没问题。



你的映射太乱了,两个id generator都是assigned,一个unsave-value是none,一个是any,自己都要搞晕了。你再仔细看看hibernate的文档吧,看看one-to-one是如何连接的。
0 请登录后投票
   发表时间:2003-09-24  
可是我就是要删除与user没有关联的 address,也就是假设address表中的记录比user中的记录要多,在这里我认为那些与user没有对应关系的address为垃圾记录。所以要删除掉,不知道你懂我的意思没有?:D
0 请登录后投票
   发表时间:2003-09-24  
《1》To yehs220兄:
2,3中你帖出 的文档看了,不太明白其中的意思,请举个例子来说明一下在特定的环境中怎么用
《2》
&lt;quote&gt; unsaved-value用于区分是新对象还是老对象(已经保存到数据库中了),
即flush()时是insert还是update
&lt;/quote&gt;
unsave-value怎么用可以具体说明一下么?(能贴出些实际代码最好,呵呵)
&lt;3&gt;.
,update="true"
insert="true" 也一样,我也是不知道在什么情况下设置成什么?能说详细一点么?《3》thanks:D
0 请登录后投票
   发表时间:2003-09-24  
引用

可是我就是要删除与user没有关联的 address,也就是假设address表中的记录比user中的记录要多,在这里我认为那些与user没有对应关系的address为垃圾记录。所以要删除掉,不知道你懂我的意思没有?

当你删除一个User时,它对应的Address也应该被级联删除

如果你要批量删除的话,直接用jdbc
0 请登录后投票
   发表时间:2003-09-24  
嗯,这个我试过了,可以级连删除的,那么如果出现了多的记录,如通过手工或者什么没有经过user,直接在address中添加的垃圾记录我怎么维护。准确地说是删除 Thanks
0 请登录后投票
   发表时间:2003-09-24  
引用

那么如果出现了多的记录,如通过手工或者什么没有经过user,直接在address中添加的垃圾记录我怎么维护。

这种数据完整性都在你的控制之下呀

加一个address到user的外键约束
0 请登录后投票
   发表时间:2003-09-24  
哦,明白了,是按照下面这样么?
<?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> 
    <class 
        name="dbdemo.Address" 
        table="Address2" 
        dynamic-update="false" 
    > 

        <id 
            name="id" 
            column="ID" 
            type="string" 
            unsaved-value="any" 
        > 
            <generator class="assigned"> 
            </generator> 
        </id> 

        <property 
            name="city" 
            type="string" 
            update="true" 
            insert="true" 
            column="City" 
        /> 

        <property 
            name="state" 
            type="string" 
            update="true" 
            insert="true" 
            column="State" 
        /> 

        <property 
            name="zip" 
            type="string" 
            update="true" 
            insert="true" 
            column="Zip" 
        /> 

         <one-to-one 
            name="user" 
            class="dbdemo.User" 
            cascade="all" 
            outer-join="true" 
            constrained="false" 
        /> 
    </class> 

</hibernate-mapping> 
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics