RudziankoŇ≠ RudziankoŇ≠ - 7 months ago 48
Java Question

How to make bean persistent?

I would like to bundle several technologies Servlet + EJB + JPA(Hibernate) + DB(PostgreSQL)

I have working

and I created
. I used example and I don't see where Hibernate tied to DB etc ...

@Table(name = "BookHibernate", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;

public Long getId() {
return id;

public void setId(Long id) { = id;

public String getName() {
return name;

public void setName(String name) { = name;


  1. Which steps I should take next?

  2. Where should I tie my
    entity to real DB table?

  3. Where should

Gab Gab
  1. 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

  2. You already did it @Table(name = "BookHibernate"...

  3. cf 1

    public class myBean {
      private EntityManager em;
      public Book getBookById(Long id) {
       return em.find(Book.class, id);