Hibernate问题不保存到数据库

时间:2010-12-16 14:54:00

标签: java hibernate

我遇到了一个休眠问题。我没有成功。我有一个架构,其中有Category,Attribute,AttributeOption。例如类别可以是“计算机”,其各自的属性可以是“RAM”,“硬盘”,例如'RAM'可以有属性选项'512MB','1024MB' 当我用hibernate尝试上面的模式时,一切都还可以。

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

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

这些是没有吸气剂和制定者的课程

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;
    }

3 个答案:

答案 0 :(得分:1)

这似乎有些不对劲:

C0.getSubCategory().add(C0);

不应该是:

C.getSubCategory().add(C0);

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

好的,我让它工作了

它的关键是类别映射文件:

<?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>