繁体   English   中英

休眠问题未保存到数据库

[英]Hibernate Problem Not Saving to database

我遇到了休眠问题。 我尝试没有成功。 我有一个架构,其中有Category,Attribute,AttributeOption。 例如,类别可以是“计算机”,并且其各自的属性可以是“ RAM”,“硬盘”,例如,“ RAM”可以具有属性选项“ 512MB”,“ 1024MB”。好。

现在我有另一个要求。 “类别”可以有许多子类别。 例如,“计算机”可以将“笔记本电脑”或“笔记本”作为子类别。 现在这些子类别本身就是类别类别

then I get this scheme:

        1. Category  ------>Category
           A category can contain many sub categories
           e.g. A computer can be a notebook or laptop

        2. Category ------>Attribure
           A category can have many attribute
           A  notebook can have RAM , Hard Disk, Screen Size

        3. Attribute  ------>AttributeOption
           An Attribute can have many attribute options
           e.g. RAM can be 512 MB, 1024 MB

这些是我的课程,没有他们的getter和setter

Class Category:
public class Category implements IsSerializable 
{
    private long CategoryId;
    private String CategoryName;
    private Set <Category> SubCategory=new HashSet <Category> ();
    private Set <Attribute> AllAttributes= new HashSet  <Attribute>();

}
Category Mapping File:


 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 8:07:32 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
    <class name="com.BiddingSystem.Models.Category" table="CATEGORY">
        <id name="CategoryId" type="long">
            <column name="CATEGORYID" />
            <generator class="native" />
        </id>
        <property name="CategoryName" type="java.lang.String">
            <column name="CATEGORYNAME" />
        </property>
        <many-to-one name="ParentCategory" class="com.BiddingSystem.Models.Category">
            <column name="PARENTCATEGORYID" />
        </many-to-one>
        <set name="SubCategory" inverse="true" lazy="true" cascade="all" fetch="join">
            <key>
                <column name="PARENTCATEGORYID" />
            </key>
            <one-to-many class="com.BiddingSystem.Models.Category" />
        </set>
        <set name="AllAttributes" table="ATTRIBUTE" inverse="false" lazy="true">
            <key>
                <column name="CATEGORYID" />
            </key>
            <one-to-many class="com.BiddingSystem.Models.Attribute" />
        </set>
    </class>
</hibernate-mapping>


Class Attribute:
public class Attribute 
{
    private long AttributeId;
    private String AttributeName;
    private Set <AttributeOption> Options= new HashSet <AttributeOption>();
}

Attribute Mapping File:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 5:25:09 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
    <class name="com.BiddingSystem.Models.Attribute" table="ATTRIBUTE">
        <id name="AttributeId" type="long">
            <column name="ATTRIBUTEID" />
            <generator class="native" />
        </id>
        <property name="AttributeName" type="java.lang.String">
            <column name="ATTRIBUTENAME" />
        </property>
        <set name="Options" table="ATTRIBUTEOPTION" inverse="false"  cascade="all">
            <key>
                <column name="ATTRIBUTEID" />
            </key>
            <one-to-many class="com.BiddingSystem.Models.AttributeOption" />
        </set>
    </class>
</hibernate-mapping>


Class AttributeOption:
public class AttributeOption 
{
    private long AttributeOptionId;
    private String Option;
    private String SQLValue;
}
Attribute Mapping File:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 5:25:09 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
    <class name="com.BiddingSystem.Models.AttributeOption" table="ATTRIBUTEOPTION">
        <id name="AttributeOptionId" type="long">
            <column name="ATTRIBUTEOPTIONID" />
            <generator class="native" />
        </id>
        <property name="Option" type="java.lang.String">
            <column name="OPTION" />
        </property>
        <property name="SQLValue" type="java.lang.String">
            <column name="SQLVALUE" />
        </property>
    </class>
</hibernate-mapping>

我正在尝试以下。 我没有收到任何错误,但是它没有保存“笔记本电脑”,它是“计算机”的类别,否则所有内容都已保存。 所以我认为问题出在类别映射文件的这一部分:

<set name="SubCategory" table="CATEGORY" cascade="all">
            <key>
                <column name="CATEGORYID" />
            </key>
            <one-to-many class="com.BiddingSystem.Models.Category" />
        </set>



This is part of my program  


    Category C=new Category();
    C.setCategoryName("Computer");
    AttributeOption R512= new AttributeOption();R512.setOption("512");R512.setSQLValue("512");
    AttributeOption R1024= new AttributeOption();R1024.setOption("1024");R1024.setSQLValue("1024");


        Category C0= new Category();
    C0.setCategoryName("Laptop");

    C.getSubCategory().add(C0);

    Attribute RAM= new Attribute();
    RAM.setAttributeName("RAM");

    RAM.getOptions().add(R512);RAM.getOptions().add(R1024);

    C.getAllAttributes().add(RAM);



    Transaction tx = null;
    try
    {
        tx=session.beginTransaction();
        tx.begin();
        session.saveOrUpdate(C);
        tx.commit();
        return true;
    }
    catch (Exception e)
    {
        tx.rollback();
        e.printStackTrace();
        return false;
    }

这似乎有问题:

C0.getSubCategory().add(C0);

不应该是:

C.getSubCategory().add(C0);

Hummm,我在这段代码中看到了一些需要改进的地方:首先,您应该使用persist()方法来持久化您的实体。 其次,要持久保存整个对象图,您必须:a)手动持久保存子对象,然后持久保存“主”对象; 或b)在您的关系上使用Cascade参数,以便持久保留“主要”对象也将触发相关子对象的持久性。

您可以在此处找到有关级联的更多信息: http : //docs.jboss.org/hibernate/stable/core/reference/en/html/example-parentchild.html#example-parentchild-cascades

好,我成功了

关键是类别映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 8:37:02 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
    <class name="com.BiddingSystem.Models.Category" table="CATEGORY">
        <id name="CategoryId" type="long">
            <column name="CATEGORYID" />
            <generator class="native" />
        </id>
        <property name="CategoryName" type="java.lang.String">
            <column name="CATEGORYNAME" />
        </property>

        <many-to-one name="ParentCategory" class="com.BiddingSystem.Models.Category">
            <column name="PARENT_CATEGORY_ID" />
        </many-to-one>

        <set name="SubCategory" lazy="false" cascade="all-delete-orphan" inverse="true">
            <key>
                <column name="PARENT_CATEGORY_ID" />
            </key>
            <one-to-many class="com.BiddingSystem.Models.Category" />
        </set>

        <set name="AllAttributes" table="ATTRIBUTE" inverse="false" lazy="true">
            <key>
                <column name="CATEGORYID" />
            </key>
            <one-to-many class="com.BiddingSystem.Models.Attribute" />
        </set>
    </class>
</hibernate-mapping>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM