简体   繁体   中英

Can't persist entity. EJBException

Trying to learn JPA, EJB, ManagedBeans, JSF etc but I'm getting an error trying to persist an entity. That is invoking the BlogManager save method through my facelet. Here is the JSF, ManagedBean, EJB and 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()}" />&nbsp;&nbsp;
        Likes: &nbsp;&nbsp;<h:outputText value="" />&nbsp;&nbsp;&nbsp;
        <h:commandLink action="" value="like">
            <f:param name="tweetid" value="" />
            <f:param name="username" value="" />
        </h:commandLink>&nbsp;<br />
        <textarea>
            <h:outputText value="" />
        </textarea>
    </c:forEach>
</ui:define>

</ui:composition>

Not sure if this is allowed but I've uploaded the strack trace to http://pastebin.com/7Mp0J7yG instead of cluttering it all over this post. So yeah I'm gettting an EJB error.

Your app fails on bean validation. So look at it. You have @NotNull annotation on id field (why? primary key should not be null anyway), but from the sources you provided I can't figure out how id is generated. No strategy is defined. Add @GeneratedValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM