-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Unidirectional one-to-many association, again!
PostPosted: Sun Jan 04, 2009 1:56 pm 
Newbie

Joined: Fri Nov 07, 2008 1:57 pm
Posts: 8
If I have, for example, database situation like this:

Image

How can I make unidirectional one-to-many association with List collection? Keep in mind that I can't change the database, and that I must use List collection! I already tried several examples which were suggested in documentation (like 7.2.3 and 7.4.1), and I always get the following exception:

Repeated column in mapping for entity: Address column: addressId (should be mapped with insert="false" update="false")

Please, suggest me a solution?!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2009 2:12 pm 
Newbie

Joined: Fri Nov 07, 2008 1:57 pm
Posts: 8
These are examples that I tried:

1) Based on 7.2.3. example from documentation:

Code:
    <class name="Person">
        <id column="personId" name="id">
            <generator class="native"/>
        </id>
        <list name="addresses" lazy="false">
            <key column="personId" not-null="true" />
            <list-index base="1" column="addressId" />
            <one-to-many class="Address" />
        </list>
    </class>

    <class name="Address">
        <id column="addressId" name="id">
            <generator class="native"/>
        </id>
    </class>


2) Based on 7.4.1. example from documentation:

Code:
    <class name="Person">
        <id column="personId" name="id">
            <generator class="native"/>
        </id>
        <list name="addresses" lazy="false">
            <key column="personId" not-null="true" />
            <list-index base="1" column="addressId" />
            <one-to-many class="Address" />
        </list>
    </class>

    <class name="Address">
        <id column="addressId" name="id">
            <generator class="native"/>
        </id>

        <many-to-one column="personId" name="person" not-null="true" insert="false" update="false" />
    </class>


Both examples produce exception:

Repeated column in mapping for entity: Address column: addressId (should be mapped with insert="false" update="false")


Top
 Profile  
 
 Post subject: Unidirectional one-to-many association, again!
PostPosted: Mon Jan 05, 2009 12:11 am 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
Hello medi, i think that you are misunderstanding the purpose of the list-index attribute of the list element. It is used not to persist the id of the entity (address) but to store the index itself of the list of the association. Because of this you have to name this column something like address_idx or something that you like here are some more information about the usage of this attribute, http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections-mapping.html#collections-indexed. If you can't change the database to reflect the need of an extra column then you should use a bag or a set element instead http://www.hibernate.org/hib_docs/v3/reference/en-US/html/collections-advancedmappings.html.

regards,


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2009 4:19 am 
Newbie

Joined: Fri Nov 07, 2008 1:57 pm
Posts: 8
Thanks.

<bag> collection mapping solved the problem. I thought, until now, that <list> collection mapping was the only way to map to a List collection in persistent classes. This helped me a lot.


Top
 Profile  
 
 Post subject: Re: Unidirectional one-to-many association, again!
PostPosted: Mon Nov 21, 2011 3:25 am 
Newbie

Joined: Fri Jun 05, 2009 3:01 am
Posts: 4
I too tried to use <bag> as the collection being passed can have duplicates. However I am getting the error:

Caused by: net.sf.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.my.services.billing.model.MYCustomerBill.Id
..
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class

Below is my hibernate mapping.

<hibernate-mapping>

<class name="com.my.services.billing.model.MYCustomerBill" table="MY_CUSTOMER_BILL" mutable="true">
<id name="Id" column="CUST_BILL_ID" type="long">
<generator class="assigned"/>
</id>
<property name="BillingMsgId" column="BILLING_MSG_ID" type="long" not-null="false"/>
<property name="PeriodStartDate" column="PERIOD_START_DATE" type="date" not-null="false"/>
<property name="PeriodEndDate" column="PERIOD_END_DATE" type="date" not-null="false"/>
<property name="TranslatorId" column="TRANSLATOR_ID" type="string" length="11" not-null="false"/>
<property name="TotalAmtExTaxCents" column="TOT_AMT_EX_TAX_CENTS" type="double" not-null="false"/>
<property name="Status" column="BILL_STATUS" type="com.my.services.billing.model.MYCustomerBillStatus" not-null="false"/>
<bag name="Items" cascade="all">
<key column="CUST_BILL_ID" />
<one-to-many class="com.my.services.billing.model.MYBillItem" />
</bag>
</class>

<class name="com.my.services.billing.model.MYBillItem" table="MY_BILL_ITEM" mutable="true">
<id name="Id" column="BILL_ITEM_ID" type="long">
<generator class="assigned"/>
</id>
<!--many-to-one name="BillId" class="com.my.services.billing.model.MYCustomerBill" fetch="select"-->
<many-to-one name="BillId" class="com.my.services.billing.model.MYCustomerBill">
<column name="CUST_BILL_ID" not-null="true" />
</many-to-one>
<property name="ServiceCode" column="SERVICE_CODE" type="string"/>
<property name="FeeType" column="FEE_TYPE" type="string"/>
<property name="FeeSubType" column="FEE_SUB_TYPE" type="string"/>
<property name="Provider" column="PROVIDER" type="string"/>
<property name="Qty" column="QUANTITY" type="long"/>
<property name="AmtExTaxCentsTotal" column="AMT_EX_TAX_TOTAL" type="double"/>
<property name="AmtExTaxCentsPerItem" column="AMT_EX_TAX_PER_ITEM" type="double"/>
<bag name="Discounts" cascade="all">
<key column="BILL_ITEM_ID" />
<one-to-many class="com.my.services.billing.model.MYBillDiscountItem" />
</bag>
</class>

<class name="com.my.services.billing.model.MYBillDiscountItem" table="MY_ITEM_DISCOUNT" mutable="true">
<id name="Id" column="ITEM_DISC_ID" type="long">
<generator class="assigned"/>
</id>
<!--many-to-one name="BillId" class="com.my.services.billing.model.MYCustomerBill" fetch="select"-->
<many-to-one name="ItemId" class="com.my.services.billing.model.MYBillItem">
<column name="BILL_ITEM_ID" not-null="true" />
</many-to-one>
<property name="DiscountTypeId" column="DISCOUNT_TYPE_ID" type="long"/>
<property name="AmtCentsExTax" column="DISC_AMT_EX_TAX_CENTS" type="double"/>
<property name="Percentage" column="DISC_PERCENTAGE NUMBER" type="double"/>
</class>

</hibernate-mapping>

Don't think passing a LinkedLists inside the objects in place of bags can be a problem. This is bit urgent for me. Please help if anyone has gone through a similar issue.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.