Hibernate Exception - 无法找到命名参数

时间:2015-06-26 16:07:46

标签: java hibernate java-ee jpa hibernate-criteria

我正在尝试从实体(表)StudySeries中提取数据库中的对象列表:

@Entity 
@Table(name="StudySeries", uniqueConstraints = {
@UniqueConstraint(columnNames = "SeriesInstanceUID")})
public class StudySeries implements Serializable {
    ...
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "SeId", unique = true, nullable = false)
    private Long seId;

    @Column(name="SeriesInstanceUID", unique=true, nullable = false)
    private String seriesInstanceUID;
    ...
    @ManyToOne
    @JoinColumn(name = "StId", referencedColumnName="StId")
    private StudyDetails studyDetails;
    ...
}

此实体与StudyDetails一起加入N-1(在StudyDetails上有许多StudySeries):

@Entity
@Table(name="StudyDetails", uniqueConstraints = @UniqueConstraint(columnNames = "StudyInstanceUID"))
public class StudyDetails implements Serializable {

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name="StId", unique = true, nullable = false)
    private Long stId;

    @Column(name="StudyInstanceUID", unique=true, nullable = false)
    private String studyInstanceUID;
    ...
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "studyDetails", cascade = CascadeType.ALL)
    private Set<StudySeries> studySeries = new HashSet<StudySeries>(0);
    ...
}

在我的StudySeriesDAOImpl()中,我想:

@Override
public List<StudySeries> getStudySeriesObjectsByStudyId(Long stId) {

    List<StudySeries> results=new ArrayList<>();
    Session s=HibernateUtil.openSession();
    s.beginTransaction();
    String hql = "from StudySeries E where E.studyDetails.stId = stId";
    Query query = s.createQuery(hql);
    query.setParameter("stId", stId);
    results = query.list();
    s.getTransaction().commit();
    s.close();
    log.info(">>>>> list size: " + results.size());        

    return results;
}

我也尝试过hql查询:

String hql = "from StudySeries E where E.stId = stId";

但是我得到了:

org.hibernate.QueryParameterException: could not locate named parameter [stId]
at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:100) at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:100)
at org.hibernate.engine.query.spi.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:106)
at org.hibernate.internal.AbstractQueryImpl.determineType(AbstractQueryImpl.java:466)
at org.hibernate.internal.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:436)
at com.npap.dao.StudySeriesDAOImpl.getStudySeriesObjectsByStudyId(StudySeriesDAOImpl.java:239)

任何想法有什么不对?

1 个答案:

答案 0 :(得分:0)

在StudySeries类中,id被命名为&#39; seId&#39;而不是&#39; stId&#39;。 你应该这样做:String hql =&#34;来自StudySeries E,其中E.seId = stId&#34 ;;