0

我正在使用命名查询(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

}

我在许多网站上搜索过,但无法找到幕后的原因。如果我做错了什么,请给我建议。一些好的参考将不胜感激。谢谢

4

2 回答 2

0

只是为了纠正您的问题,您的查询加入了 paymentid1_.Id 而不是 paymentid1_.id?还是我错过了预期和实际查询之间的差异?

我不是专业人士,但只是猜测我会说您的查询正在加入 Product_Catalog_Vendor 的 id:

@Id
@Column(name="Id")
private int id ;

所以因为这就是为什么它的ID而不是 ID ...

于 2013-08-09T06:50:58.300 回答
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;
}
于 2013-08-09T07:57:11.697 回答