简体   繁体   English

在JPA中使用WHERE查询实体

[英]Use WHERE in JPA to query the entity

I have this entity: 我有这个实体:

@Entity
@Table
public class Terminals implements Serializable {

    private static final long serialVersionUID = 5288308199642977991L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Column
    private int merchant_id;

    @Column
    private String terminalToken;

.....
}

I tried to use this query: 我尝试使用此查询:

public Terminals getTerminalToken(String terminalToken) throws Exception {
        return entityManager.find(Terminals.class, terminalToken);
    }

Looks like it's selecting only the table key. 看起来它只是在选择表键。

How I can select the table column terminalToken ? 如何选择表列terminalToken

You better use Spring data to build your queries along with JPA Repositories . 您最好使用Spring数据JPA存储库一起构建查询。 You will just need to extend the JpaRepository interface, and follow the naming conventions to name your methods. 您只需要扩展JpaRepository接口,并遵循命名约定来命名您的方法。

Your method will look like this: 您的方法将如下所示:

public List<Terminal> findByTeminalToken(String TerminalToken);

Otherwise you will need to use entityManager.createQuery() method instead of entityManager.find() because the latter one is only used with the id column. 否则,您将需要使用entityManager.createQuery()方法而不是entityManager.find()因为后者仅与id列一起使用。

If you are looking for pure java (i am more in favour of Philipp solution) perhaps you wish to check out this solution. 如果您正在寻找纯Java(我更赞成Philipp解决方案),也许您想看看这个解决方案。 https://www.objectdb.com/java/jpa/query/criteria . https://www.objectdb.com/java/jpa/query/criteria Sorry for not posting a direct solution but i think it woths more to give you the source. 很抱歉没有发布直接的解决方案,但我想为您提供更多信息。

By the way, why not using spring data? 顺便说一句,为什么不使用弹簧数据? Much easier 容易得多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM