0

I'm trying create a generic class for automate the creation of jpacontainer in my project but doesn't work and returns some errors. The errors refer No EntityProvider has been set.

I'm trying this.

/** 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>

Any idea ?

FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118
  • I don't think you create a container rather you use the one your app is in. You should use injection to get at you JPA entities and the persistence.xml file should state which persistence unit you are using. – onesixtyfourth Mar 17 '14 at 14:17
  • yep! my persistence.xml file is configured with my beans: br.com.academiaonline.beans.Empresa. what I want is create a generic class that extends JPAContainer. – FernandoPaiva Mar 17 '14 at 16:57
  • can you please share your persistence.xml? For JPA container to work you need to have at least one Provider like hibernate/Eclipselink/OpenJPA in your class path and the same has to be configured in your persistence.xml – Patton Mar 18 '14 at 04:32
  • @Patton, I'm using Eclipselink and does have in my classpath... – FernandoPaiva Mar 18 '14 at 10:28
  • Hey Bossm you have `academiaonline` as your Persistence Unit name and `ps_unitname` as the one in your java class. So there is name mismatch – Patton Mar 18 '14 at 13:19
  • @Patton, really, my project is don't equals this post, but what I want is create a generic class that extend JPAContainer passing the type of bean as parameter, can you understand ? example: public class PersistJPAContainer extends JPAContainer{} – FernandoPaiva Mar 18 '14 at 14:11
  • How to I get the type of bean did passed by parameter ? – FernandoPaiva Mar 18 '14 at 14:13

2 Answers2

0

Try some thing like this in your Constructor

public class CustomJPA extends 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));
   }

}

Where ever I have to initalise my Container, I do it some thing like this

 JPAContainer<Bean> myBean= new CustomJPA<Bean>(Bean.class);

Hope this helps. I feel that you are writing some redundant code.

NOTE:

  • I have not tested this snippet of code.
  • Try to use appropriate class as your EnityProvider, I used the default one in this example

BTW, is this what you are expecting??

Patton
  • 2,002
  • 3
  • 25
  • 36
  • the parameter of my constructor is (Class entityClass), using super(JPAContainer(entityClass)) like your suggest, there some errors. – FernandoPaiva Mar 18 '14 at 16:58
0

now works...after some tests finally works.

I did so.

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);

And works !

FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118