public class Book {
@Id
private Integer isbn;
@Column
private String name;
// getter and setter
}
When I'm making a query on this table, there 2 ways on doing this:1st way is to do it in SQL style (take note on line 4):
EntityManagerFactory emf = Persistence.createEntityManagerFactory("book");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Query q = em.createQuery("select b from Book b");
List list = q.getResultList();
em.getTransaction().commit();
em.close();
2nd way is to do it through object-oriented way (take note on line 5): EntityManagerFactory emf = Persistence.createEntityManagerFactory("book");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Book book = em.find(Book.class, 1);
tx.commit();
em.close();
Cool huh!?! Happy Programming @!

No comments:
Post a Comment