What is wrong with this persistence unit configuration? - java

I am trying to set up a persistence unit in JEE 6 on JBoss AS 6.
The injected EntityManager is always null. I've fiddled around with the configuration quite a bit but can't get anything to work. I've tried JTA/LOCAL_RESOURCE, I've tried using a unit name in my code the same as in my persistence.xml, but also with the war name prepended, as the log suggests the JNDI name might be that.
I have a mysql-ds.xml file as follows:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>myconnection</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/cog1</connection-url>
<driver-class>org.gjt.mm.mysql.Driver</driver-class>
<user-name>bw</user-name>
<password></password>
<min-pool-size>2</min-pool-size>
<max-pool-size>20</max-pool-size>
<idle-timeout-minutes>5</idle-timeout-minutes>
<exception-sorter-class-name>com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter</exception-sorter-class-name>
<valid-connection-checker-class-name>com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker</valid-connection-checker-class-name>
</local-tx-datasource>
</datasources>
I have a persistence.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<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="myunit" transaction-type="RESOURCE_LOCAL">
<jta-data-source>java:/myconnection</jta-data-source>
<non-jta-data-source>java:/myconnection</non-jta-data-source>
<class>com.cognitura.simulation.dao.model.History</class>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.logging.level" value="INFO" />
</properties>
</persistence-unit>
</persistence>
And I have a bean with the following code in it:
#Stateful
#LocalBean
public class HistoryEJB {
#PersistenceContext(unitName = "myunit")
private EntityManager em;
#EJB
private RealityElement el;
I can see that the RealityElement el is getting injected fine, so I think it's definitely being container-managed?
Grateful for any ideas!
EDIT: The logs seem to be fine for setting up the 'myunit' unit. There is a warning, but I read that it's not important?
01:28:06,276 INFO [org.jboss.jpa.deployment.PersistenceUnitDeployment] Starting persistence unit persistence.unit:unitN
ame=cog1.war#myunit
01:28:06,278 INFO [org.hibernate.ejb.Ejb3Configuration] Processing PersistenceUnitInfo [
name: myunit
...]
01:28:06,305 WARN [org.hibernate.ejb.Ejb3Configuration] Persistence provider caller does not implement the EJB3 spec co
rrectly.PersistenceUnitInfo.getNewTempClassLoader() is null.
Seems happy here:
01:28:06,965 INFO [org.hibernate.impl.SessionFactoryObjectFactory] Bound factory to JNDI name: persistence.unit:unitNam
e=cog1.war#myunit

Could it be the jndi name? Try making the following changes in datasource and persistence files
<jndi-name>java:jboss/datasources/employeedb</jndi-name>
<jta-data-source>java:jboss/datasources/employeedb</jta-data-source>

Related

JBoss EAP 6.3 CMT Multiple persistence units

This is essentially a duplicate of How to locate the source of JBAS011470 error in JBoss?
But essentially, As soon as I add a second persistence unit, it gives me this error. It's ridiculous. I'm not going to disable the JPA subsystem like some people suggest - that sounds wrong.
My persistence.xml setup is as follows, where java:/NAME is set up as a datasource in standalone.xml:
<?xml version="1.0"?>
<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="name" transaction-type="JTA">
<jta-data-source>java:/NAME</jta-data-source>
<class>za.co.classes.A</class>
<class>za.co.classes.B</class>
<class>za.co.classes.C</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<property name="jboss.entity.manager.factory.jndi.name"
value="java:jboss/persistence/NAME" />
<property name="jboss.entity.manager.jndi.name"
value="java:jboss/persistence/em/NAME" />
<property name="hibernate.dialect" value="za.co.equrahealth.dao.SQLServerDialect" />
</properties>
</persistence-unit>
The error starts as soon as I add a second persistence unit. So spring context is irrelevant.
Well, I luckily have multiple databases within the same schema, so I came up with a workaround. But it's obviously not going to solve the problem when there are multiple schemas. I think the cause of this issue might actually be a bug in JBoss.
#PersistenceContext
private EntityManager entityManager;
private EntityManager getEntityManager(String source)
{
if ("a".equalsIgnoreCase(source))
{
entityManager.createNativeQuery("USE A_DB;").executeUpdate();
}
else
{
entityManager.createNativeQuery("USE B_DB").executeUpdate();
}
return entityManager;
}

Binding the following Entries in Global JNDI empty for Firebird

I have trouble binding local and remote versions of service beans to the Global JNDI for Firebird database. To check whether I'm missing something I also setup a test configuration for SQL Server which works just fine. I'm using jaybird-2.1.6.jar. Something is incomplete or wrong in my Firebird JPA setup.
When I use d20pid/D20PIDServiceBean/local to access the local service bean I get an error saying:
Could not get the JNDI resource with the following JNDI name: d20pid/D20PIDServiceBean/local
Understandably so since it hasn't been bound.
Any help would be greatly appreciated! Thank you!
Persistence.xml part for firebird:
<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="D20-PID-DS">
<jta-data-source>java:/D20-PID-DS</jta-data-source>
<class>edu.msu.its.d20pid.ejb.entity.ProvStudent</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.FirebirdDialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
Datasource file firebird details (intentionally left out credentials)
<local-tx-datasource>
<jndi-name>D20-PID-DS</jndi-name>
<connection-url>jdbc:firebirdsql:idcard.dev.ais.msu.edu:/home2/d20/idcard.db</connection-url>
<driver-class>org.firebirdsql.jdbc.FBDriver</driver-class>
<user-name></user-name>
<password></password>
<idle-timeout-minutes>15</idle-timeout-minutes>
<check-valid-connection-sql>SELECT CAST(1 as INTEGER) FROM rdb$database</check-valid-connection-sql>
<track-statements>false</track-statements>
<prepared-statement-cache-size>0</prepared-statement-cache-size>
<metadata>
<type-mapping>Firebird</type-mapping>
</metadata>
</local-tx-datasource>
server.log on EAR deployment; local and remote for D6501ServiceBean (SQL Server) are bound but not for D20PIDServiceBean (Firebird)
17:10:23,318 INFO [SessionSpecContainer] Starting boss.j2ee:ear=d20pid.ear,jar=d20pid-ejb3.jar,name=D20PIDServiceBean,service=EJB3
17:10:23,322 INFO [EJBContainer] STARTED EJB: edu.msu.its.d20pid.ejb.servicebean.D20PIDServiceBean ejbName: D20PIDServiceBean
17:10:23,324 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
17:10:23,327 INFO [SessionSpecContainer] Starting jboss.j2ee:ear=d20pid.ear,jar=d20pid-ejb3.jar,name=D6501ServiceBean,service=EJB3
17:10:23,329 INFO [EJBContainer] STARTED EJB: edu.msu.its.d20pid.ejb.servicebean.D6501ServiceBean ejbName: D6501ServiceBean
17:10:23,357 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
d20pid/D6501ServiceBean/remote - EJB3.x Default Remote Business Interface
d20pid/D6501ServiceBean/remote-edu.msu.its.d20pid.ejb.service.remote.D6501ServiceRemote - EJB3.x Remote Business Interface
d20pid/D6501ServiceBean/local - EJB3.x Default Local Business Interface
d20pid/D6501ServiceBean/local-edu.msu.its.d20pid.ejb.service.local.D6501ServiceLocal - EJB3.x Local Business Interface
My code is working now. I hadn't annotated the local and remote beans with #Local and #Remote. That fixed it.

How to reference external hibernate.cfg.xml from persistence.xml

I have a JAR file that needs to be deployed into multiple environments, each with their own database connection parameters. Rightly or wrongly, I've been requested to make those database connection parameters external to the JAR. I've seen examples where persistence.xml can reference an external hibernate.cfg.xml that contains the specific connection parameters. Here's my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="myApp" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.ejb.cfgfile" value="./hibernate.cfg.xml" />
</properties>
</persistence-unit>
</persistence>
That external hibernate.ejb.cfg is located in the same folder as the JAR file, but the reference is failing. Here's the error I get:
DEBUG Ejb3Configuration - Look up for persistence unit: myApp
DEBUG Ejb3Configuration - Detect class: true; detect hbm: true
DEBUG Ejb3Configuration - Detect class: true; detect hbm: true
DEBUG Ejb3Configuration - Creating Factory: myApp
ERROR MyDaoImpl - initEntityManager() exception:
javax.persistence.PersistenceException: [PersistenceUnit: myApp] Unable to configure EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:265)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:34)
at com.touchnet.MyApp.server.dao.MyDaoImpl.initEntityManager(MyDaoImpl.java:486)
at com.touchnet.MyApp.server.dao.MyDaoImpl.getEntityManager(MyDaoImpl.java:457)
at com.touchnet.MyApp.server.dao.MyDaoImpl.getTransaction(MyDaoImpl.java:512)
at com.touchnet.MyApp.server.dao.MyDaoImpl.beginTransaction(MyDaoImpl.java:502)
at com.touchnet.MyApp.server.service.MyAppService.loadInputFile(MyAppService.java:346)
at com.touchnet.MyApp.server.commandLine.MyAppLoader.main(MyAppLoader.java:74)
at com.touchnet.MyApp.server.commandLine.MyAppLauncher.main(MyAppLauncher.java:44)
Caused by: org.hibernate.HibernateException: C:/MyApp/lib/hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1411)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1433)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:972)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:753)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
... 10 more
ERROR MyDaoImpl - No EntityManager configured.
DEBUG MyDaoImpl - getTransaction() returns null
ERROR MyDaoImpl - No Transaction configured.
How can I access that external hibernate.cfg.xml from persistence.xml?
The error here is
Caused by: org.hibernate.HibernateException: C:/MyApp/lib/hibernate.cfg.xml not found
As it was pointed out in a comment, it looks for it in the folder WEB-INF/clases
Copy the file to that folder and change the following line to:
<property name="hibernate.ejb.cfgfile" value="hibernate.cfg.xml" />
Cheers.

Vaadin JPAContainer doesn't work

I created a web app application like my work thesis. This application used the entire stack java EE 6: JPA2, EJB, JSF, RichFaces....
Now i'm trying to change the UI of my application from RichFaces to Vaadin. The first problems began with use of JPA container. i can't get a instance of entitymanager.
this is my persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="primary">
<jta-data-source>java:jboss/VaadinDS</jta-data-source>
<class>org.mypackage.entity.Utente</class>
<class>org.mypackage.entity.Indirizzo</class>
<class>org.mypackage.entity.Paese</class>
<properties>
<!-- Properties for Hibernate -->
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.show_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
this is my singelton class where i want to get the i instance of entitymanager
public class Utility {
private static EntityManager entityManager;
public Utility() {
// TODO Auto-generated constructor stub
}
public static EntityManager getInstance(){
if(entityManager == null)
entityManager = JPAContainerFactory.createEntityManagerForPersistenceUnit("primary");
return entityManager;
}
}
but doesn't work , i showed always the followed error:
[com.vaadin.server.DefaultErrorHandler] (http-localhost-127.0.0.1-8080-1) : java.lang.NullPointerException
where i wrong?
Your persistence.xml file needs to have a dialect property
Also, you need 2 other things that the book of vaadin explain if you are using hibernate with the jpacontainer: The use of one entity manager per reqeust pattern and the use of hibernate lazy loading delegate on your entity provider. If you need more help, I'll keep updating this answer.

JPA Exception: No Persistence provider for EntityManager named MyJPAApplicationPU

I am newbie with JPA. I'm trying to run some sample code using JPA but I get the following exception:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU
I put my exception message here,
INFO: Could not find any META-INF/persistence.xml file in the classpath
javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyJPAApplicationPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
at com.myJpa.TestJPA.setUp(TestJPA.java:30)
at com.myJpa.TestJPA.main(TestJPA.java:72)
Any help would be appreciated.
Well, the error is self explaining, you need to provide a META-INF/persistence.xml to use JPA. This file is used to define a "persistence unit". From the JPA 1.0 specification:
6.2.1 persistence.xml file
A persistence.xml file defines a
persistence unit. It may be used to
specify managed persistence classes
included in the persistence unit,
object/relational mapping information
for those classes, and other
configuration information for the
persistence unit and for the entity
manager(s) and entity manager factory
for the persistence unit. The
persistence.xml file is located in
the META-INF directory of the root
of the persistence unit. This
information may be defined by
containment or by reference, as
described below.
Here is a sample persistence.xml for a Java SE environment (using Hibernate as JPA provider):
<?xml version="1.0" encoding="UTF-8"?>
<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_1_0.xsd"
version="1.0">
<persistence-unit name="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.mycompany.Foo</class>
<class>com.mycompany.Bar</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.DriverManagerConnectionProvider" />
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-testing-jpa"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
Some comments about the above file:
when running in a Java SE environment, you cannot rely on JTA and the transaction type must be RESOURCE_LOCAL (which is actually the default in a Java SE environment but specifying it make it more clear).
when running in a Java SE environment, you cannot use a JDNI data source and the provider will obtain connections directly from the JDBC driver so you must pass the relevant informations to the provider (driver class name, connection url, user, password). With JPA 1.0 the properties used to pass these metadata are provider specific.
To insure the portability of a Java SE application, it is necessary to explicitly list the managed persistence classes that are included in the persistence unit.
For JPA to work, you need META-INF/persistence.xml. I will assume this is a web-app, so this folder has to be in WEB-INF/classes/.
The persistence.xml file would look like this:
<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_1_0.xsd"
version="1.0">
<persistence-unit name="MyJPAApplicationPU" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
</persistence>
EntityManagerFactory emf = Persistence.createEntityManagerFactory("<JDBC connection>");
Check the correct JDBC Connection.

Categories

Resources