NullPointerException on Heroku but in Glassfish everythin is ok? - java

I'm developing a simple application on JAVA EE using JSF, JPA and Maven.
the question is: Why the tomcat servlet on Heroku is returning null on the time to inject a ManagedBean while in Glassfish server everything is fine?
There are my codes:
#ManagedBean(name = "homeBean")
#SessionScoped
public class HomeBean {
//simple properties
#ManagedProperty(value="#{emFactoryBean}")
private EntityManagerFactoryBean factoryBean;
#PostConstruct
public void init(){
sitioService = new SitioService(factoryBean);
sitios = sitioService.getSitios();
}
//Getters and setters
}
The exception occurs when a new instace of SitioService is created, In other words. Here is the factorBean Class:
#ApplicationScoped
public class EntityManagerFactoryBean {
private EntityManagerFactory entityManagerFactory;
public EntityManagerFactoryBean() {
this.entityManagerFactory = Persistence.
createEntityManagerFactory("prueba");
}
//getter and setter
}
My persistence.xml is:
<?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="prueba" transaction-type="RESOURCE_LOCAL">
<class>com.sertracen.sivt.modelo.Imagen</class>
<class>com.sertracen.sivt.modelo.Review</class>
<class>com.sertracen.sivt.modelo.Sitio</class>
<properties>
<property name="javax.persistence.jdbc.url" value="{url}"/>
<property name="javax.persistence.jdbc.user" value="{usuario}"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.password" value="{contraseƱa}"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation.scripts.action" value="create"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
</properties>
</persistence-unit>
Obviously my parameter connections are ok.
Hope for your answers!
Cheers!
----STACK TRACE:
om.sun.faces.mgbean.ManagedBeanCreationException: An error occurred performing resource injection on managed bean homeBean
....
Caused by: java.lang.NullPointerException
at com.sertracen.sivt.negocio.SitioService.<init>(SitioService.java:26)
at com.sertracen.sivt.web.HomeBean.init(HomeBean.java:75)

Related

service exeption when creating entity manager factory

I tried building my project today and am having problems connecting to MySQL. I am getting an exception : org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] when I try to create my entity manager factory. It was working fine yesterday. I have checked MySql is running and have tried to restart computer but nothing has worked.
Here is my 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="VictoryPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.stevie.memberfx.model.HouseholdEntity</class>
<class>com.stevie.memberfx.model.MemberEntity</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/victorydb?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.user" value="stephen"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="ozzie2014"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
and my code to create the entity manager factory:
public class JpaUtil {
private static final EntityManagerFactory entityManagerFactory;
static {
try {
entityManagerFactory = Persistence.createEntityManagerFactory("VictoryPU");
} catch (Throwable ex) {
// Log the exception.
System.out.println("Initial EntityManagerFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}
}
You have to change mysql driver to version 5 and change the driver name in your persistence.xml file to the old name com.mysql.jdbc.Driver

No persistence provider

I am trying to run a very basic JPA example on Wildfly.
This is the structure of my project:
Person and Project are two JPA entities.
This is the content of my 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="testJpa">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>entities.Person</class>
<class>entities.Project</class>
<properties>
<property name="eclipselink.target-database" value="Derby"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/JPADB"/>
<property name="javax.persistence.jdbc.user" value="user"/>
<property name="javax.persistence.jdbc.password" value="pwd"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Tables are correctly generated from the entity classes so I know the persistence.xml is valid.
I am trying to create an entity in the CreatePerson class, like this:
public class CreatePerson {
public static void main( String[ ] args ) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("testJpa");
EntityManager entitymanager = emfactory.createEntityManager( );
entitymanager.getTransaction( ).begin( );
Person usr = new Person( );
usr.setName("Bob");
entitymanager.persist( usr );
entitymanager.getTransaction( ).commit( );
entitymanager.close( );
emfactory.close( );
}
}
I get the following error
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named testJpa
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at services.CreatePerson.main(CreatePerson.java:11)
It is totally unclear what are you doing. Wildfly is an JavaEE application server, but I see JavaSE class public static void main.
You should create JavaEE project (for Eclipse, as far as I know you need to download Eclipse IDE for Java EE Developers ) then probably create DAO EJB, inject EntityManager in it with
#PersistenceContext(unitName = "puName")
private EntityManager em;
then you code should work
Person usr = new Person("blabla");
em.persist(usr)
IMHO, you should look for another tutorial.

exception JPA derby config for junit?

I have a config file persistence.xml as mentioned below:
<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="entity"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<validation-mode>NONE</validation-mode>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:src/test/resources/sql/entityDB;create=true" >
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.logging.level" value="ALL" />
</properties>
</persistence-unit>
and load persistence code as:
public static void main(String[] args) {
String persistenceUnit = "entity";
Properties pros = new Properties();
pros.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML,
"src/test/resources/META-INF/persistence.xml");
// Get the entity manager for the tests.
entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit,pros);
entityManager = entityManagerFactory.createEntityManager();
Query q = entityManager.createQuery("select * from entityTables");
List<Entity> todoList = q.getResultList();
entityManager.close();
}
But when I am calling createEntityManagerFactory I am getting bellow exception:
Exception in thread "main" javax.persistence.PersistenceException:No Persistence provider for EntityManager named entity
How can I fix it?
After <persistence-unit name="entity" transaction-type="RESOURCE_LOCAL">, use the persistence provider name:
<provider>org.hibernate.ejb.HibernatePersistence</provider>
Edit : or put in the classpath of the manifest this packages : - persistence.jar - eclipselink.jar

Would providing `EntityManagerFactory` solve `Unable to retrieve EntityManagerFactory for unitName xyz`

This is the (bottom) error from the stack trace:
Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:138)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:171)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:477)
...
The code is as follows (inspired by a #BalusC's example I found on SO today):
#Stateless
public class GradService {
#PersistenceContext
private EntityManager em;
public List<Grad> listAll() {
return em.createQuery("SELECT g FROM Grad g", Grad.class).getResultList();
}
}
As reference, this is my persistence.xml (webmodule-root:Configuration files:WEB-INFMETA-INF: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="MojaPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/ekstraResource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
It seems, that you placed your persistence.xml file in wrong location. It should by located in /META-INF/ folder and not in WEB-INF.

#PersistenceContext is null on Glassfish embedded (no ejb)

im facing following problem. In my application I'm trying to use #PersistenceContext injected into the NON ejb instance of DAO by Glassfish embedded server. So it's a pojo controlled by CDI. Like this :
#Named
public class DummyDAO implements Serializable {
#PersistenceContext private EntityManager entityManager;
public void testManager() {
if (entityManager == null) {
throw new RuntimeException("It's null!!");
}
System.out.println("Hello! I'm injected");
}
}
This dummy DAO injects into a JSF bean by CDI like this :
#ManagedBean
#SessionScoped
public class OrderImportManagerBean implements Serializable {
#Inject
DummyDAO dummyDAO;
public void testManager() {
dummyDAO.testManager();
}
}
testManager() called from an xhtml page like #{orderImportManagerBean.testManager}
entityManager is always null. The CDI itself works, all instances injected as they should but not this one. I have persistence.xml and orm.xml under resources/META-INF catalog. So after the project is packaged I get META-INF and stuff in right place (classes/META-INF). Here is my persistence.xml :
<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="persistance-unit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
<property name="javax.persistence.jdbc.user" value="sa"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name = "hibernate.show_sql" value = "true" />
</properties>
</persistence-unit>
</persistence>
Now I'm wondering what is wrong with my configuration? Any suggestions?
Thanks.

Categories