我想捆绑几种技术Servlet + EJB + JPA(Hibernate)+ DB(PostgreSQL)
我工作Servlet
,我创建了Bean
。我used example并且我没有看到Hibernate与DB等绑定在哪里......
@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;
}
}
问题:
Book
实体绑定到真正的数据库表?EntityManager
应该出现在哪里?答案 0 :(得分:1)
创建另一个bean(一个CDI或一个EJB无状态)并在其中注入一个entityManager(@persistenceContext),使用这个bean来获取或持久保存您的实体
您已经完成了@Table(name = "BookHibernate"...
cf 1
@Named
public class myBean {
@PersistenceContext
private EntityManager em;
public Book getBookById(Long id) {
return em.find(Book.class, id);
}
}