繁体   English   中英

在JPA中使用WHERE查询实体

[英]Use WHERE in JPA to query the 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;

.....
}

我尝试使用此查询:

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

看起来它只是在选择表键。

如何选择表列terminalToken

您最好使用Spring数据JPA存储库一起构建查询。 您只需要扩展JpaRepository接口,并遵循命名约定来命名您的方法。

您的方法将如下所示:

public List<Terminal> findByTeminalToken(String TerminalToken);

否则,您将需要使用entityManager.createQuery()方法而不是entityManager.find()因为后者仅与id列一起使用。

如果您正在寻找纯Java(我更赞成Philipp解决方案),也许您想看看这个解决方案。 https://www.objectdb.com/java/jpa/query/criteria 很抱歉没有发布直接的解决方案,但我想为您提供更多信息。

顺便说一句,为什么不使用弹簧数据? 容易得多

暂无
暂无

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

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