尝试学习 JPA、EJB、ManagedBeans、JSF 等,但在尝试持久化实体时遇到错误。那是通过我的facelet 调用BlogManager 保存方法。这里是 JSF、ManagedBean、EJB 和实体。
@Entity
@Table(name = "BLOG")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Blog.findAll", query = "SELECT b FROM Blog b"),
@NamedQuery(name = "Blog.findByBlogid", query = "SELECT b FROM Blog b WHERE b.blogid = :blogid"),
@NamedQuery(name = "Blog.findByText", query = "SELECT b FROM Blog b WHERE b.text = :text"),
@NamedQuery(name = "Blog.findByTimestamp", query = "SELECT b FROM Blog b WHERE b.timestamp = :timestamp")})
public class Blog implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "BLOGID")
private Integer blogid;
@Size(max = 5000)
@Column(name = "TEXT")
private String text;
@Column(name = "TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
private Date timestamp;
public Blog() {
}
public Blog(Integer blogid) {
this.blogid = blogid;
}
Getters and Setters...
EJB:
@Stateless
public class BlogFacade extends AbstractFacade<Blog> {
@PersistenceContext(unitName = "MyBlogPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public BlogFacade() {
super(Blog.class);
}
public List<Blog> getBlogs(){
em.createNativeQuery("USE Blog").executeUpdate();
Query query = em.createNamedQuery("Blog.findAll");
return query.getResultList();
}
public void deleteBlog(int id){
Query query = em.createNativeQuery("DELETE FROM blog WHERE blogid = :blogid");
query.setParameter(":blogid", id);
query.executeUpdate();
}
}
托管Bean
@Named("blogManager")
@ManagedBean
@SessionScoped
public class BlogManager implements Serializable {
private Blog BlogEntity;
@EJB
private BlogFacade BlogFacade;
@PostConstruct
public void init(){
BlogEntity = new Blog();
}
public Blog getBlogEntity(){
return this.BlogEntity;
}
public void setBlogEntity(Blog BlogEntity){
this.BlogEntity = BlogEntity;
}
public void save(){
BlogFacade.create(BlogEntity);
}
public void delete(){
BlogFacade.remove(BlogEntity);
}
public void deleteBlog(@QueryParam("blogid") int id){
BlogFacade.deleteBlog(id);
}
public List<Blog> getBlogs(){
return BlogFacade.getBlogs();
}
}
小面
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns="http://www.w3.org/1999/xhtml"
template="./template.xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:define name="left">
<h:form>
<h:inputTextarea value="#{blogManager.blogEntity.text}" p:placeholder="Tweet max 30 chars." style="width: 100%; height:150px; resize: none;" /><br/><br/>
<h:commandButton value="Tweet" action="#{blogManager.save}" />
</h:form>
</ui:define>
<ui:define name="right">
<c:forEach items="#{blogManager.getBlogs()}" var="item">
<h:outputText value="#{item.getText()}" />
Likes: <h:outputText value="" />
<h:commandLink action="" value="like">
<f:param name="tweetid" value="" />
<f:param name="username" value="" />
</h:commandLink> <br />
<textarea>
<h:outputText value="" />
</textarea>
</c:forEach>
</ui:define>
</ui:composition>
不确定这是否允许,但我已将 strack 跟踪上传到 http://pastebin.com/7Mp0J7yG而不是把它弄乱在这篇文章中。是的,我遇到了 EJB 错误。
最佳答案
您的应用程序在 Bean 验证上失败。所以看看吧。您在 id 字段上有 @NotNull 注释(为什么?主键无论如何都不应该为空),但从您提供的来源来看,我无法弄清楚 id 是如何生成的。没有定义策略。添加@GeneratedValue
关于java - 无法持久化实体。 EJB异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23731262/