我正在做学校作业,但在尝试将我的实体保存到 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)