I am trying to join 2 tables, user and hosting_platform, using following HQL query. hosting_platform table has a FK reference to user table. Database is postgresql.
select u.id, u.userName, p.platform.id, p.itemPrice, p.revenueShare
from User u left join HostingPlatform p on u.id = p.platform.id AND
p.item.id = :itemId where u.userType = 'Platform'
I am not getting expected results from above query because hibernate is not generating proper SQL query from above HQL. Following plain SQL is generated by Hibernate.
select
user0_.id as col_0_0_,
user0_.user_name as col_1_0_,
hostingpla1_.id as col_2_0_,
hostingpla1_.item_price as col_3_0_,
hostingpla1_.revenue_share as col_4_0_
from
users user0_
left outer join
user_role user0_1_
on user0_.id=user0_1_.user_id
left outer join
hosting_platform hostingpla1_
on (
user0_.id=hostingpla1_.id
and hostingpla1_.item_id=?
)
where
user0_.user_type='Platform'
Based on my HQL join should happen on user0_.id=hostingpla1_.platform_id but Hibernate is doing it on user0_.id=hostingpla1_.id.
HostingPlatform.java definition is as
@Entity
@Table(name="hosting_platform")
public class HostingPlatform {
private static final long serialVersionUID = 6311364761937265306L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hosting_platform_id_seq")
@SequenceGenerator(name = "hosting_platform_id_seq", sequenceName = "hosting_platform_id_seq", allocationSize=1)
@Column(name = "id")
private Long id;
@JoinColumn(name = "platform_id")
private User platform;
@ManyToOne
@JoinColumn(name="item_id")
private Item item;
@Column(name = "item_price")
private Double itemPrice;
@Column(name = "revenue_share")
private Double revenueShare;
}
Thank you.
I was missing @ManyToOne annotation on 'platform' field in HostingPlatform.java. Problem resolved after adding annotation
@ManyToOne
@JoinColumn(name = "platform_id")
private User platform;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.