-->
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.  [ 7 posts ] 
Author Message
 Post subject: Wrong value attribution by EntityKey in EmbeddedId
PostPosted: Mon Jan 11, 2016 3:50 pm 
Newbie

Joined: Mon Jan 11, 2016 3:03 pm
Posts: 4
I'm using Hibernate 3.5 Native Sql in a Oracle legacy database and mapping my queries in a xml file.
When I am trying to map a entity that has a EmbeddedId like that:

Code:
<return alias="vlr" class="org.domain.myapp.entity.EntityWithEmbeddedId" >
   <return-property name="id" >      
      <return-column name="FIELD_1" />   
      <return-column name="FIELD_2"  />
      <return-column name="FIELD_3" />
       <return-column name="FIELD_4" />
   </return-property>
       ...


Hibernates retrieve the values but in the wrong properties. The debug shows this

Code:
14:50:12,296 TRACE [org.hibernate.loader.Loader] (http-localhost/127.0.0.1:8080-4) Processing result set
14:50:12,297 DEBUG [org.hibernate.loader.Loader] (http-localhost/127.0.0.1:8080-4) Result set row: 0
14:50:12,298 TRACE [org.hibernate.type.descriptor.sql.BasicExtractor] (http-localhost/127.0.0.1:8080-4) Found [19] as column [FIELD_1]
14:50:12,300 TRACE [org.hibernate.type.descriptor.sql.BasicExtractor] (http-localhost/127.0.0.1:8080-4) Found [199] as column [FIELD_2]
14:50:12,301 TRACE [org.hibernate.type.descriptor.sql.BasicExtractor] (http-localhost/127.0.0.1:8080-4) Found [4] as column [FIELD_3]
14:50:12,302 TRACE [org.hibernate.type.descriptor.sql.BasicExtractor] (http-localhost/127.0.0.1:8080-4) Found [201504] as column [FIELD_4]
14:50:12,304 DEBUG [org.hibernate.loader.Loader] (http-localhost/127.0.0.1:8080-4) Result row: EntityKey[org.domain.myapp.entity.EntityWithEmbeddedId#component[FIELD_4,FIELD_1,FIELD_3,FIELD_2]{field4=19, field1=199, field2=201504, field3=4}]


I changed the order of the fields to reflect the EntityKey but I'd like to know how hibernate determines the entry order of the fields of a EmbeddedId.

This is my EmbeddedId's class
Code:
@SuppressWarnings("serial")
@Embeddable
public class MyEmbeddedId implements java.io.Serializable {

   private Long field1;
   private Long field2;
   private Long field3;
   private Long field4;

   //Getters and Setters
}


And this is how a use in the EntityWithEmbeddedId
Code:
   ......
   @EmbeddedId
   @AttributeOverrides({
         @AttributeOverride(name = "field1", column = @Column(name = "FIELD_1", nullable = false, scale = 0)),
         @AttributeOverride(name = "field2", column = @Column(name = "FIELD_2", nullable = false, scale = 0)),
         @AttributeOverride(name = "field3", column = @Column(name = "FIELD_3", nullable = false, scale = 0)),
         @AttributeOverride(name = "field4", column = @Column(name = "FIELD_4", nullable = false, scale = 0)) })
   @NotNull
   public MyEmbeddedId getId() {
      return this.id;
   }
   ......


Top
 Profile  
 
 Post subject: Re: Wrong value attribution by EntityKey in EmbeddedId
PostPosted: Tue Jan 12, 2016 4:23 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Why do you mix JPA annotations with hbm.xml mapping for SQL. JPA also have support for mapping named native queries:

http://www.thoughts-on-java.org/result-set-mapping-basics/


Top
 Profile  
 
 Post subject: Re: Wrong value attribution by EntityKey in EmbeddedId
PostPosted: Tue Jan 12, 2016 10:05 am 
Newbie

Joined: Mon Jan 11, 2016 3:03 pm
Posts: 4
My app uses JPA for normal functions, but I'm using that query for a batch processing like this :

Code:
statelessSession = ((Session) entityManager.getDelegate()).getSessionFactory().openStatelessSession();
ArrayList<EntityWithEmbeddedId> entities = (ArrayList<EntityWithEmbeddedId>) statelessSession
   .getNamedQuery("NamedQueryForEntityWithEmbeddedId")
   .setParameter("param1", param1)
   .list();


Top
 Profile  
 
 Post subject: Re: Wrong value attribution by EntityKey in EmbeddedId
PostPosted: Tue Jan 12, 2016 10:54 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Can you add rge full configuration of the sql-query?


Top
 Profile  
 
 Post subject: Re: Wrong value attribution by EntityKey in EmbeddedId
PostPosted: Tue Jan 12, 2016 12:25 pm 
Newbie

Joined: Mon Jan 11, 2016 3:03 pm
Posts: 4
My xml file looks like this:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
   <sql-query name="NamedQueryForEntityWithEmbeddedId">
      <return alias="vlr" class="org.domain.myapp.entity.EntityWithEmbeddedId" >
         <return-property name="id" >      
            <return-column name="FIELD_1" />   
            <return-column name="FIELD_2"  />
            <return-column name="FIELD_3" />
             <return-column name="FIELD_4" />
         </return-property>
      </return>
       <![CDATA[
      select vlr.ESTRUTURA_ID as FIELD_1,
           vlr.VARIAVEL_ID as FIELD_2,
           0 as FIELD_3,
           vlr.ANOMES as FIELD_4,
           str.INSTANCIA_ID as FIELD_5,
            vlr.ANO as FIELD_6,
            vlr.MES as FIELD_7,
            vlr.VALOR FIELD_8,
            vlr.OBS as FIELD_9 
        from SNJ_VALORES_DAS_VARIAVEIS vlr
          left join SNJ_ESTRUTURA str on str.ID = vlr.ESTRUTURA_ID
          left join SNJ_VARIAVEL  vrv on vrv.ID = vlr.VARIAVEL_ID
          left join SNJ_ARQUIVOS  arq on arq.ID = -1
       where vlr.ESTRUTURA_ID = :estruturaId
         and (:instanciaId = -1)
         and vlr.VARIAVEL_ID in ( select cmp.COMPONENTE_ID from SNJ_COMPONENTES cmp 
                                  where cmp.PRINCIPAL_ID = :principalId)             
         and ( vlr.ANOMES       = :periodo
               or
                vlr.ANOMES = ( select max(v.ANOMES) from SNJ_VLRS_VARIAVEL v
                               where v.GRUPO_ID = 0
                                 and v.ESTRUTURA_ID = vlr.ESTRUTURA_ID
                                 and v.VARIAVEL_ID = vlr.VARIAVEL_ID
                                 and v.ANOMES <= :periodo
                             )
               or
                 ( not exists( select * from SNJ_VLRS_VARIAVEL v
                                 where v.GRUPO_ID = 0
                                 and v.ESTRUTURA_ID = vlr.ESTRUTURA_ID
                                           and v.VARIAVEL_ID = vlr.VARIAVEL_ID
                                           and v.ANOMES <= :periodo)
                           and
                           vlr.ANOMES = ( select min(v.ANOMES) from SNJ_VLRS_VARIAVEL v
                                          where v.GRUPO_ID = 0
                                  and v.ESTRUTURA_ID = vlr.ESTRUTURA_ID
                                         and v.VARIAVEL_ID = vlr.VARIAVEL_ID
                                            and v.ANOMES >= :periodo)
                       )
           )
       order by vlr.VARIAVEL_ID ,
               vlr.ESTRUTURA_ID ,
               vlr.ANOMES
       ]]>
   </sql-query>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Wrong value attribution by EntityKey in EmbeddedId
PostPosted: Tue Jan 12, 2016 1:05 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
It looks fine, probably it's a bug. You need to open a JIRA issue and provide a test case for this.


Top
 Profile  
 
 Post subject: Re: Wrong value attribution by EntityKey in EmbeddedId
PostPosted: Tue Jan 12, 2016 1:16 pm 
Newbie

Joined: Mon Jan 11, 2016 3:03 pm
Posts: 4
Thanks for your help mihalcea_vlad.


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