java.lang.IllegalArgumentException:Object:se.nackademin.BeachBoysSweden.entities.Book@79da4980不是已知的实体类型

时间:2013-05-08 19:54:12

标签: jsf jpa eclipselink glassfish-3

我正在完成学校作业,但在尝试将我的实体保存到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";
    }

}

的persistence.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> 

Root case:

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)

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

尝试使用

添加课程
<class>Book</class>

尝试删除,

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

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

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

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