我正在使用命名查询(hibernate 4).Entity定义如下。
@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
}
在sql下生成Hibernate: -
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
}
Pincodes实体:
@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
}
我在很多网站上搜索过但无法找到幕后的原因。如果我做错了,请给我建议。一些好的参考将不胜感激。感谢
答案 0 :(得分:0)
只是为了让你的问题正确,你的查询加入paymentid1_.Id而不是paymentid1_.id?或者我错过了预期和真实查询之间的差异?
我不是专业人士,但只是猜测我会说你的查询加入了Product_Catalog_Vendor的id:
@Id
@Column(name="Id")
private int id ;
因为这就是为什么它的 Id 而不是id ...
答案 1 :(得分:0)
我认为由于单向映射你根本不需要mappedBy,或者无论如何,你是以一种奇怪的方式使用它们。
只有当关联是双向的(不是你的情况)时才需要mappedBy
,并且应该引用一个字段,该字段的类型与声明mappedBy的实体的类型相同(而不是类似的String类型)在您的情况下。)您以mappedBy
属性
referencedColumnName
在你的例子中:
@Entity
public class Product_Catalog_Vendor extends baseEntity.Entity {
@ManyToOne
private PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION pvpol;
}
和
public class PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION extends baseEntity.Entity {
@Column(name="Payment_Id")
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="pvpol")
private Set<Product_Catalog_Vendor> paymentId;
}