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.  [ 2 posts ] 
Author Message
 Post subject: one-to-many indexed List not working
PostPosted: Tue Feb 19, 2008 12:25 pm 
Newbie

Joined: Tue Feb 19, 2008 11:42 am
Posts: 2
Hibernate version:3.2.1

Name and version of the database you are using:Oracle 10G

Mapping documents:

Code:
<hibernate-mapping>
   <class name="com.sinfic.ipdms.example.domain.Ecra1" table="ECRA1">
      <composite-id class="com.sinfic.ipdms.example.domain.Ecra1Id"
         mapped="false" name="id" unsaved-value="undefined">
         <key-property name="numeroconteudo" type="big_decimal">
            <column name="NUMEROCONTEUDO" precision="22" scale="0" />
         </key-property>
         <key-property name="versaoconteudo" type="big_decimal">
            <column name="VERSAOCONTEUDO" precision="22" scale="0" />
         </key-property>
      </composite-id>
      <property generated="never" lazy="false" name="descricao"
         type="string">
         <column length="100" name="DESCRICAO" />
      </property>
      <property generated="never" lazy="false"
         name="numeroconteudosel" type="big_decimal">
         <column name="NUMEROCONTEUDOSEL" precision="22" scale="0" />
      </property>
      <property generated="never" lazy="false"
         name="versaocontuedosel" type="big_decimal">
         <column name="VERSAOCONTUEDOSEL" precision="22" scale="0" />
      </property>
      <list name="ecra2s" lazy="true" inverse="false">
         <key>
            <column name="NUMEROCONTEUDO" not-null="true"
               precision="22" scale="0" />
            <column name="VERSAOCONTEUDO" not-null="true"
               precision="22" scale="0" />
         </key>
         <list-index column="IDX" base="1" />
         <one-to-many class="com.sinfic.ipdms.example.domain.Ecra2" />
      </list>
   </class>
</hibernate-mapping>


Code:
<hibernate-mapping>
   <class name="com.sinfic.ipdms.example.domain.Ecra2" table="ECRA2">
      <composite-id>
         <key-many-to-one name="ecra1"
            class="com.sinfic.ipdms.example.domain.Ecra1">
            <column name="numeroconteudo" />
            <column name="versaoconteudo" />
         </key-many-to-one>
         <key-property name="idx" type="int">
            <column name="IDX" precision="22" scale="0" />
         </key-property>
      </composite-id>
      <property name="descricao" type="string">
         <column name="DESCRICAO" length="100" />
      </property>
   </class>
</hibernate-mapping>



Java Code:

Code:
public class Ecra1 implements java.io.Serializable {

   private Ecra1Id id;
   private String descricao;
   private BigDecimal numeroconteudosel;
   private BigDecimal versaocontuedosel;
   private List<Ecra2> ecra2s = new ArrayList<Ecra2>(0);

   public Ecra1() {
   }

   public Ecra1(Ecra1Id id) {
      this.id = id;
   }

   public Ecra1(Ecra1Id id, String descricao, BigDecimal numeroconteudosel,
         BigDecimal versaocontuedosel, List<Ecra2> ecra2s) {
      this.id = id;
      this.descricao = descricao;
      this.numeroconteudosel = numeroconteudosel;
      this.versaocontuedosel = versaocontuedosel;
      this.ecra2s = ecra2s;
   }

   public Ecra1Id getId() {
      return this.id;
   }

   public void setId(Ecra1Id id) {
      this.id = id;
   }

   public String getDescricao() {
      return this.descricao;
   }

   public void setDescricao(String descricao) {
      this.descricao = descricao;
   }

   public BigDecimal getNumeroconteudosel() {
      return this.numeroconteudosel;
   }

   public void setNumeroconteudosel(BigDecimal numeroconteudosel) {
      this.numeroconteudosel = numeroconteudosel;
   }

   public BigDecimal getVersaocontuedosel() {
      return this.versaocontuedosel;
   }

   public void setVersaocontuedosel(BigDecimal versaocontuedosel) {
      this.versaocontuedosel = versaocontuedosel;
   }

   public List<Ecra2> getEcra2s() {
      return this.ecra2s;
   }

   public void setEcra2s(List<Ecra2> ecra2s) {
      this.ecra2s = ecra2s;
   }
}

Code:
public class Ecra2 implements java.io.Serializable {

   /**
    *
    */
   private static final long serialVersionUID = 3531229132518791450L;
   private Ecra1 ecra1;
   private String descricao;
   private int idx;

   public Ecra2() {
   }

   public Ecra2(Ecra1 ecra1, int idx) {
      this.ecra1 = ecra1;
   }

   public int getIdx() {
      return this.idx;
   }

   public void setIdx(int idx) {
      this.idx=idx;
   }

   public String getDescricao() {
      return this.descricao;
   }

   public void setDescricao(String descricao) {
      this.descricao = descricao;
   }
   public Ecra1 getEcra1() {
      return ecra1;
   }

   public void setEcra1(Ecra1 ecra1) {
      this.ecra1 = ecra1;
   }
}


The generated SQL (show_sql=true):
Code:
Hibernate: insert into ECRA1 (DESCRICAO, NUMEROCONTEUDOSEL, VERSAOCONTUEDOSEL, NUMEROCONTEUDO, VERSAOCONTEUDO) values (?, ?, ?, ?, ?)
Hibernate: insert into ECRA1 (DESCRICAO, NUMEROCONTEUDOSEL, VERSAOCONTUEDOSEL, NUMEROCONTEUDO, VERSAOCONTEUDO) values (?, ?, ?, ?, ?)
Hibernate: update ECRA2 set NUMEROCONTEUDO=?, VERSAOCONTEUDO=?, IDX=? where numeroconteudo=? and versaoconteudo=? and IDX=?
Hibernate: update ECRA2 set NUMEROCONTEUDO=?, VERSAOCONTEUDO=?, IDX=? where numeroconteudo=? and versaoconteudo=? and IDX=?


Hi, I’ve a one-to-many mapped relation between Ecra1 and Ecra2, where Ecra1 acts as the parent and Ecra2 as the child.

I want to pass the numeroconteudo and versaoconteudo from ecra1 to ecra2 and to use the idx as index of the collection.

I got the serialization part of it working with a <composite-element instead of a <one-to-many in

Code:
      <list name="ecra2s" lazy="true" inverse="false">
         <key>
            <column name="NUMEROCONTEUDO" not-null="true"
               precision="22" scale="0" />
            <column name="VERSAOCONTEUDO" not-null="true"
               precision="22" scale="0" />
         </key>
         <list-index column="IDX" base="1" />
         <one-to-many class="com.sinfic.ipdms.example.domain.Ecra2" />
      </list>


But then I got problems in loading the Collection from the database... anyway i think the correct way of doing it is with a <one-to-many the problem is that a got updates on ecra2 instead of inserts...

Any ideas? Thanks in advanced...

_________________
João Pedro


Top
 Profile  
 
 Post subject: Problem assigning FK to Childs
PostPosted: Wed Feb 20, 2008 11:38 am 
Newbie

Joined: Tue Feb 19, 2008 11:42 am
Posts: 2
Ok, so I kept on trying to solve the problem and I think I got closer to the solution. The actual situation is the now Hibernate will serialize the elements in the List, if I supply the rest of the pk apart of the idx...

The mapping of the list is now:

Code:
      <list name="ecra2s" lazy="false" inverse="false" cascade="all">
         <key>
            <column name="NUMEROCONTEUDO" not-null="true"
               precision="22" scale="0" />
            <column name="VERSAOCONTEUDO" not-null="true"
               precision="22" scale="0" />
         </key>
         <list-index column="IDX" base="0" />
         <one-to-many class="com.sinfic.ipdms.example.domain.Ecra2"/>
      </list>


and when I commit the transaction a get the following error:

Code:
Hibernate: insert into ECRA1 (DESCRICAO, NUMEROCONTEUDOSEL, VERSAOCONTUEDOSEL, NUMEROCONTEUDO, VERSAOCONTEUDO) values (?, ?, ?, ?, ?)
Hibernate: insert into ECRA2 (DESCRICAO, numeroconteudo, versaoconteudo, IDX) values (?, ?, ?, ?)
IPDMS[2008-02-20 15:17:26]  WARN (JDBCExceptionReporter.java:77) SQL Error: 1400, SQLState: 23000
IPDMS[2008-02-20 15:17:26] ERROR (JDBCExceptionReporter.java:78) ORA-01400: cannot insert NULL into ("PIPS"."ECRA2"."NUMEROCONTEUDO")


It seems that the ecra1 is not setting the values for numeroconteudo and versoaconteudo in ecra2...

If I do e2.setEcra1(e1); before committing the session it works fine, but this way I’m manually assigning part of the pk, which is the fk to ecra1...

Any ideas?

Thanks

_________________
João Pedro


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