java.lang.Object继承;无法投射到shoppingbasket.Product

时间:2017-12-11 22:21:48

标签: java hibernate hibernate-mapping

我正在尝试检索与他们相关的优惠产品列表。在遍历查询结果之后,我希望能够使用products类中的getters / setter,但我知道它不起作用,因为查询没有返回Product的实例。

获取产品的功能:

public List<Product> getProducts() {
    factory = (new Configuration()).configure().buildSessionFactory();
    Session session = factory.getCurrentSession();

    session.beginTransaction();

    List<Product> products = new ArrayList<Product>();

    Query query = session.createQuery("from Product p INNER JOIN p.offers"); 
    //The castList is declared and explained at the bottom of listing
    List<Product> list =  query.list();

    Iterator<Product> iter = list.iterator();

    while (iter.hasNext()) {
        Product product = iter.next();
        System.out.println(product);
    }
}

提供的Hibernate映射:

<hibernate-mapping>
<class name="shoppingbasket.Offer" table="offers">
    <id name="offerID" type="integer" access="field">
        <column name="OfferID" />
        <generator class="assigned" />
    </id>
    <property name="offerDescription" type="java.lang.String" access="field">
        <column name="OfferDescription" length="60" not-null="true"/>
    </property>
    <property name="shortDescription" type="java.lang.String" access="field">
        <column name="ShortDescription" length="10" not-null="false"/>
    </property>
    <property name="TFTPOTGroup" type="java.lang.Integer" access="field">
        <column name="TFTPOTGroup" length="4" not-null="false" default="null"/>
    </property>
    <property name="discountPercentage" type="java.lang.Double" access="field">
        <column name="DiscountPercentage"  not-null="false" default="null"/>
    </property>
</class>

产品的Hibernate映射:

<hibernate-mapping>
<class name="shoppingbasket.Product" table="products">
    <id name="productID" type="integer" access="field">
        <column name="ProductID" />
        <generator class="assigned" />
    </id>
    <property name="offerID" type="java.lang.Integer" access="field">
        <column name="OfferID" />
    </property>
    <property name="productName" type="java.lang.String" access="field">
        <column name="ProductName" length="40" not-null="true"/>
    </property>
    <property name="unitPrice" type="java.math.BigDecimal" access="field">
        <column name="UnitPrice"/>
    </property>
    <one-to-one name="offers" class="shoppingbasket.Offer" />
</class>

产品类别:

public class Product implements java.io.Serializable {
private Integer productID;
private Integer offerID;
private String productName;
private BigDecimal unitPrice;
private Offer offer;
public Integer getProductID() {
    return productID;
}
public void setProductID(Integer productID) {
    this.productID = productID;
}
public Integer getOfferID() {
    return this.offerID;
}
public void setOfferID(Integer offerID) {
    this.offerID = offerID;
}
public Offer getOffers() {
    return offer;
}
public void setOffers(Offer offer) {
    this.offer = offer;
}
//more getters/setters
}

提供课程:

public class Offer
{
private Integer offerID;
private String offerDescription;
private String shortDescription;
private Integer TFTPOTGroup;
private Double discountPercentage;
public Integer getOfferID() {
    return offerID;
}
public void setOfferID(Integer offerID) {
    this.offerID = offerID;
}
//more getters/setters

}

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

#Potential Unnecessary Projections Data:

由于您未在查询中指定 SELECT 子句并放置显式连接,因此hibernate将每行返回2个对象(产品,商品),其中由于基础映射关联,可能已经使用商品数据填充了产品对象。

尝试在查询中添加 select 子句,看看它是否正确投射

Add SELECT p FROM ... to the query