【问题标题】:HQL where clause with String collection type具有字符串集合类型的 HQL where 子句
【发布时间】:2016-07-01 03:31:57
【问题描述】:

我想根据用户提供的一些变量进行搜索。

我正在做什么以从表格集合中获取数据:

这是我的实体:

@Entity
@Table
public class FrontRequest implements Serializable { 
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long tutorfront_id;

    @Column
    private String tutorialType;

    @Column
    private String tutorialLevel;

    @ElementCollection(fetch=FetchType.EAGER)
    @Fetch(value = FetchMode.SELECT)
    private Collection<String> tutorialSubjects= new HashSet<String>();

    @Column
    private String tutorialCity;

    @Column
    private String noOfStudent;

    @Column
    private String classWeek;

    @Column
    private String tutionFee;

    @Column
    private String tutionFeerate;

    @Column
    private String tutorSex;

    @Column
    private String tutorEducation;


    @Column
    private Date postingDate;

    @Column
    private String studentSchool;
}

现在我想获取 FrontRequest 对象作为列表,这里我有一个主题作为输入。

现在我想在 FrontRequest.TutorialSubjects 中获取 FrontRequest here 主题。

为此,我尝试如下操作:

Query query=session.createQuery("from FrontRequest frontreq where :tutorialsubject IN frontreq.tutorialSubjects");

query.setParameter("tutorialsubject", tutorialSubject);

 List<FrontRequest> frontRequest=query.list();

但是它给出了无效语法的异常。

我不知道我错在哪里。可能是我做错了。

是的,问题是一样的

Issue when trying to use the IN operator in a JPQL query

但是,它与此略有不同,即我在我的实体中使用字符串类型集合。当我尝试使用这里的成员时,它无法使用它并引发异常

org.hibernate.hql.internal.ast.QuerySyntaxException: 子树意外结束 [来自 com.tutor.entity.FrontRequest frontreq where frontreq.tutorialCity like '%' and :tutorialsubject MEMBER OF frontreq.tutorialSubjects]

请帮忙。

提前致谢。

【问题讨论】:

标签: java hibernate hql


【解决方案1】:

我终于解决了 where 子句中“In elements”的问题。

查询是:

 from FrontRequest frontreq where :tutorialsubject IN elements (frontreq.tutorialSubjects)

query.setParameter("tutorialsubject", tutorialSubject);

 List<FrontRequest> frontRequest=query.list();

感谢所有人,应该给它+1。

【讨论】:

    猜你喜欢
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 2015-09-13
    • 1970-01-01
    相关资源
    最近更新 更多