我正在尝试创建一个通用类,用于在我的项目中自动创建jpacontainer但不起作用并返回一些错误。错误参考No EntityProvider已设置。
我正在尝试这个。
/** class init jpacontainer */
public class PersistJPAContainer<T> extends JPAContainer<T>{
private static final long serialVersionUID = 1L;
private static final String PERSISTENCE_UNITNAME = "ps_unitname";
public PersistJPAContainer(Class<T> entityClass) {
super(entityClass);
JPAContainer<?> container = JPAContainerFactory.make(entityClass, PERSISTENCE_UNITNAME);
}
}
/** my app UI */
public class AcademiaonlineUI extends UI {
private final JPAContainer<Empresa> datasource = new PersistJPAContainer<Empresa>(Empresa.class);
@Override
protected void init(VaadinRequest request) {
Table tabela = new Table("Empresa", datasource);
VerticalLayout vLayout = new VerticalLayout();
vLayout.addComponent(tabela);
setContent(vLayout);
}
}
/** errors */
Mar 17, 2014 11:14:30 AM com.vaadin.server.DefaultErrorHandler doDefault
SEVERE:
java.lang.IllegalStateException: No EntityProvider has been set
at com.vaadin.addon.jpacontainer.JPAContainer.doGetEntityProvider(JPAContainer.java:313)
at com.vaadin.addon.jpacontainer.JPAContainer.size(JPAContainer.java:912)
at com.vaadin.ui.AbstractSelect.size(AbstractSelect.java:762)
at com.vaadin.ui.Table.refreshRenderedCells(Table.java:1654)
at com.vaadin.ui.Table.attach(Table.java:4171)
at com.vaadin.server.AbstractClientConnector.attach(AbstractClientConnector.java:583)
at com.vaadin.ui.AbstractComponent.attach(AbstractComponent.java:572)
at com.vaadin.ui.AbstractComponent.setParent(AbstractComponent.java:479)
at com.vaadin.ui.AbstractSingleComponentContainer.setContent(AbstractSingleComponentContainer.java:137)
at com.vaadin.ui.UI.setContent(UI.java:1162)
at br.com.academiaonline.AcademiaonlineUI.init(AcademiaonlineUI.java:24)
at com.vaadin.ui.UI.doInit(UI.java:614)
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:223)
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:73)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1382)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:724)
//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="ps_unitname" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>br.com.academiaonline.beans.Empresa</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:~/AODB"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value="master"/>
<!-- Cria a base de dados automaticamente -->
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<!-- Exibe log de erros no console do eclipse -->
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
有什么想法吗?
答案 0 :(得分:0)
在构造函数中尝试这样的事情
公共类CustomJPA扩展了JPAContainer {
private static final long serialVersionUID = -1752491307148335890L;
private static final String PU_UNIT="ps_unitname";
public CustomJPA(Class<T> entityClass, String persitenceUnitName) {
super(entityClass);
EntityManager entityManager = JPAContainerFactory.createEntityManagerForPersistenceUnit(persitenceUnitName);
setEntityProvider(new CachingMutableLocalEntityProvider<T>(entityClass,entityManager));
}
public CustomJPA(Class<T> entityClass) {
super(entityClass);
EntityManager entityManager =JPAContainerFactory.createEntityManagerForPersistenceUnit(PU_UNIT);
setEntityProvider(new CachingMutableLocalEntityProvider<T>(entityClass,entityManager));
}
}
无论我在哪里开始我的容器,我都会做这样的事情
JPAContainer<Bean> myBean= new CustomJPA<Bean>(Bean.class);
希望这会有所帮助。我觉得你正在编写一些冗余代码。
注意:
答案 1 :(得分:0)
现在有效......经过一些测试终于有效。
我这样做了。
public class PersistJPAContainer<T> extends JPAContainer<T>{
private static final long serialVersionUID = 1L;
private static final String PERSISTENCE_UNITNAME = "ps_unitname";
public PersistJPAContainer(Class<T> entityClass) {
super(entityClass);
EntityManager em = JPAContainerFactory.createEntityManagerForPersistenceUnit(PERSISTENCE_UNITNAME);
setEntityProvider(new CachingMutableLocalEntityProvider<T>(entityClass, em));
}
}
public class AcademiaonlineUI extends UI {
private final PersistJPAContainer<Empresa> datasource = new PersistJPAContainer<Empresa>(Empresa.class);
并且有效!