将书值添加到@Entity值时,请输出null JavaEE

时间:2013-05-08 14:05:37

标签: java-ee maven-2 cdi inject

我可能做了一些非常愚蠢的事情,我已经尝试了大约4个小时,现在我无法弄清楚出了什么问题。当我尝试将一本书添加到数据库时,MySQL会增加,但Mysqltable列中设置的所有值都是“null”。

我正在使用maven 2.1和Mysql运行eclipse和glassfish 3.1.1。

我可能混淆了一些东西,或者我的注释是错误的或者是愚蠢的东西。请帮忙!

图书实体

package se.andolf.entities;

import java.io.Serializable;

import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@SessionScoped
@Named
@Table(name = "BOOKS")
public class Books implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id //private key  
    @Column
    (name = "BOOK_ID")
    private Long bookId;

    @Column
    (name = "TITLE")
    private String title;

    @Column
    (name = "AUTHOR")
    private String author;

    @Column
    (name = "PRICE")
    private String price;

    public Long getBookId() {
        return bookId;
    }

    public void setBookId(Long bookId) {
        this.bookId = bookId;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        System.out.println(author);
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }
}

BookController的

package se.andolf.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

import se.andolf.entities.Books;

@Named
@SessionScoped

public class BookController {

@PersistenceContext()
private EntityManager entityManager;

@Resource
UserTransaction userTransaction;

@Inject
private Books book; 

public String saveBook() {

    try {
        System.out.println(book.getAuthor() + " hej");
        userTransaction.begin();
        book.setAuthor(book.getAuthor());
        book.setTitle(book.getTitle());
        book.setPrice(book.getPrice());
        entityManager.persist(book);
        userTransaction.commit();
    } catch (NotSupportedException | SystemException | SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
        e.printStackTrace();
    }

    return "index";
}

public List<Books> getAllBooks(){
    Query query = entityManager.createQuery("SELECT s FROM Books s");
    List<Books>allBooks = query.getResultList();
    return allBooks;
    }
}

的index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets">
     <h:head>
       <title>Boklista</title>
     </h:head>
     <h:body>
     <h:messages></h:messages>
     <ui:repeat var="i" value="#{bookController.getAllBooks()}">
        <h:panelGrid columns="4">
            <h:outputLabel value="#{i.bookId}" />
            <h:outputLabel value="#{i.title}"/>
            <h:outputLabel value="#{i.author}"/>
            <h:outputLabel value="#{i.price}"/>
        </h:panelGrid>
     </ui:repeat>
       <h:form>
         <h:panelGrid columns="2">
            <h:outputLabel for="title" value="Title:"/>
            <h:inputText id="title" value="#{books.title}" required="true"/>
            <h:outputLabel for="author" value="Author:"/>
            <h:inputText id="author" value="#{books.author}" required="true"/>
            <h:outputLabel for="price" value="Price: "/>
            <h:inputText id="price" value="#{books.price}" required="true"/>
            <h:commandButton value="Submit" action="#{bookController.saveBook()}"/>
         </h:panelGrid>
       </h:form>
     </h:body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FaceletTest</display-name>
  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
<context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>

1 个答案:

答案 0 :(得分:0)

您无法让CDI管理您的JPA实体,但仍希望它们能够正确保留。如果你打算以这种方式做某事,你应该使用生产者。