0

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:

  1. Which steps I should take next?
  2. Where should I tie my Book entity to real DB table?
  3. Where should EntityManager appear?
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
  • Grab one of the Hibernate/JPA tutorials (not just an example) and follow those. It should become clear by then. To be honest, the answers to your question would probably be too long for SO, especially since that depends on your environment (e.g. are you using an application server), your application setup etc. – Thomas Jun 22 '16 at 13:51
  • maybe http://stackoverflow.com/questions/22772980/struggling-to-understand-entitymanager-proper-use can be interesting – Gab Jun 22 '16 at 14:03

1 Answers1

1
  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

    @Named
    public class myBean {
    
      @PersistenceContext
      private EntityManager em;
    
      public Book getBookById(Long id) {
       return em.find(Book.class, id);
      }
    }
    
Gab
  • 7,869
  • 4
  • 37
  • 68