为什么JPA选择查询执行会返回一个异常" canot execute query"信息?

时间:2013-11-19 11:43:19

标签: jpa guid jpql

数据库中的表(Oracle 11g)是这样的:

名称:LOG_ALIM_MAIL

列:ID_LOG RAW(由触发器中的SYS_GUID()自动生成),ALIMENTATION Number(9),DATE_LOG Date

PK:ID_LOG

FK:ALIMENTATION参考ALIMENTATION.ID_ALIMENTATION(Number(9))

LOG_ALIM_MAIL类:

@Entity
public class LogAlimMail implements Serializable {

private static final long serialVersionUID = 2243374060845658640L;

@Id
private Long idLog;

private Date dateLog;

private Alimentation alimentation;

public LogAlimMail() {

}

public Long getIdLog() {
    return idLog;
}

public void setIdLog(Long idLog) {
    this.idLog = idLog;
}

public Date getDateLog() {
    return dateLog;
}

public void setDateLog(Date dateLog) {
    this.dateLog = dateLog;
}

@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
public Alimentation getAlimentation() {
    return alimentation;
}

public void setAlimentation(Alimentation alimentation) {
    this.alimentation = alimentation;
}
}

营养类:     @实体     public class Alimentation实现Serializable {

private static final long serialVersionUID = 5790314265385194058L;

private Long idAlimentation;

private Integer etat;

public Alimentation() {

}

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "my_alimentation_seq_gen")
@SequenceGenerator(name = "my_alimentation_seq_gen", sequenceName = "SEQ_ID_ALIMENTATION")
public Long getIdAlimentation() {
    return idAlimentation;
}

public Integer getEtat() {
    return etat;
}

public void setEtat(Integer etat) {
    this.etat = etat;
}

public void setIdAlimentation(Long idAlimentation) {
    this.idAlimentation = idAlimentation;
}
}

我有两个问题:

  1. 我正在尝试执行以下选择查询:

    public List<LogAlimMail> getAllByIdAlim(Long idAlim) {
    String request = "select a from LogAlimMail a where a.alimentation.idAlimentation = " + idAlim;
    
    Query query = this.getEntityManager().createQuery(request);
    
    return query.getResultList();
    }
    

    我得到了例外:

    java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: idAlimentation of: administration.LogAlimMail [select a from administration.LogAlimMail a where a.alimentation.idAlimentation = 1]
    
  2. 我无法在idLog(Long)和ID_LOG(由SYS_GUID()生成的RAW)之间进行正确的JPA映射。

  3. 由于

1 个答案:

答案 0 :(得分:0)

idAlim是一个号码吗?如果没有,你应该使用idAlimentation ='“+ idAlim +”'“; 或者更好的是,使用绑定变量:

idAlimentation =:idAlim“; query.setString(“idAlim”,idAlim);