Reputation: 11251
I would like to bundle several technologies Servlet + EJB + JPA(Hibernate) + DB(PostgreSQL)
I have working Servlet
and I created Bean
. I used example and I don't see where Hibernate tied to DB etc ...
@Entity
@XmlRootElement
@Table(name = "BookHibernate", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Question:
Book
entity to real DB table?EntityManager
appear?Upvotes: 0
Views: 59
Reputation: 8323
Create an other bean (a CDI one or an EJB stateless one) and inject an entityManager (@persistenceContext) inside, use this one to fetch or persist your entity to the database
You already did it @Table(name = "BookHibernate"...
cf 1
@Named
public class myBean {
@PersistenceContext
private EntityManager em;
public Book getBookById(Long id) {
return em.find(Book.class, id);
}
}
Upvotes: 1