我正在制作购物篮摇摆应用程序。我有以下两个类和映射文件:
Product.java
public class Product implements java.io.Serializable {
private Integer productID;
private Integer offerID;
private String productName;
private BigDecimal unitPrice;
private static SessionFactory factory;
private Offer offer;
//getters+setters
产品映射文件:
<hibernate-mapping>
<class name="shoppingbasket.Product" table="products">
<id name="productID" type="java.lang.Integer" access="field">
<column name="ProductID" />
<generator class="assigned" />
</id>
<many-to-one name="Offer" class="shoppingbasket.Offer" fetch="select">
<column name="OfferID" not-null="false" />
</many-to-one>
<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>
</class>
</hibernate-mapping>
Offer.java
public class Offer
{
private Integer offerID;
private String offerDescription;
private String shortDescription;
private Integer TFTPOTGroup;
private Double discountPercentage;
private Set<Offer> offer;
// getters+setters
提供映射文件:
<hibernate-mapping>
<class name="shoppingbasket.Offer" table="offers">
<id name="offerID" type="java.lang.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>
<set name="offer" table="products"
inverse="true" lazy="true" fetch="select">
<key>
<column name="OfferID" not-null="true" />
</key>
<one-to-many class="shoppingbasket.Product" />
</set>
</class>
最后是getProducts()函数
public List<Product> getProducts() {
factory = (new Configuration()).configure().buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
List<Product> products = new ArrayList<Product>();
Product q = new Product();
Query query = session.createQuery("select p from Product p JOIN p.Offer where p.Offer = Offer");
List<Product> list = query.list();
Iterator<Product> iter = list.iterator();
while (iter.hasNext()) {
Product product = iter.next();
System.out.println(product.toString());
products.add(product);
}
System.out.println(products);
return products;
}
我不知道我做错了什么,但查询只返回了16个结果中的11个。它没有返回的结果是productsID in products = null的结果。我已经尝试了查询中where子句的各种变体,但没有成功。
任何帮助将不胜感激。
答案 0 :(得分:0)
我一问完这个问题,经过几个小时的搜索,我自己就解决了。我改变时查询返回所有结果:
Query query = session.createQuery("select p from Product p JOIN p.Offer o where p.Offer=o.offerID or p.Offer is null");
为:
Query query = session.createQuery("select p from Product p LEFT OUTER JOIN p.Offer");
答案 1 :(得分:0)
I think it's about data type and your join does not work properly. You can use
int, string ... primitive in hibernate mapping
EX:
private int productID;
...
<id name="offerID" type="int" access="field">
<column name="OfferID" />
<generator class="assigned" />
</id>
<property name="offerDescription" type="string" access="field">
<column name="OfferDescription" length="60" not-null="true"/>
</property>