gpt4 book ai didi

java.lang.Object;无法转换到购物篮。产品

转载 作者:行者123 更新时间:2023-12-02 11:53:00 26 4
gpt4 key购买 nike

我正在尝试检索产品列表及其相关优惠。迭代查询结果后,我希望能够使用产品类中的 getter/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);
}
}

Offer 的 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

}

任何帮助将不胜感激

最佳答案

#潜在不必要的预测数据:

由于您没有在查询中指定 SELECT 子句并放置显式联接,hibernate 将每行返回 2 个对象(产品、优惠),其中由于底层映射关联,Product 对象可能已经填充了 Offer 数据。

尝试将 select 子句添加到查询中,看看它是否正确转换

Add SELECT p FROM ... to the query 

关于java.lang.Object;无法转换到购物篮。产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47762420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com