我正在使用命名查询(hibernate 4)。实体定义如下。
@Entity
@NamedQuery(
name = "findAllProduct",
query = "SELECT PC.pincode,PO.description"
+" FROM PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION PVPOL"
+" INNER JOIN PVPOL.paymentId PID"
+" INNER JOIN PVPOL.pincode PC"
+" INNER JOIN PVPOL.paymentOptions PO"
+" where PVPOL.id = :id"
)
public class PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION extends baseEntity.Entity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="Payment_Id")
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="id")
private Set<Product_Catalog_Vendor> paymentId;
@Column(name="pincode_id")
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="pincode_id")
private Set<Pincodes> pincode;
@Column(name = "payment_options")
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="paymentOptions")
private Set<Payment_Options> paymentOptions;
//Protected setter getter here
}
Hibernate 生成下面的 sql:-
select pincode2_.pincode as col_0_0_, paymentopt3_.Description as col_1_0_
from PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION product_ve0_
inner join Product_Catalog_Vendor paymentid1_
on product_ve0_.id=paymentid1_.Id
inner join Pincodes pincode2_
on product_ve0_.id=pincode2_.pincode_id
inner join payement_options paymentopt3_
on product_ve0_.id=paymentopt3_.payment_options
where product_ve0_.id=?
代替
select pincode2_.pincode as col_0_0_, paymentopt3_.Description as col_1_0_
from PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION product_ve0_
INNER JOIN product_catalog_vendor paymentid1_
ON **product_ve0_.payment_id = paymentid1_.id**
INNER JOIN PINCODES pincode2_
ON **product_ve0_.pincode_id = pincode2_.pincode_id**
INNER JOIN payement_options paymentopt3_
ON **product_ve0_.payment_options=paymentopt3_.payment_options**
where product_ve0_.id=1;
Product_catalog_vendor 类:
@Entity
public class Product_Catalog_Vendor extends baseEntity.Entity {
@Id
@Column(name="Id")
private int id ;
//Setters and getters here
}
密码实体:
@Entity
public class Pincodes extends baseEntity.Entity {
@Id
private int pincode_id;
@Column(name="pincode")
private int pincode;
//Setters and getters here
}
payment_options 下面的实体:
@Entity
@Table(name="payement_options")
public class Payment_Options extends baseEntity.Entity {
@Id
@Column(name="payment_options")
private int paymentOptions;
//Setter getter
}
我在许多网站上搜索过,但无法找到幕后的原因。如果我做错了什么,请给我建议。一些好的参考将不胜感激。谢谢