-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate SQLQuery: resultset to many-to-many association
PostPosted: Fri Jul 20, 2012 2:08 am 
Newbie

Joined: Fri Jul 20, 2012 1:53 am
Posts: 1
I am trying to map a sql query with joins through intermediate table to an entity with many-to many association:

Code:
@Entity
@Table(name = "ATMBRANCH_LOCATIONS")
public class AtmBranchLocation extends BaseEntity<AtmBranchLocation> {     
    @ManyToMany(
    cascade = { CascadeType.PERSIST, CascadeType.MERGE },
    mappedBy = "locations",
    targetEntity = SubwayStation.class
    )
    private Set<SubwayStation> stations;
}



@Entity
@Table(name = "SUBWAY_STATIONS")
public class SubwayStation extends BaseEntity<SubwayStation> {           
    @ManyToMany(
    targetEntity = AtmBranchLocation.class,
    cascade = { CascadeType.PERSIST, CascadeType.MERGE },
    fetch = FetchType.LAZY
    )
    @JoinTable(
    name = "NEAREST_SUBWAY_STATIONS",
    joinColumns = { @JoinColumn(name = "SUBWAY_STATION_ID") },
    inverseJoinColumns = { @JoinColumn(name = "LOCATION_ID") }
    )
    private Set<AtmBranchLocation> locations;
}

final SQLQuery query1 = session.createSQLQuery(
    "select {bl.*}, {ss.*} from ATMBRANCH_LOCATIONS bl " +
    " left outer join NEAREST_SUBWAY_STATIONS nss on bl.ID = nss.LOCATION_ID " +
    " left outer join SUBWAY_STATIONS ss on ss.ID = nss.SUBWAY_STATION_ID");

query1.addEntity("bl", AtmBranchLocation.class);
query1.addJoin("ss", "bl.stations");
query1.list();


Result: org.hibernate.exception.SQLGrammarException: ORA-00904: "SS"."SUBWAY_STATION_ID": invalid identifier (because SUBWAY_STATION_ID belongs to NEAREST_SUBWAY_STATION, not SUBWAY_STATION)

Is it possible to map such an association?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.