使用除主键之外的行的JPA查询

时间:2016-02-28 18:40:17

标签: java mysql jpa

我是JPA的新手,我正在搜索(没有运气)获取与PK @id不同的列的特定行的方法。你能举个例子吗?这是我的实体类:

 @Entity
@Table(name="bookshelf")
public class Book {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)@Column(name="id",nullable=false,unique=true)
    private long id;
    @Column(name="title",unique=false)
    private String title;
    @Column(name="author")
    private String author;
    @Column(name="year")
    private int year;
    @Column(name="comment")
    private String comment;
    @Column(name="image")
    private String image;



    public Book() {
        super();
    }

    public Book(String title, String author, int year, String comment, String image) {
        super();
        this.title = title;
        this.author = author;
        this.year = year;
        this.comment = comment;
        this.image = image;
    }

    public Book(long id, String title, String author, int year, String comment, String image) {
        super();
        this.id = id;
        this.title = title;
        this.author = author;
        this.year = year;
        this.comment = comment;
        this.image = image;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public String getComment() {
        return comment;
    }
    public void setComment(String comment) {
        this.comment = comment;
    }
    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }


}

这是我用于方法的Controller类:

public class Controller {

private EntityManagerFactory emf;
private EntityManager em;

public Controller() {
    emf = Persistence.createEntityManagerFactory("BookShelf");
    em = emf.createEntityManager();
}

public boolean addBook(Book book) {
    em.getTransaction().begin();
    em.persist(book);
    em.flush();
    em.getTransaction().commit();
    em.close();
    emf.close();
    return true;

}

public Book getBook(long id) {
    return em.find(Book.class, id);
}

public List<Book> allBooks() {
    List<Book> books = null;

    return books;
}

public Book updateBook(Book book) {
    em.getTransaction().begin();
    Book bookTemp = (Book)em.find(Book.class, book.getId());
    bookTemp.setTitle(book.getTitle());
    bookTemp.setAuthor(book.getAuthor());
    bookTemp.setYear(book.getYear());
    bookTemp.setComment(book.getComment());
    bookTemp.setImage(book.getImage());
    em.merge(book);
    em.getTransaction().commit();
    em.close();
    emf.close();
    return bookTemp;

}


public Book getLastRow() {
    Book book = new Book();
    em.getTransaction().begin();
    Query query = em.createQuery("select t from Book t order by t.id desc");
    @SuppressWarnings("unchecked")
    List<Book> list = query.setMaxResults(1).getResultList();
    try {
        book = list.get(0);
        em.close();
        emf.close();
        return book;
    } catch (ArrayIndexOutOfBoundsException e) {
        em.close();
        emf.close();
        return book;

    }

}

public void deleteBook(long id) {
    em.getTransaction().begin();
    Book book = em.find(Book.class, id);
    em.remove(book);
    em.getTransaction().commit();
    em.close();
    emf.close();

}

public Book getBookByTitle(String title) {

    return null;
}

} 有一些未完成的方法 - 不要介意它们。还没有。 最后一个是我需要的。我知道如何用普通的Query来做这件事,但我不知道JPA在这件事上是如何运作的。任何代码示例或UNDERSTANDABLE链接将不胜感激。

2 个答案:

答案 0 :(得分:0)

您可以使用JPA query

执行此操作
select b from Book b where b.title = :title

基于字符串的JPQL的替代方法是Criteria API

答案 1 :(得分:0)

您可以在JPQL Wiki books上详细阅读和理解JPA查询语言。 您还可以享受JPA的其他主题。