2013-07-20 70 views
11

我有一個枚舉作爲一個實體的屬性。當我嘗試在JPQL服務器中使用枚舉時,它給出了一個錯誤。除了將它作爲參數傳遞外,正確的方法是什麼?如何在JPQL where子句中使用JPA Enum?

枚舉是

package com.divudi.data; 

public enum Sex { 
    Male, 
    Female, 
    Unknown, 
    Other, 
} 

實體是

package com.divudi.entity.lab; 

import com.divudi.data.Sex; 
import com.divudi.entity.Item; 
import com.divudi.entity.WebUser; 
import java.io.Serializable; 
import java.util.Date; 
import javax.persistence.Entity; 
import javax.persistence.EnumType; 
import javax.persistence.Enumerated; 
import javax.persistence.Inheritance; 
import javax.persistence.InheritanceType; 
import javax.persistence.Lob; 
import javax.persistence.ManyToOne; 
import javax.persistence.Temporal; 

@Entity 
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) 
public class InvestigationItemValueFlag extends InvestigationItemValue implements Serializable { 

    private static final long serialVersionUID = 1L; 
    @Enumerated(EnumType.STRING) 
    Sex sex; 
    @ManyToOne 
    InvestigationItem investigationItemOfLabelType; 
    @ManyToOne 
    private InvestigationItem investigationItemOfValueType; 
    @ManyToOne 
    InvestigationItem investigationItemOfFlagType; 
    @ManyToOne 
    Item item; 
    long fromAge; 
    long toAge; 
    @Lob 
    private String flagMessage; 
    @Lob 
    String highMessage; 
    @Lob 
    String lowMessage; 
    @Lob 
    String normalMessage; 
    boolean displayFlagMessage; 
    boolean displayHighMessage; 
    boolean displayLowMessage; 
    boolean displayNormalMessage; 

    public InvestigationItem getInvestigationItemOfLabelType() { 
     return investigationItemOfLabelType; 
    } 

    public void setInvestigationItemOfLabelType(InvestigationItem investigationItemOfLabelType) { 
     this.investigationItemOfLabelType = investigationItemOfLabelType; 
    } 



    public String getHighMessage() { 
     return highMessage; 
    } 

    public void setHighMessage(String highMessage) { 
     this.highMessage = highMessage; 
    } 

    public String getLowMessage() { 
     return lowMessage; 
    } 

    public void setLowMessage(String lowMessage) { 
     this.lowMessage = lowMessage; 
    } 

    public String getNormalMessage() { 
     return normalMessage; 
    } 

    public void setNormalMessage(String normalMessage) { 
     this.normalMessage = normalMessage; 
    } 

    public boolean isDisplayFlagMessage() { 
     return displayFlagMessage; 
    } 

    public void setDisplayFlagMessage(boolean displayFlagMessage) { 
     this.displayFlagMessage = displayFlagMessage; 
    } 

    public boolean isDisplayHighMessage() { 
     return displayHighMessage; 
    } 

    public void setDisplayHighMessage(boolean displayHighMessage) { 
     this.displayHighMessage = displayHighMessage; 
    } 

    public boolean isDisplayLowMessage() { 
     return displayLowMessage; 
    } 

    public void setDisplayLowMessage(boolean displayLowMessage) { 
     this.displayLowMessage = displayLowMessage; 
    } 

    public boolean isDisplayNormalMessage() { 
     return displayNormalMessage; 
    } 

    public void setDisplayNormalMessage(boolean displayNormalMessage) { 
     this.displayNormalMessage = displayNormalMessage; 
    } 





    public Item getItem() { 
     return item; 
    } 

    public void setItem(Item item) { 
     this.item = item; 
    } 



    public InvestigationItemValueFlag() { 
    } 

    public Sex getSex() { 
     return sex; 
    } 

    public void setSex(Sex sex) { 
     this.sex = sex; 
    } 

    public long getFromAge() { 
     return fromAge; 
    } 

    public void setFromAge(long fromAge) { 
     this.fromAge = fromAge; 
    } 

    public long getToAge() { 
     return toAge; 
    } 

    public void setToAge(long toAge) { 
     this.toAge = toAge; 
    } 

    public String getFlagMessage() { 
     return flagMessage; 
    } 

    public void setFlagMessage(String flagMessage) { 
     this.flagMessage = flagMessage; 
    } 

    public InvestigationItem getInvestigationItemOfValueType() { 
     return investigationItemOfValueType; 
    } 

    public void setInvestigationItemOfValueType(InvestigationItem investigationItemOfValueType) { 
     this.investigationItemOfValueType = investigationItemOfValueType; 
    } 

    public InvestigationItem getInvestigationItemOfFlagType() { 
     return investigationItemOfFlagType; 
    } 

    public void setInvestigationItemOfFlagType(InvestigationItem investigationItemOfFlagType) { 
     this.investigationItemOfFlagType = investigationItemOfFlagType; 
    } 





} 

JSF託管豆如下(有關代碼只)

public String getPatientDynamicLabel(InvestigationItem ii, Patient p) { 
    String dl; 
    String sql; 
    dl = ii.getName(); 
    long ageInDays = commonFunctions.calculateAgeInDays(p.getPerson().getDob(), Calendar.getInstance().getTime()); 
    sql = "select f from InvestigationItemValueFlag f where f.fromAge < " + ageInDays + " and f.toAge > " + ageInDays + " and f.investigationItemOfLabelType.id = " + ii.getId(); 
    List<InvestigationItemValueFlag> fs = getIivfFacade().findBySQL(sql); 
    for (InvestigationItemValueFlag f : fs) { 
     if (f.getSex() == p.getPerson().getSex()) { 
      dl = f.getFlagMessage(); 
     } 
    } 
    return dl; 
} 

誤差是

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Error compiling the query [select f from InvestigationItemValueFlag f where f.sex = com.divudi.data.Male and f.fromAge < 12419 and f.toAge > 12419 and f.investigationItemOfLabelType.id = 2678], line 1, column 57: unknown identification variable [com]. The FROM clause of the query does not declare an identification variable [com]. 

回答

16

不應該使用字符串連接將參數傳遞給查詢。您應該使用參數(命名,preferrably):

String jpql = 
    "select f from InvestigationItemValueFlag f" 
    + " where f.sex = :sex" 
    + " and ..."; 
Query query = em.createQuery(jpql); 
query.setParameter("sex", Sex.Male); 

這將照顧正確轉義的,正確的SQL生成(枚舉可以被映射爲一個字符串或爲序),以及避免JPQL注入攻擊。

也請不要命名sqlfindBySQL它實際上是JPQL。