0

I am newbee with EJB3.

I have create a dao User.

@Entity
@Table(name = "t_user")
public class User implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;
    @Column(name = "name")
    private String name;

    private String street;
    private int houseNumber;

    @Column(name = "email") 
    private String email;
//... getters and setters

}

A stateless EJB:

@Stateless
public class ItemFacade implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @PersistenceContext(unitName = "databaseName", type = PersistenceContextType.EXTENDED)
    private EntityManager em;
    @PersistenceUnit
    private EntityManagerFactory emf;

    public void create(User user) {
        emf = Persistence.createEntityManagerFactory("databaseName");
        em = emf.createEntityManager();
        em.persist(user);
    }
}

The managed bean:

@ManagedBean
@RequestScoped
public class UserBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private User user = new User();
    @EJB
    private ItemFacade itemFacade;
    public void save(){   
        itemFacade = new ItemFacade();
        itemFacade.create(this.user);
    }
    //....
}

The persistence.xml:

<?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="databaseName" >
          <jta-data-source>TestDS</jta-data-source>
        </persistence-unit>
</persistence>

And the tomee.xml:

<?xml version="1.0" encoding="UTF-8"?>
<tomee>
  <!-- see http://tomee.apache.org/containers-and-resources.html -->

  <!-- activate next line to be able to deploy applications in apps -->
  <!-- <Deployments dir="apps" /> -->
    <Resource id="TestDS" type="DataSource">
      JdbcDriver com.mysql.jdbc.Driver
      JdbcUrl jdbc:mysql://localhost:3306/databaseName
      UserName username
      Password password
      JtaManaged true
    </Resource>
</tomee>

But I get the following exception when I try to persist a User:

Starting OpenJPA 2.4.0-nonfinal-1598334
may 23, 2015 2:33:39 PM com.sun.faces.lifecycle.ApplyRequestValuesPhase execute
ADVERTENCIA: <openjpa-2.4.0-nonfinal-1598334-r422266:1599166 fatal user error> org.apache.openjpa.persistence.ArgumentException: The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database Connectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl@fdaf18ee". 
javax.el.ELException: <openjpa-2.4.0-nonfinal-1598334-r422266:1599166 fatal user error> org.apache.openjpa.persistence.ArgumentException: The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database Connectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl@fdaf18ee". 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:291)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:931)
    at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:105)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:44)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: <openjpa-2.4.0-nonfinal-1598334-r422266:1599166 fatal user error> org.apache.openjpa.persistence.ArgumentException: The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database Connectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl@fdaf18ee". 
    at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:72)
    at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:849)
    at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDBDictionaryInstance(JDBCConfigurationImpl.java:602)
    at org.apache.openjpa.jdbc.meta.MappingRepository.endConfiguration(MappingRepository.java:1518)
    at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:535)
    at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:460)
    at org.apache.openjpa.lib.conf.PluginValue.instantiate(PluginValue.java:121)
    at org.apache.openjpa.conf.MetaDataRepositoryValue.instantiate(MetaDataRepositoryValue.java:68)
    at org.apache.openjpa.lib.conf.ObjectValue.instantiate(ObjectValue.java:83)
    at org.apache.openjpa.conf.OpenJPAConfigurationImpl.newMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:967)
    at org.apache.openjpa.conf.OpenJPAConfigurationImpl.getMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:958)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:642)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:202)
    at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:155)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:226)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:153)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:59)
    at manager.ItemFacade.create(ItemFacade.java:35)
    at views.UserBean.save(UserWizard.java:48)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    ... 34 more

How can I resolve it?

cool
  • 19
  • 1
  • 5
  • so "TestDS" is not the correct JNDI location for your datasource. More likely something like "java/TestDS" – Neil Stockton May 23 '15 at 13:04
  • I modify the persistence.xml: java/TestDS But it continue without working, how to know the correct JNDI location for my datasource? – cool May 23 '15 at 13:22
  • Can you try with : jdbc/TestDS – Arpit Aggarwal May 23 '15 at 13:32
  • Yes, the same problem in this line: em = emf.createEntityManager(); Thanks. – cool May 23 '15 at 13:44
  • the other one that this javaee stuff sometime use is something like "java:comp/env/TestDS" – Neil Stockton May 23 '15 at 15:28
  • I try also with java:comp/env/TestDS and it does not work, I do not understand well, it is not suppose to be the resouce id of the tomee.xml, where is suppose to be the name of the database? – cool May 24 '15 at 10:45

1 Answers1

0

Your EJB is wrong, extended is for stateful only and since you get injected the entity manager dont call Persistence and dont use the entitymanagerfactory:

@Stateless
public class ItemFacade {
  @PersistenceContext(unitName = "databaseName")
  private EntityManager em;

  public void create(User user) {
      em.persist(user);
  }
}
Romain Manni-Bucau
  • 3,354
  • 1
  • 16
  • 13