0

我正在做学校作业,但在尝试将我的实体保存到 mysql 数据库时不断收到 java.lang.IllegalArgumentException。该项目使用 JPA、EJB、JSF、Glassfish 和连接池。请记住,我是 Java Web 开发的新手。

实体:

import javax.inject.Named;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

import javax.persistence.Table;


@Named
@ConversationScoped
@Entity
@Table(name = "BOOKS")
public class Book implements Serializable{

        private static final long serialVersionUID = 1L;

        @Id
        @Column(name = "BOOK_ID")
        @GeneratedValue(strategy = GenerationType.AUTO)  
        private long bookId;

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

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

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

        public Book(){

        }

        public Long getBookId() {
                return bookId;
        }

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

        public String getTitle() {
                return title;
        }

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

        public String getAuthor() {
                return author;
        }

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

        public int getPrice() {
                return price;
        }

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

}//end class

DAO 对象:

import java.util.List;


import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;



@Stateless
@LocalBean
public class BookDaoBean implements BookDao {


    @PersistenceContext
    private EntityManager entityManager; 


    @SuppressWarnings("unchecked")
    public List<Book> getBooks() {

        List<Book> matchingBooksList;

        Query query = entityManager.createQuery("SELECT s FROM Book s");

        matchingBooksList = query.getResultList(); 

        return matchingBooksList;
    }


    @Override
    public void saveBook(Book book) {

        if(book.getAuthor() != null){


            entityManager.persist(book); 


        }else{

            System.out.println("Book object id is " + book.getBookId());

        }

    }

}//end class

控制器:

import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import se.nackademin.BeachBoysSweden.DAO.BookDaoBean;
import se.nackademin.BeachBoysSweden.entities.Book;



@Named
@RequestScoped 
public class BookController {

    @EJB
    private BookDaoBean bookDao;
    @Inject
    private Book book;
    @Inject
    private Conversation conversation; 

    public String enterForm(){
        conversation.begin();
        System.out.print("enter form");
        return "form";
    }

    public String goToConfirmation(){
        return "confirmation";

    }

    public String saveBook(){   
        bookDao.saveBook(book);
        conversation.end();
        return "list";
    }

}

持久性.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
       http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="book4PersistenceUnit"
        transaction-type="JTA">
        <jta-data-source>jdbc/__BookPool</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
    </persistence-unit>
</persistence> 

根案例:

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalArgumentException: Object: se.nackademin.BeachBoysSweden.entities.Book@73b9c795 is not a known entity type.
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4169)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:440)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:269)
4

2 回答 2

0

由于您使用的是带注释的类。您必须告诉 hibernate 可以扫描哪些文件以查找注释。

<persistence-unit name="book4PersistenceUnit"
        transaction-type="JTA">
    <!-- Explicitly define entities in class tags -->
    <class>Book</class>

    <properties>
          <!-- Scan for annotated classes and Hibernate mapping XML files -->
          <property name="hibernate.archive.autodetection" value="class, hbm"/>
    </properties>
</persistence-unit>
于 2013-05-08T20:12:21.720 回答
0

尝试使用添加类

<class>Book</class>

尝试删除,

<exclude-unlisted-classes>false</exclude-unlisted-classes>

确保您的类部署在与会话 bean 相同的 jar/ear 中。

确保您使用的是正确的持久性单元。

如果您正在重新部署,请尝试重新启动 Glassfish 服务器,您可能在取消部署时遇到了一些问题。

于 2013-05-09T12:56:20.057 回答