簡體   English   中英

Java Hibernate查詢未返回所有結果

[英]Java hibernate query not returning all results

我正在申請購物籃秋千。 我有以下兩個類和映射文件:

產品.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個。 它不返回的結果是product中的OfferID = null的結果。 我嘗試了查詢中where子句的各種變體,但均未成功。

任何幫助,將不勝感激。

經過數小時的搜索,我問了一個問題后便自己解決了。 當我更改時,查詢返回所有結果:

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"); 
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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM