尝试学习JPA,EJB,ManagedBeans,JSF等,但是我在尝试持久化实体时遇到错误。这是通过我的facelet调用BlogManager保存方法。这是JSF,ManagedBean,EJB和Entity。
@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();
}
}
ManagedBean
@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();
}
}
的facelet
<?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错误。
答案 0 :(得分:1)
您的应用无法验证bean。所以看看吧。你在id字段上有@NotNull注释(为什么?主键不应该是null),但是从你提供的来源我无法弄清楚如何生成id。没有定义策略。添加@GeneratedValue