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: SQLResultSetMapping + NativeQuery always create 2nd Query
PostPosted: Sat May 16, 2009 6:02 am 
Newbie

Joined: Mon Jan 12, 2009 6:54 pm
Posts: 3
Hi,

I'm using JPA 1.0 on top of Hibernate 3.3.1.GA.

I'm trying to map a custom query to a POJO. The native Query looks like:

Code:
SELECT a.nusernr as userId, a.suserid as username, b.ssexid as gender, date_part( 'year', age( b.dbirth)) as age, c.bpicture as bpicture,d.ncountrynr as ncountrynr ,d.stld as stld ,d.siso as siso ,d.bflag as bflag ,d.scountry as scountry  FROM core_usge."user" a JOIN core_usda.person b ON (a.nusernr =? and b.nusernr = a.nusernr) JOIN community.userdata c ON (c.nusernr = a.nusernr) JOIN core_geo.country d ON (d.ncountrynr = c.ncountrynr) limit ?


but always creates an additional query, although I already have all the data I want
Code:
Hibernate: select country0_.ncountrynr as ncountrynr0_0_, country0_.siso as siso0_0_, country0_.stld as stld0_0_, country0_.bflag as bflag0_0_, country0_.scountry as scountry0_0_ from core_geo.country country0_ where country0_.scountry=? and country0_.ncountrynr=?



Here's the POJO with the annotations:
Code:
@SqlResultSetMappings(
      value = {
            @SqlResultSetMapping(name = "UserStringMapping",
                  entities = {
                        @EntityResult(entityClass = UserString.class,

                              fields = {
                                    @FieldResult(name = "userId", column = "userId")
                                    , @FieldResult(name = "username", column = "username")
                                    , @FieldResult(name = "gender", column = "gender")
                                    , @FieldResult(name = "age", column = "age")

                                    , @FieldResult(name = "country.name", column = "scountry")
                                    , @FieldResult(name = "country.countryId", column = "ncountrynr")
                                    , @FieldResult(name = "country.TLD", column = "stld")
                                    , @FieldResult(name = "country.ISO", column = "siso")
                                    , @FieldResult(name = "country.flag", column = "bflag")
                              })
                  }
            )

      }
)
@Entity
public class UserString {


   private int userId = 0;
   private String username = "";
   private int age = 0;
   private boolean online = false;
   private Gender gender = null;

   private Country country;


   public UserString() {
   }

   @Id
   @Column(insertable = false, updatable = false)
   public int getUserId() {
      return userId;
   }

   public void setUserId(int userId) {
      this.userId = userId;
   }

   @Column(insertable = false, updatable = false)
   public String getUsername() {
      return username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   @Column(insertable = false, updatable = false)
   public int getAge() {
      return age;
   }

   public void setAge(int age) {
      this.age = age;
   }

   @Transient
   public boolean isOnline() {
      return online;
   }

   public void setOnline(boolean online) {
      this.online = online;
   }


   @Enumerated(EnumType.STRING)
   @Column(insertable = false, updatable = false)
   public Gender getGender() {
      return gender;
   }

   public void setGender(Gender gender) {
      this.gender = gender;
   }


   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumns(
         {
               @JoinColumn(name = "scountry", referencedColumnName = "scountry"),
               @JoinColumn(name = "ncountrynr", referencedColumnName = "ncountrynr")
         }
   )
   public Country getCountry() {
      return country;
   }

   public void setCountry(Country country) {
      this.country = country;
   }

   @Override
   public String toString() {
      return "UserString{" +
            "userId=" + userId +
            ", username='" + username + '\'' +
            ", age=" + age +
            ", online=" + online +
            ", gender=" + gender +
            ", country=" + country +
            '}';


   }
}


I think, the problem lies with the @ManyToOne and @JoinColumns annotations, but if I leave them out
I always get a
Code:
Caused by: org.hibernate.MappingException: dotted notation reference neither a component nor a many/one to one

exception.


I would be really glad if someone has a clue, how I can make this work. All the examples (Spaceship/Captain; Employee/Manager, etc.) always want that explicit lookup... I already have all the data I want in the first query but simply want to map it to the object.


Thx,
Dominik


Top
 Profile  
 
 Post subject: Re: SQLResultSetMapping + NativeQuery always create 2nd Query
PostPosted: Sun May 17, 2009 5:56 am 
Newbie

Joined: Thu May 14, 2009 10:34 am
Posts: 11
hi Dominik,
I ,also, have exactly same problem. Please, can any body answers this question? Please shed some light on this.


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.