2017-12-12 1 views
0

私はショッピングバスケットスイングアプリケーションを作成しています。Javaの休止状態のクエリがすべての結果を返さない

<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 

オファーマッピングファイル:

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 

製品マッピングファイルProduct.java:私は2つの次のクラスとマッピングファイルを持っています

<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を返してはいけません。返されない結果は、ProductsのOfferID = nullの場合です。私は成功のないクエリのwhere節の様々なバリエーションを試しました。

ご協力いただければ幸いです。

答えて

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