-->
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.  [ 11 posts ] 
Author Message
 Post subject: Can hibernate map an array of objects?
PostPosted: Tue Mar 07, 2006 3:23 pm 
Newbie

Joined: Tue Mar 07, 2006 3:15 pm
Posts: 17
Hi

Looked in the manual and have searched this forum but no answer :-(

I have two classes, lets call them A and B. They look like this

Code:
public class A {

private B[] bs;

....

}

public class B {

private A a;

}


Can hibernate map this?

I know that using some sort of collection instead of an array is a better way, but I have my reasons for using an array. Does anyone know how to map this?


Top
 Profile  
 
 Post subject: array and primitive-array
PostPosted: Tue Mar 07, 2006 4:37 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
<array name="addresses"
table="PersonAddress"
cascade="persist">
<key column="personId"/>
<list-index column="sortOrder"/>
<many-to-many column="addressId" class="Address"/>
</array>

http://www.hibernate.org/hib_docs/v3/re ... tions.html

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 5:34 am 
Newbie

Joined: Tue Mar 07, 2006 3:15 pm
Posts: 17
Thanks - that did solve my problem..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 9:39 am 
Newbie

Joined: Tue Mar 07, 2006 3:15 pm
Posts: 17
One more thing. The index or list-index column. Do I really have to have this field in the DB? How can I tell hibernate to not care about this?

I do not really care about the ordering of index of the array. I just want hibernate to persist an array without caring about the ordering. Is there a way I can tell this to hibernate?


Top
 Profile  
 
 Post subject: index
PostPosted: Wed Mar 08, 2006 11:24 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
yes, it must be in the database because arrays and lists are ordered collections and Hibernate preserves their order. If you do not care about order you should use set.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 9:13 am 
Newbie

Joined: Tue Mar 07, 2006 3:15 pm
Posts: 17
Ok. More problems with this one ;-)

I got this mapping now:

Code:
<array name="adresses" cascade="save-update">
   <key column="ORG_ID"/>
   <index column="A_INDEX"/>
   <one-to-many class="AdressDO"/>
</array>



The ID in Organisation (ORG_ID) is mapped as an Identity column in MS-SQL and the same for the ID in the address table.

It inserts the org (the parent) and all the addresses but the foreign key in the address table (ORG_ID) is NULL. Why does it not insert this key?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 12:45 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Please provide full mapping for the array enclosing entity, I presume it is Organization class.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 13, 2006 5:16 am 
Newbie

Joined: Tue Mar 07, 2006 3:15 pm
Posts: 17
Here is the full mapping for the Organisation class, Address class and the Organisation.java file

Code:
<hibernate-mapping package="data">
   <class name="OrganisasjonDO" table="ORGANISASJONS_BA">
      <id name="id" column="ORGANISASJONS_ID">
         <generator class="identity"/>
      </id>
      <property name="navn">
         <column name="NAVN"/>
      </property>
      <property name="orgnummer">
         <column name="ORGANISASJONS_NUMMER"/>
      </property>
      <array name="adresser" cascade="save-update" table="ADRESSE_BA">
         <key column="ORGANISASJONS_ID"/>
         <index column="A_INDEX"/>
         <one-to-many class="AdresseDO"/>
      </array>
      <property name="kundenummer">
         <column name="KUNDE_NUMMER"/>
      </property>
   </class>
</hibernate-mapping>

<hibernate-mapping package="data">
   <class name="AdresseDO" table="ADRESSE_BA" >
      <id name="id" column="ADRESSE_ID" >
         <generator class="identity"/>
      </id>
      <version column="VERSJONS_NR" name="versjon"/>
      <property name="gate" column="GATE"/>
      <property name="postnummer" column="POSTNUMMER"/>
      <property name="poststed" column="POSTSTED"/>
      <property name="faktura" column="FAKTURA"/>
      <property name="index" column="A_INDEX"/>
   </class>
</hibernate-mapping>

package data;

import java.io.Serializable;


public class OrganisasjonDO extends KundeDO {

    private java.lang.Integer id;
    private java.lang.String navn;
    private java.lang.String orgnummer;
   
    private AdresseDO[] adresser = new AdresseDO[1];
   

private EnhetDO[] enheter;

   private SelskapsOffertDO[] selskapsofferter;
   
   private PersonDO kontakt;
   

    public OrganisasjonDO() {
    }




   /**
    * @return
    */
   public AdresseDO[] getAdresser() {
      return adresser;
   }

   /**
    * @return
    */
   public java.lang.Integer getId() {
      return id;
   }

   /**
    * @return
    */
   public java.lang.String getNavn() {
      return navn;
   }

   /**
    * @return
    */
   public java.lang.String getOrgnummer() {
      return orgnummer;
   }

   /**
    * @param adresseDOs
    */
   public void setAdresser(AdresseDO[] adresseDOs) {
      int i = 0;
      while(i<adresseDOs.length) {
         ((AdresseDO) adresseDOs[i]).setIndex(i);
         i++;      
      }
      adresser = adresseDOs;
   }

   /**
    * @param integer
    */
   public void setId(java.lang.Integer integer) {
      id = integer;
   }

   /**
    * @param string
    */
   public void setNavn(java.lang.String string) {
      navn = string;
   }

   /**
    * @param string
    */
   public void setOrgnummer(java.lang.String string) {
      orgnummer = string;
   }

   /**
    * @return
    */
   public EnhetDO[] getEnheter() {
      return enheter;
   }

   /**
    * @param enhetDOs
    */
   public void setEnheter(EnhetDO[] enhetDOs) {
      enheter = enhetDOs;
   }

   /**
    * @return
    */
   public SelskapsOffertDO[] getSelskapsofferter() {
      return selskapsofferter;
   }

   /**
    * @param offertDOs
    */
   public void setSelskapsofferter(SelskapsOffertDO[] offertDOs) {
      selskapsofferter = offertDOs;
   }

   /**
    * @return
    */
   public PersonDO getKontakt() {
      return kontakt;
   }

   /**
    * @param personDO
    */
   public void setKontakt(PersonDO personDO) {
      kontakt = personDO;
   }

}







Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 6:09 am 
Newbie

Joined: Tue Mar 07, 2006 3:15 pm
Posts: 17
Does anyone have an idea why this does not work? Really need to get it to work, it will save me a lot of time..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 8:06 am 
Newbie

Joined: Tue Mar 07, 2006 3:15 pm
Posts: 17
I know;-) The solution seems to be to add not-null="true" to the foreign key column in the array mapping. Like this:

<array name="adresser" cascade="all">
<key column="ORGANISASJONS_ID" not-null="true"/>
<index column="A_INDEX" />
<one-to-many class="AdresseDO"/>
</array>

Is this a bug or just me using something wrong?


Top
 Profile  
 
 Post subject: Re: array and primitive-array
PostPosted: Tue Apr 04, 2006 8:00 am 
Newbie

Joined: Fri Mar 24, 2006 9:31 am
Posts: 10
Location: Berlin, Germany
[quote="kgignatyev"]<array name="addresses"
table="PersonAddress"
cascade="persist">
<key column="personId"/>
<list-index column="sortOrder"/>
<many-to-many column="addressId" class="Address"/>
</array>

How can I achieve this mapping by using ejb3 annotations ?

I have a class A and B like the following:
Code:
public class A {

private B[] myBs;

public B[] getB() {
   return myBs;
}

...


Which annotations I need to apply to the code to get the class B stored in a separate table.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.