2017-03-22 7 views
0

私は参加する必要がある4つのテーブルがあります。休止状態で4つのテーブルを結合する方法

  1. 製品
  2. サービス
  3. SERVICE_TYPE
  4. service_duration

製品がサービスを有し、サービスタイプおよび持続時間を有している:これらの4つの表です。

ユーザは、製品、タイプ、サービス、およびサービスの期間を選択します。

サービスが選択されたタイプと期間で利用できない場合、より低い優先度のタイプ(優先度はservice_typeテーブルの属性)を持つサービスを取得したいと考えています。 PHPで

(Laravel)私はそうのように参加することができる午前:

DB::table('product') 
     ->join('product_service', 'product_service.product_id', '=', 'product.id') 
     ->join('service', 'service.id', '=', 'product_service.service_id') 
     ->join('service_type', 'service.type_id', '=', 'service_type.id') 
     ->join('service_duration', 'service.duration_id', '=', 'service_duration.id') 
     ->where('product.id', id) 
     ->where('service_type.priority', '<', priority) 
     ->where('service_duration.duration', duration) 
     ->where('maintenance.active', 1) 
     ->orderBy('service_type.priority', 'desc') 
     ->select('service.*') 
     ->first(); 

にHibernateエンティティの基準でこれを行うことが可能にどのように?私は製品側から参加することから始めたいが、最終的にはサービスを選択する。

私は私の関係はそれほどのように定義している:

Productクラス

@Entity 
@Table(name = "product") 
public class product implements Serializable { 
     @OneToMany(fetch = FetchType.LAZY, mappedBy = "id.product", cascade=CascadeType.ALL) 
     private Set<ProductService> services = new HashSet<ProductService>(); 
    } 

ProductServiceクラス:

@Entity 
@Table(name = "product_service") 
@AssociationOverrides({ @AssociationOverride(name = "id.service", joinColumns = @JoinColumn(name = "service_id")), 
     @AssociationOverride(name = "id.product", joinColumns = @JoinColumn(name = "product_id"))}) 

public class ProductService implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @EmbeddedId 
    private ProductServicePK id = new ProductServicePK(); 


    @Column(name = "price") 
    private Float price; 

    @Column(name = "code") 
    private String code; 
} 

ProductServicePKクラス:

@Embeddable 
public class ProductServicePK implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @ManyToOne 
    private Product product; 

    @ManyToOne 
    private Service service; 
} 

サービスCLAS s:

したがって、他のオブジェクトを「保持」するオブジェクトは、製品オブジェクトです。だから私は選択されたものよりも優先順位が低いその製品のサービスを受ける方法がわかりません。

私は基準またはHQLでこれを行いたいと思います。

+0

はい、これは可能です。Entityクラスはすでに '@ OneToOne' /' @ OneToMany'/'@ ManyToOne'の関係で設定されていますか? –

+0

Hibernateのドキュメンテーションは、リレーションを扱う方法を明確に説明しています。あなたはHibernateのドキュメントを見ましたか? [Hibernateのドキュメント](http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html) –

+0

@DeanClarkはい、そうです。 – user3552551

答えて

0

明らかにJPA Criteria APIは、複合PKパターンを使用するときに必要なものをサポートしていません。私はこれがどこかで言及されたことを発見した。here。だから私は最高のソリューションがHQLを使っていることが分かりました。

Query q = session.createQuery("select s " 
      + "from Service s " 
      + "join s.productService as ps " 
      + "join s.type as t " 
      + "where s.duration = :durationId " 
      + "and ps.id.product.id = :productId " 
      + "and t.priority <= :priorityValue " 
      + "order by t.priority desc"); 
    q.setLong("durationId", durationId); 
    q.setInteger("priorityValue", priorityValue); 
    q.setLong("productId", productId); 

    return (Service) q.setMaxResults(1).uniqueResult(); 
1

あなたの休止状態戸建基準が好きになるでしょう:

DetachedCriteria criteria = DetachedCriteria.forClass(Product.class,  "product") 
    .criteria.createAlias("product.product_service", "productService") 
    .createAlias("productService.service","service") 
    .createAlias("service.service_type","serviceType") 
    .createAlias("service_duration","serviceDuration") 
    .add(Restrictions.eq("product.id", id)) 
    .add(Restrictions.gt("serviceType.priority", priority)) 
    .add(Restrictions.eq("serviceDuration.duration", duration)) 
    .setProjection(add your productService projection here) 
    getExecutableCriteria(session).SetMaxResults(1) ; 

しかし、あなたはproduct代わりのproduct_servicefrom queryを使用しているなぜあなたはproduct_service詳細を必要とするので、私は、あなたのポイントを得ていないのですか?。

あなたがquerycriteria.htmlを使用することができ、ここではIT関連のドキュメントがある>https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querycriteria.html またはあなたがHQL使用することができます - あなたの結果を達成するために>https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.htmlを。

+0

同じ結果が得られるのは、他の方法で起こっても構わないと思います。 – user3552551

+0

@ user3552551上記のようにDetachedCriteriaを使用することができます。 –

+0

多くの関係が定義されているので、私は製品側から始めていると思います。私はあなたのソリューションのおかげで試してみます。 – user3552551