2017-12-14 6 views
0

我々は、以下のSQLで2つのテーブルを作成すると仮定すると}選択し、オーダーリストは、(内部基準は、休止状態への参加)

および

public class Product { 

private int id; 
private String name; 
private String description; 
private Double price; 
private Supplier supplier; 

public int getId() { return id;} 
public void setId(int id) { this.id = id; } 

public String getName() { return name;} 
public void setName(String name) { this.name = name;} 

public String getDescription() { return description;} 
public void setDescription(String description) { this.description = description; } 

public Double getPrice() {return price;} 
public void setPrice(Double price) { this.price = price;} 

@OneToOne(targetEntity=ProductAssignment.class, mappedBy = "supplierId", fetch = FetchType.LAZY) 
public Supplier getSupplier() { return supplier;} 
public void setSupplier(Supplier supplier) { this.supplier = supplier; } 

}

私はサプライヤーで、カウントして、すべての製品の順序を選択したい場合は、私は以下のコードを使用することができます

Criteria crit = session.createCriteria(Product.class); 
Criteria critSupplier = crit.createCriteria("supplier"); 
critSupplier.addOrder(Order.desc("count")); 

しかし、今、私は製品に価格ですべてのサプライヤーの順序を選択したいを表。

私はMySQLを使用したい場合は、以下のスクリプトです: select * from supplier s inner join product p ON s.id = p.supplierId order by p.price

今、私はJavaコードでHibernateの基準クエリにこのSQLを転送したいですか?

この場合、私を助けてください。

答えて

1

ここでは、サプライヤと製品の2つのモデルの間に双方向の関係があります。双方向の関係であるため、両方のモデルが互いに認識し合い、それらを結合するリンク(supplierId)に基づいて互いに情報を再収集する必要があるからです。また、関係は1つの(サプライヤ)-toMany(製品)

です。まず、サプライヤが関係の存在を認識している必要があるという事実はありません。あなたは、リスト製品のサプライヤーモデルを変更することで、この「意識」を表現し、それに追加する必要があります。

public class Supplier implements Serializable{ 
    private int id; 
    private String name; 
    private int count; 
    private List<Product> products; 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getCount() { 
     return count; 
    } 

    public void setCount(int count) { 
     this.count = count; 
    } 

    public List<Product> getProducts() { 
     return products; 
    } 

    public void setProducts(List<Product> products) { 
     this.products = products; 
    } 

    @Override 
    public String toString() { 
     return "Supplier{" + "name=" + name + '}'; 
    } 

第二段階は、(あなたのケースの休止状態に)あなたの二つのモデル間の関係をORMを伝えることです。オンラインでは、この巧妙な休止状態の「ステップ」を説明する豊富なドキュメントを見つけることができます。あなたの場合は、このような何かがすべきです。サプライヤーの

Hibernateマッピング:製品の

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="com.xxx.stackoverflowdb.model.Supplier" table="Supplier"> 
     <id column="id" name="id" type="int"> 
      <generator class="assigned"/> 
     </id> 
     <property column="name" name="name" type="string"/> 
     <property column="count" name="count" type="int"/> 
     <bag name="products" table="product" inverse="true" lazy="false" fetch="select"> 
      <key> 
       <column name="id"/> 
      </key> 
      <one-to-many class="com.xxx.stackoverflowdb.model.Product"/> 
     </bag> 
    </class> 
</hibernate-mapping> 

Hibernateのマッピングは:あなたが見ることができるように

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="com.xxx.stackoverflowdb.model.Product" table="PRODUCT"> 
     <id column="id" name="id" type="int"> 
      <generator class="assigned"/> 
     </id> 
     <property column="name" name="name" type="string"/> 
     <property column="description" name="description" type="string"/> 
     <property column="price" name="price" type="double"/> 
     <many-to-one name="supplierId" class="com.xxx.stackoverflowdb.model.Supplier" column="supplierId" insert="false" update="false" lazy="false"/> 
    </class> 
</hibernate-mapping> 

、両方のマッピングファイルには、関係を宣言します。このセットを使用すると、基準を作成してそれを実行することができます。今は冬眠しているので、関係を知って、それはあなたを助けることができます。

public class Tester { 
public static void main(String[] args) { 

    //gets a session, assuming your cg file is in a folder called hibernate_dispatcher 
    //under classpath 
    SessionFactory sessionFactory = new Configuration().configure("/hibernate_dispatcher/hibernate.cfg.xml") 
           .buildSessionFactory(); 
    Session session = sessionFactory.openSession(); 
    //gets a session, assuming your cg file is in a folder called hibernate_dispatcher 
    //under classpath 


    //YOUR own query --> gets all products order by count in supplier 
    Criteria criteria1 = session.createCriteria(Product.class); 
    criteria1.createAlias("supplierId", "supp"); 
    criteria1.addOrder(Order.desc("supp.count")); 

    for(Object p:criteria1.list()){ 
     Product nthP=(Product)p; 
     System.out.println(nthP); 
    } 
    //YOUR own query --> gets all products order by count in supplier 



    //the query you've asked --> gets all products order by price in Product 
    Criteria criteria2 = session.createCriteria(Supplier.class); 
    criteria2.createAlias("products", "prod"); 
    criteria2.addOrder(Order.desc("prod.price")); 

    for(Object s:criteria2.list()){ 
     Supplier nthS=(Supplier)s; 
     System.out.println(nthS); 
    } 
    //the query you've asked --> gets all products order by price in Product 
} 

}

:私はそれを証明する簡単なテスタークラスを作成しました
関連する問題