Search code examples
hibernategxt

How to set up the hibernate file?


I have a AAAModelData.java when is having getter and setter like :

public static final String STRING_BBB = "StringBBB";
public List<BBBModelData> getBBB() { return get(StringBBB); }
public void setBBB(List<BBB> StringBBB) { set(STRING_BBB, StringBBB); }

In Database: table_AAA

-------------------------------
|column Uid | column FoodId | column FoodName|
-------------------------------
1            1               chocolate
2            2               cake
3            3               biscuss

table_BBB

-------------------------------
|column Uid | column FoodId | column ingredient|
-------------------------------
1            1                sugar
2            1                favour
3            2                salt
4            2                coco
5            2                milk
6            3                beef
7            3                fish

Now i am using hibernate to mapping table_AAA with table_BBB in table_AAA.hbm.xml , i want to search table_BBB column B.

for example, my program want to get all ingredient (actually i need full table of table_BBB for my work) for the "FoodId" 2 cake. So i can get the table_BBB into the class BBB / BBBModelData which FoodId = 2;

But i have a very very very hard problem when using hibernate, a lots of problem occurs such as : No row with the given identifier exists, expected java.util.List , actual value xxxxxxxxx, Serialize, more and more...

I hope someone can provide the < mapping> / < set> ? to me. Here is somethings i had tried before:

<many-to-one name="BBB"
class="com.foo.gwt.shared.model.entity.BBB"
column="FoodId" lazy="false" not-null="true" />

<many-to-one name="BBB"
class="com.foo.gwt.shared.model.entity.BBB"
cascade="save-update"/>

<property name="BBB" type="java.lang.String">
<column name="FoodId" />
</property> -->

actually i tried too many example.... can't list them all out....


Solution

  • As far as I see, BBB can not be mapped as a distinct entity with the current table structure. Is this structure fix? If you want to use the structure as it is, take a look at component mapping, maybe this will help you: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch09.html#components-incollections

    <class name="com.foo.gwt.shared.model.entity.AAA" table="table_AAA">
        ...
        <list name="BBB" table="table_BBB" lazy="true">
            <key column="FoodId">
            <composite-element class="com.foo.gwt.shared.model.entity.BBB">
                <property name="ingredient"/>
            </composite-element>
        </list>
        ...
    </class>
    

    I don't know if or how you have mapped the uid columns, but you might have to take care of them to.

    If the table structure isn't fix, I would suggest two distinct tables for AAA and BBB and a third table to link them in an n:m-relation.

    (sorry for answering and not commenting, not enough reputation yet...)