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