0

I'm beginning Java EE with NetBeans 8.1, Glassfish 4.1 and Apache Derby (included in GlassFish).

For this purpose I'm calling to put and store a car data with its attributes.

But this simple facade always returns "java.lang.IllegalStateException" whereas I'm seeing no mistakes in my Class files.

Here is the very simple Entity of model.car with basic getters and setters :

@Entity
@Table(name = "car")
public class Car implements java.io.Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Integer id;
    private String brand;
    private String model;
    private String year;
    private String energy;
    private String hp;
    private String tp;
    private byte[] picture;

Here is the "Facade" for this Entity to store data in Apache Derby :

@Stateless
public class CarFacade extends AbstractFacade<Car> {

    @PersistenceContext(unitName = "CarLocPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public CarFacade() {
        super(Car.class);
    }

}

I'm having a lot of trouble debugging the following exception since it is very simple short part of code and everything should works fine. Moreover, the StackTrace doesn't show the breaking point like in Java SE.

Here is the exception StackTrace :

Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName CarLocPU
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:138)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTxRequiredCheck(EntityManagerWrapper.java:158)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.doTransactionScopedTxCheck(EntityManagerWrapper.java:151)
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.persist(EntityManagerWrapper.java:281)
    at facade.AbstractFacade.create(AbstractFacade.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:64)
    at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
    at sun.reflect.GeneratedMethodAccessor965.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
    at sun.reflect.GeneratedMethodAccessor994.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
    ... 53 more

Thank you in advance for you help.

EDIT :

Here is the structure of the project.

enter image description here

And here is the persistence.xml content, the application is named "CarLoc".

<?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="CarLocPU" transaction-type="JTA">
    <jta-data-source>java:app/jdbc/CarLoc</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>
Paul
  • 1,410
  • 1
  • 14
  • 30
  • There is a problem with the `EntityManager`, please update the question with your `persistence.xml`. – unwichtich Feb 05 '16 at 14:24
  • In the future you get an exception, focus on the bottommost root cause. All causes thereafter are just consequences. I reduced irrelevant noise from the question as you seem to be incorrectly focusing on JSF as the cause. Nonetheless, in the future try literally interpreting and/or copypasting the root cause into a decent [search engine](https://www.google.com/search?q=java.lang.IllegalStateException%3A+Unable+to+retrieve+EntityManagerFactory+for+unitName). Usually the answer is right there. – BalusC Feb 05 '16 at 14:44
  • Could you also provide the structure of your project? – António Ribeiro Feb 05 '16 at 15:03
  • I added this elements to the question. Thanks for your feedback I notice the interesting lines of StackTrace are in the bottom nor like in JSE. – Paul Feb 05 '16 at 15:39
  • Your configuration files should be inside a META-INF folder, in order to be accessible in your runtime classpath, as stated by @BalusC [here](http://stackoverflow.com/a/25093377/1346996). – António Ribeiro Feb 05 '16 at 15:45
  • The file persistence.xml is automatically put here by NetBeans and I can't move it. On the filesystem it appears to be in "src/conf" folder. I created a META-INF folder inside it but still the problem remains. I think the problem might be the persistence.xml file itself which is miswritten. I'll try this way, thank you for your comment. – Paul Feb 05 '16 at 16:12
  • Also looking at your `persistence.xml` I noticed that you don't have any provider declared. What are you using to access your Derby database? – António Ribeiro Feb 05 '16 at 16:48

1 Answers1

1

If you are using JPA outside of the EJB container, you need to declare a JPA <provider> in your persistence.xml.

By default Glassfish uses EclipseLink as the JPA provider. Assuming you don't want to change this, you will want to change your persistence.xml to declare EclipseLink as the JPA provider:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="CarLocPU" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <property name="eclipselink.target-database" value="Derby"/>
            <!-- JDBC connection properties -->
            <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
            <property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/myDBName;create=true;"/>
            <property name="eclipselink.jdbc.user" value="APP"/>
            <property name="eclipselink.jdbc.password" value="APP"/>
        </properties>
    </persistence-unit>
</persistence>

Source:
Oracle Docs - Chapter 7 Using the Java Persistence API

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61