我是JPA的新手,我正在搜索(沒有運氣)獲取與PK @id
不同的列的特定行。你能給我和例子嗎?這是我的實體類:JPA與主鍵以外的行查詢
@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鏈接將不勝感激。