所以,我正在使用JPA(Glassfish Server 4.1.2)在JAVA EE中完成一个项目。 但是当我坚持使用我的Facade时出现了这个错误:
void Game::tick(){
(this->*(button1->click))();
}
这是我的档案:
Controller(IndexBean):
Caused by: java.lang.IllegalArgumentException: Object: be.isl.prj.story.entity.Universe@3b28b3d6 is not a known Entity type.
AbstractFacade:
package be.usl.prj.story.controller;
import be.isl.prj.story.dao.UniverseFacade;
import be.isl.prj.story.entity.Universe;
import java.io.Serializable;
import java.util.ArrayList;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.inject.Inject;
/**
* IndexController
*/
@ManagedBean
@RequestScoped
public class IndexBean implements Serializable{
ArrayList<String> list = new ArrayList<String>();
private String test;
@Inject
private UniverseFacade universeFacade;
@PostConstruct
private void init(){
}
public IndexBean() {
}
public void doTest(){
Universe universe = new Universe("YEAH", "REallyNew gen");
/*
// this works :
EntityManagerFactory emf = Persistence.createEntityManagerFactory("StoryGeneratorPU");
EntityManager entityManager = emf.createEntityManager();
entityManager.persist(universe);
*/
this.universeFacade.create(universe);
//System.out.println(this.universeFacade.find(139).getName());
}
}
UniverseFacade:
package be.isl.prj.story.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
public abstract class AbstractFacade<T> {
private Class<T> entityClass;
@PersistenceContext(unitName="StoryGeneratorPU")
protected EntityManager entityManager;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("StoryGeneratorPU");
this.entityManager = emf.createEntityManager();
}
public void create(T entity) {
entityManager.persist(entity);
}
public void edit(T entity) {
entityManager.merge(entity);
}
public void remove(T entity) {
entityManager.remove(entityManager.merge(entity));
}
public T find(Object id) {
T object = entityManager.find(entityClass, id);
return object;
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
List list = entityManager.createQuery(cq).getResultList();
return list;
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = entityManager.createQuery(cq);
q.setMaxResults(range[1] - range[0] + 1);
q.setFirstResult(range[0]);
List list = q.getResultList();
return list;
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq = entityManager.getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(entityManager.getCriteriaBuilder().count(rt));
javax.persistence.Query q = entityManager.createQuery(cq);
int count = ((Long) q.getSingleResult()).intValue();
return count;
}
}
宇宙实体:
package be.isl.prj.story.dao;
import be.isl.prj.story.entity.Universe;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
@Stateless
public class UniverseFacade extends AbstractFacade<Universe>{
public UniverseFacade() {
super(Universe.class);
}
public EntityManager getEntityManager(){
return this.entityManager;
}
}
Persistence.xml:
package be.isl.prj.story.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "universe")
public class Universe implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="universe_id_seq")
@SequenceGenerator(name="universe_id_seq", sequenceName="universe_id_seq", allocationSize=1)
@Column( name = "id_universe" )
private int idUniverse;
@Column( name = "name" )
protected String name;
@Column( name = "content" )
protected String content;
// ALTER SEQUENCE universe_id_universe_seq RENAME TO universe_id_seq;
public Universe() {
}
public Universe(int idUniverse, String name, String content) {
this.setIdUniverse(idUniverse);
this.setName(name);
this.setContent(content);
}
public Universe(String name, String content) {
this.setName(name);
this.setContent(content);
}
public int getIdUniverse() {
return idUniverse;
}
public void setIdUniverse(int idUniverse) {
this.idUniverse = idUniverse;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
最后是堆栈跟踪:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="StoryGeneratorPU" transaction-type="JTA">
<class>be.isl.prj.story.entity.Universe</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
感谢您的回答。