JPA Dynamic, typesafe queries

摘要:JPA Dynamic, typesafe queries

為了一個原因,找到以下的網址,來解決我如果去使用不是用下SQL或HQL的方式來撈資料

http://www.ibm.com/developerworks/java/library/j-typesafejpa/

會使用這個會原因,是我的萬用取得資料的method出了錯,

原因是我的class使用了idClass,是為了應付複合主鍵。

如下:

package com.project.model;

import java.util.Date;
import java.io.Serializable;
import javax.persistence.*;
import java.lang.*;

@Entity
@Table(name="temp_data")
@PersistenceUnit( unitName = "temp")
@IdClass(TempDataId.class)
public class TempData implements Serializable {
    @Id
    @Column(name="source")
    private String source;
    @Id
    @Column(name="path")
    private String path;
 

    public String getSource()
    {
        return source;
    }
    public void setSource(String source)
    {
        this.source = source;
    }

    public String getPath()
    {
        return text_path;
    }
    public void setPath(String path
    {
        path= path
    }
}

上面是建立一個temp_data資料表對映的TempData Class

但他有兩個欄位組合的PK

所以會需要另外建IdClass如下

package com.project.model;

import java.io.Serializable;

public class TempDataId implements Serializable {
    private String source;
    private String path;

    public String getSource()
    {
        return source;
    }
    public void setSource(String source)
    {
        this.source = source;
    }
    public String getPath()
    {
        return path;
    }
    public void setPath(String path)
    {
        this.path = path;
    }

    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof TempDataId )) {
            return false;
        }
        TempDataId  castOther = (TempDataId )other;
        return
            (this.source.equals(castOther.source))
            && (this.path.equals(castOther.path));  

    }

    public int hashCode() {
        final int prime = 31;
        int hash = 17;
        hash = hash * prime + ((int) (this.source.hashCode()  ^ (this.source.hashCode() >>> 32)));
        hash = hash * prime + ((int) (this.path.hashCode()  ^ (this.path.hashCode()  >>> 32)));

        return hash;
    }
}

然後,因為使用萬用的method會出問題,

我獨立另外變成一個方法,來處理條件的設定的method

但參考了該文章的作法,

會另外建立條件專案的參數class

如下

package com.project.model;
import javax.persistence.metamodel.SingularAttribute;
import java.util.Date;

@javax.persistence.metamodel.StaticMetamodel(TempData.class)
public class TempData_ {
  public static volatile SingularAttribute source;
  public static volatile SingularAttribute path;
}

然後才是開始撰寫設定equal及and條件建立method

    public TempData getTempData(TempData tempData)
    {
        TempData result = null;
        EntityManager em =this.getEntifyManager();
        CriteriaBuilder qb = em.getCriteriaBuilder();
        CriteriaQuery c = qb.createQuery(TempData.class);
        Root p = c.from(TempData.class);
        Predicate condition = qb.equal(p.get(TempData_.source), tempData.getSource());
        Predicate condition_temp = qb.equal(p.get(TempData_.path),tempData.getPath());
        condition =  qb.and(condition, condition_temp);
        c.where(condition);
        TypedQuery q = em.createQuery(c);
        List result_list = q.getResultList();

        if(result_list.size()>0)
            result = result_list.get(0);
        
        return result;
    }

這樣就可以處理複合主鍵的問題了。

(應該也只是為了處理我的程式留下的bug才會)