简体   繁体   中英

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 ?

You better use Spring data to build your queries along with JPA Repositories . You will just need to extend the JpaRepository interface, and follow the naming conventions to name your methods.

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.

If you are looking for pure java (i am more in favour of Philipp solution) perhaps you wish to check out this solution. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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