I am currently testing out using OSGi. I am running this through Eclipse. I want to have my DAO layer as part of an OSGi solution, but my first stumbling block is this error:
Jun 29, 2009 6:12:37 PM org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.0.GA
Jun 29, 2009 6:12:37 PM org.hibernate.ejb.Version <clinit>
INFO: Hibernate EntityManager 3.3.0.GA
Jun 29, 2009 6:12:37 PM org.hibernate.ejb.Ejb3Configuration configure
INFO: Could not find any META-INF/persistence.xml file in the classpath
I have tried putting the persistence.xml file in a lot of different places, to no avail. Any ideas on what I am doing wrong?
Is there a way to manually load the persistence.xml?
The activator looks like this:
package com.activator;
public class PersistenceActivator implements BundleActivator {
#Override
public void start(BundleContext arg0) throws Exception {
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("postgres");
EntityManager em = emf.createEntityManager();
SimpleDaoImpl dao = new SimpleDaoImpl();
dao.setEntityManager(em);
}
#Override
public void stop(BundleContext arg0) throws Exception {
// TODO Auto-generated method stub
}
}
Here is what my directory structure looks like:
alt text http://www.freeimagehosting.net/uploads/7b7b7d2d30.jpg
Here is my Manifest.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Dao Plug-in
Bundle-SymbolicName: Dao
Bundle-Version: 1.0.0
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.4.0"
Bundle-Activator: com.activator.PersistenceActivator
Export-Package: com.dao.service
Require-Bundle: HibernateBundle;bundle-version="1.0.0"
HibernateBundle contains all of the Hibernate and Persistence Jars.
Here is my Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence>
<!-- Sample persistence using PostgreSQL. See postgres.txt. -->
<persistence-unit name="postgres" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<!--
Comment out if schema exists & you don't want the tables dropped.
-->
<property name="hibernate.hbm2ddl.auto" value="create-drop" /> <!-- drop/create tables #startup, drop tables #shutdown -->
<!-- Database Connection Settings -->
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="postgres" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/test" />
<!-- Not sure about these... -->
<property name="hibernate.max_fetch_depth">16</property>
<property name="hibernate.jdbc.batch_size">1000</property>
<property name="hibernate.use_outer_join">true</property>
<property name="hibernate.default_batch_fetch_size">500</property>
<!-- Hibernate Query Language (HQL) parser. -->
<property name="hibernate.query.factory_class">
org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<!-- Use c3p0 for the JDBC connection pool -->
<property name="hibernate.c3p0.min_size">3</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />
</properties>
</persistence-unit>
</persistence>
Things I have tried in the Manifest's Classpath with no luck:
Bundle-ClassPath: ., META-INF/persistence.xml
Bundle-ClassPath: ., ../META-INF/persistence.xml
Bundle-ClassPath: ., /META-INF/persistence.xml
Bundle-ClassPath: ., ./META-INF/persistence.xml
Bundle-ClassPath: ., META-INF
Bundle-ClassPath: ., ../META-INF
Bundle-ClassPath: ., /META-INF
Bundle-ClassPath: ., ./META-INF
Bundle-ClassPath: ., C:\Workspaces\OSGiJPA\Dao\META-INF\persistence.xml
Bundle-ClassPath: ., C:\Workspaces\OSGiJPA\Dao\META-INF
Use EclipseLink and forget about Hibernate and other implementations, because :
You'll have to play with the classloader too much...
Thread.currentThread().setContextClassLoader(...)
You'll be tempted to set the bundle-classpath attribute and add dependencies manually instead of installing jar bundles.
You'll get provider not found errors or you might not be able to find persistence.xml
All the above efforts might not work after many attempts.
However, with EclipseLink it's a no brainer, the implementation was designed to work out of the box in an OSGI environment and there aren't any class loading headaches.
(only a suggestion): Better if you use a lazy loader instead do the job into the activator. For example use a singleton that is invoked into SimpleDaoImpl contructor.
Move META-INF/persistent.xml under src folder (src/META-INF/persistent.xml) because under develop META-INF folder is not in classpath, it works only in runtime mode.
If you are using EclipseLink jpa OSGi, your MANIFEST.MF missing of JPA-PersistenceUnits entry. Add
JPA-PersistenceUnits: postgres
into the MANIFEST.MF.
Then in your launch configuration set the start level of org.eclipse.persistence.jpa.osgi (for ecliselink 2.3.x otherwise org.eclipse.persistence.jpa for 2.1.x) to 2 and start level of javax.persistence to 1.
GOOD LUCK, actually 2.3 has a problem in deployment, doesn't handle bundleresource://xxxx URLs :(, 2.1.2 works very well ;)
I am not using persistence.xml but hibernate.cfg.xml which is similar:
src/main/resource/hibernate/hibernate.cfg.xml
In my Activator I am getting the file via the bundle context:
Here is some example code how I do it and also reference that file:>
private void initHibernate(BundleContext context) {
try {
final AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.configure(context.getBundle().getEntry("/src/main/resource/hibernate/hibernate.cfg.xml"));
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
As you can see line which gets the config file is:
context.getBundle().getEntry("/src/main/resource/hibernate/hibernate.cfg.xml")
As you can see my hibernate.cfg.xml is NOT inside the META-INF folder. It is just in the root folder under /src/......
Hope that helps.
Christoph
You need to have the directory that contains META-INF on the classpath. Each directory is searched for META-INF and if found, then persistence.xml is searched for.
If you put "META-INF" on the classpath, then you'd need another META-INF in that directory.
Try using Bundle-ClassPath like this in your manifest
Bundle-ClassPath: ., /location/of/persistence.xml
The Meta-inf directory is not on the classpath. This should work by simply placing it under your src dirctory. If you want it in a separate location, then you will have to specify the Bundle-Classpath to include that directory. By default the classpath is '.'.
I'm getting the same problem.
I think eclipse link is the best option to use in a OSGi Environment. And there are no problem because you will work basically with the JPA implementation. When you need to move to Hibernate, just replace persintece.xml config and some libs.
you need to set property (for hibernate it will be different):
javax.persistence.provider=org.apache.openjpa.persistence.PersistenceProviderImpl
for call:
Persistence.createEntityManagerFactory(entityManagerFactoryName, properties)
to make it work.
And as mentioned before, you need classloader wrapping. You can use ClassloaderEntityManager from https://issues.apache.org/jira/browse/OPENJPA-1783 to do that.
Regards
Related
I have an application running on Glassfish 4.1 that uses a JDBC Resource. In the application itself I have a persistence.xml file that lists all the entities and tells the container which JDBC Resource to use. I have defined some properties to log the SQL it executes. It looks something like this:
<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="MyResource_PU" transaction-type="JTA">
<jta-data-source>jdbc/my_resource</jta-data-source>
<class>com.example.entities.EntityOne</class>
<class>com.example.entities.EntityTwo</class>
<class>com.example.entities.EntityThree</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>
Now when this application goes to production, I don't want the SQL to be logged. So every time I do a release, I need to remind myself to change the eclipselink.logging properties.
I thought, there should be a better way to handle this. So I went to the Glassfish admin console, to JDBC Resources and added the 2 properties there, but that doesn't work.
Is there any way to remove the "environment specific" values out of the persistence.xml file and into the Glassfish configuration? I have googled for a while now, but don't seem to find the correct sollution. What I did find is that Hibernate has the option to specify a "configuration file", but I haven't found this for EclipseLink.
We used maintain property files for each environment such as DEV,QA,PROD,UAT etc in different files and copy one of them during build.
Ant build
<property environment="env" />
<!-- ***** COMMAND LINE ARGUMENTS DEMOED HERE -->
<property name="build_type" value= "${env.build_type}"/>
<copy todir="deploy">
<fileset dir="src_dir"/>
<globmapper from=${env.build_type}".persistence.xml" to="persistence.xml"/>
</copy>
Run build like this
ant -Denv.build_type=PROD
This will copy PROD.persistence.xml to persistence.xml
ant -Denv.build_type=DEV
This will copy DEV.persistence.xml to persistence.xml
As per glassfish documentation, persistence.xml settings are meant to take precedence over global settings, so I wouldn't recommend anything in a persistence.xml file that you would want to override later.
That said, EclipseLink server integration can make use of a server log, allowing external control over settings. See this for a description of setting logging properties in glassfish that should control the log file EclipseLink writes to. Otherwise, you can define a different log mechanism in your peristence.xml file, such as log4J or a custom one that you can control how you wish as touched on here
I've been diving into ServiceMix 5.4.0 and OSGi, and have run across a rather weird behavior with OpenJPA.
I have a data source defined like so:
<blueprint
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/test"/>
<property name="username" value="test"/>
<property name="password" value="test"/>
</bean>
<service interface="javax.sql.DataSource" ref="dataSource">
<service-properties>
<entry key="osgi.jndi.service.name" value="jdbc/test"/>
</service-properties>
</service>
</blueprint>
Using the jndi:names command, I can verify that the data source is visible:
karaf#root> jndi:names
JNDI Name Class Name
osgi:service/jndi org.apache.karaf.jndi.internal.JndiServiceImpl
osgi:service/jdbc/test org.apache.commons.dbcp.BasicDataSource
karaf#root>
My persistence.xml:
<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="test" transaction-type="JTA">
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/test)</jta-data-source>
<class>com.example.persistence.security.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.jdbc.DBDictionary" value="postgres"/>
<property name="openjpa.Log" value="slf4j"/>
</properties>
</persistence-unit>
</persistence>
I then inject the persistence unit into a DAO class via Blueprint:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint default-activation="eager"
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0"
xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0">
<bean id="securityDAO" class="com.example.security.dao.SecurityDAOImpl" init-method="init">
<tx:transaction method="*" value="Required" />
<jpa:context property="entityManager" unitname="test" />
</bean>
<service ref="securityDAO" interface="com.example.security.dao.SecurityDAO">
</service>
</blueprint>
The persistence unit is successfully injected, which I verify in the init-method of the DAO:
public void init() {
if (em==null) {
log.error("Entity manager not found. Check JPA configuration.");
throw new RuntimeException("No EntityManager found");
}
log.info("Started SecurityDAO");
}
After all my diligent work, ServiceMix rewards me with the following cryptic exception when I call my DAO's method from another bean:
....
public void setSecurityDAO (SecurityDAO dao) {
this.dao = dao;
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userName = req.getParameter("userName");
String password = req.getParameter("password");
// Invocation of injected DAO results in exception
User u = dao.authenticateUser(userName, password);
This results in the following:
Caused by: java.lang.RuntimeException: The DataSource osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/test) required by bundle persistence/0.0.1.SNAPSHOT could not be found.
at org.apache.aries.jpa.container.unit.impl.JndiDataSource.getDs(JndiDataSource.java:87)
at org.apache.aries.jpa.container.unit.impl.DelayedLookupDataSource.getConnection(DelayedLookupDataSource.java:36)
at org.apache.openjpa.lib.jdbc.DelegatingDataSource.getConnection(DelegatingDataSource.java:116)
at org.apache.openjpa.lib.jdbc.DecoratingDataSource.getConnection(DecoratingDataSource.java:93)
at org.apache.openjpa.jdbc.schema.DataSourceFactory.installDBDictionary(DataSourceFactory.java:233)
... 54 more
Caused by: javax.naming.NoInitialContextException: Unable to find the InitialContextFactory org.eclipse.jetty.jndi.InitialContextFactory.
at org.apache.aries.jndi.ContextHelper.getInitialContext(ContextHelper.java:148)
at org.apache.aries.jndi.OSGiInitialContextFactoryBuilder.getInitialContext(OSGiInitialContextFactoryBuilder.java:49)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at org.apache.aries.jpa.container.unit.impl.JndiDataSource.getDs(JndiDataSource.java:64)
... 58 more
Somehow the OSGi-exported data source is not finding its way into the persistence bundle. The strange part is that when I added the following code to the init-method to see if I could execute a test query, not only does OpenJPA not throw an exception in the init method, the invocation of the DAO that was triggering the exception now works as well:
public void init() {
if (em==null) {
log.error("Entity manager not found. Check JPA configuration.");
throw new RuntimeException("No EntityManager found");
}
try {
Query q = em.createNativeQuery("SELECT 1=1");
q.getFirstResult();
} catch (Exception ex) {
log.error("Unable to execute test query against database", ex);
throw new RuntimeException(ex);
}
log.info("Started SecurityDAO");
}
So, to summarize: If I call a method from a different bundle than my DAO, OpenJPA throws an exception indicating that it can't find the InitialNamingContext, and does not show any indication in the log that it has started. If I execute a query inside my DAO before an external component calls into it, somehow OpenJPA is able to find the InitialNamingContext, OpenJPA shows up in the log, and subsequent invocations from outside the DAO bundle begin to work.
Obviously, I'm missing something basic here. Any help or thoughtful explanation of what's breaking, or what I'm doing wrong, will be greatly appreciated.
EDIT:
I hadn't noticed last night, but when I added in the test query, the following lines appear in the log. They are absent when I comment out that query:
... | Runtime | 220 - org.apache.openjpa - 2.3.0 | Starting OpenJPA 2.3.0
... | JDBC | 220 - org.apache.openjpa - 2.3.0 | Using dictionary class "org.apache.openjpa.jdbc.sql.PostgresDictionary".
... | JDBC | 220 - org.apache.openjpa - 2.3.0 | Connected to PostgreSQL version 9.9 using JDBC driver PostgreSQL Native Driver version PostgreSQL 9.3 JDBC4.1 (build 1102).
EDIT 2:
Tried it on plain vanilla Karaf 3.0.3, and got the same error. As a workaround, I created a separate bean in the bundle that executes the above-mentioned test query. Apparently, as long as a single bean in the bundle makes a call to OpenJPA before a bean outside the bundle tries to make a call, OpenJPA will be correctly initialized.
Since this is mentioned nowhere I can see in the OpenJPA/ServiceMix docs, I can only presume that I'm doing something wrong elsewhere in my configuration.
EDIT 3:
Per John Forth, here is the MANIFEST.MF
Manifest-Version: 1.0
Bnd-LastModified: 1430533396366
Build-Jdk: 1.8.0_45
Built-By: somedude
Bundle-Blueprint: OSGI-INF/blueprint/blueprint.xml
Bundle-Description: Database access layer for Peer Review product
Bundle-ManifestVersion: 2
Bundle-Name: Example :: Persistence
Bundle-SymbolicName: persistence-jpa
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: com.example.persistence.security;version="0.0.1.SNAPSHOT",co
m.example.security.dao;version="0.0.1.SNAPSHOT";uses:="com.example.persistence.
security,javax.persistence"
Export-Service: com.example.security.dao.SecurityDAO
Import-Package: javax.persistence;version="[1.1,2)",org.osgi.service.blu
eprint;version="[1.0.0,2.0.0)",org.slf4j;version="[1.7,2)"
Meta-Persistence: META-INF/persistence.xml
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.7))"
Tool: Bnd-2.3.0.201405100607
And, since it may be related, the pom.xml of the JPA bundle:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>example</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>persistence-jpa</artifactId>
<packaging>bundle</packaging>
<name>Example :: Persistence</name>
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jpa_2.0_spec</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.5.3</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Meta-Persistence>META-INF/persistence.xml</Meta-Persistence>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Import-Package>*</Import-Package>
<Export-Package>com.example.persistence*,com.example.security.*;version=${project.version}</Export-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
If you are using OSGI the class visibility is defined in the MANIFEST.MF files.
Thus the persistence bundle can only see and load classes which are imported in its MANIFEST.MF.
A proper way to extend an existing bundle is to define a fragment which is attached to the existing bundle. This way you can provide classes (e.g. DAOs) and files (e.g. persistence.xml) and make the visible to the fragment-host.
The MANIFEST.MF then looks like
Bundle-ManifestVersion: 2
Bundle-Name: foo.bar.openjpa-fragment
Bundle-SymbolicName: foo.bar.openjpa-fragment;singleton:=true
Bundle-Version: 0.0.1.SNAPSHOT
Bundle-Vendor: foo bar
Fragment-Host: org.apache.openjpa-bundle
Bundle-ClassPath: .
Note that this is only an example.
OSGI means to provide proper visibility.
You can add more than one fragment to an existing bundle, e.g. to keep the configuration in a separate bundle, wich makes it easier to switch the configuration.
I decided to try out Hibernate today, which looks promising, although the setup could be easier. After having found solutions for the first million or so configuration errors I'm now stuck with this one:
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: manager1] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
(...)
Caused by: org.hibernate.HibernateException: Bean Validation not available in the class path but required in javax.persistence.validation.mode
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.applyHibernateListeners(BeanValidationIntegrator.java:281)
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:134)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:303)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1750)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
... 5 more
Peristence.xml:
<?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="manager1" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>net.zomis.hibernate.Game</class>
<class>net.zomis.hibernate.TestFields</class>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<validation-mode>CALLBACK</validation-mode>
<properties>
<property name="javax.persistence.validation.mode" value="NONE" />
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
</properties>
</persistence-unit>
</persistence>
The (as far as I can tell) relevant portions of hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
(...)
<property name="hbm2ddl.auto" >validate</property>
<mapping class="net.zomis.hibernate.Game" />
<mapping class="net.zomis.hibernate.TestFields" />
</session-factory>
</hibernate-configuration>
I have tried adding this to the build path without any change. I have googled for Bean Validation not available in the class path but required in javax.persistence.validation.mode and also just Bean Validation not available in the class path. I found the source code, but that doesn't help me much.
I have also tried removing the properties from the xml configuration files, without any luck.
I do not use Maven. I simply use Eclipse and build and run the project directly from Eclipse.
The obvious question is: How can I solve this problem? Do I need an additional jar in my build path that I have missed? (In that case which one?) and also: Why is it needed? Can I change something so that it is not needed anymore?
You need to have the validation-api (there's a download JAR link you can use as you don't use maven) in your classpath.
Hibernate searches for the javax.validation.Validation class in the classpath.
If it doesn't find it it throws the Bean Validation not available in the class path but required in javax.persistence.validation.mode error.
If you're not using maven you need to build up the classpath yourself and it's easy to miss a jar. It might be worthwhile to just use maven to bootstrap your project, and to make sure you have all the jars in place.
If you're using maven, the validation-api is pulled in as a transitive dependency from hibernate-validator.
You need the Hibernate Validator artifact:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
This will also include the Bean Validation API, which indeed is in this case a requirement as well.
In case you are not using any Bean Validation constraints (e.g. #NotNull, #Before, #Pattern, etc) you could also just remove
<validation-mode>CALLBACK</validation-mode>
or set it to NONE. Bean Validation is not required for Hibernate usage it is just an add-on defined in the JPA specification. If Bean Validation is on the classpath life cycle based validation (validation of entities on pre-update, pre-persist, etc) is automatically enabled (see also http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#d0e3096 and of course the JPA 2 specification).
On the other hand, if you don't have Bean Validation on the classpath, but explicitly ask for it via the configuration option in persistence.xml you get the error you describe.
I'm back with the same problem ...
I'm trying to uses queries in my Cassandra DB with Kundera (Cassandra ORM), this queries work in an others project but when I try to do it in webapp (using tomcat 6.0), I got this error :
com.impetus.kundera.metadata.KunderaMetadataManager - No Entity metadata found for the class
=> JavaNullPointerException.
But when I leave the persistence.xml from my project I got an other error. (NoPersistence.xml found or something ... )
So, my project found Persistence.xml, but not my Entity class : fileCassandra.
You can see my persistence.xml :
<?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">
<!-- 192.168.3.107 -->
<persistence-unit name="cassandra_pu">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<class>net.***.common.db.***.FileCassandra</class>
<properties>
<property name="kundera.nodes" value="localhost"/>
<property name="kundera.port" value="9160"/>
<property name="kundera.keyspace" value="KunderaExamples"/>
<property name="kundera.dialect" value="cassandra"/>
<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />
<property name="kundera.cache.provider.class" value="com.impetus.kundera.cache.ehcache.EhCacheProvider"/>
<!-- <property name="kundera.cache.config.resource" value="/ehcache-test.xml"/> -->
</properties>
</persistence-unit>
</persistence>
net..common.db..FileCassandra I must replace by * because it's name from my companie ;)
The same methods (include EntityManager) works in junit on other project, when I build my project in Tomcat, this error appears ...
This happens when you have multiple entries of the same class in your classpath.
The ideal place to have your entities is closest to the same class loader which loads kundera core and client(HBase, Cassandra etc.).
For example, if these kundera files are under WEB-INF/lib, you'd rather have your entities under the application where as if kundera files are on the applications lib folder, better bundle your entities in a jar and put them there (and remove the entities in your app).
Only issue which i can see is classes and persistence.xml location.
try to place persistence.xml within /WEB-INF/classes/META-INF/, Provided that your entity definitions are within classes folder!
-Vivek
im developing a standalone application and it works fine when starting it from my ide(intellij idea), but after creating an uberjar and start the application from it javax.persistence.spi.PersistenceProvider is thrown saying "No Persistence provider for EntityManager named testPU"
here is my persistence.xml which is placed under meta-inf directory:
<persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>test.model.Configuration</class>
<properties>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
and here is how im creating the entity manager factory:
emf = Persistence.createEntityManagerFactory("testPU");
im using maven and tried the assembly plug-in with the default configuration fot it, i dont have much experience with assembling jars and i dont know if im missing something, so if u have any ideas ill be glad to hear them
You are probably having problems with your libraries.. Try doing below.
Build your application JAR File
Get all libraries that you have used for the application and put them in a folder lib.
Place your JAR file and lib folder in a new folder say MyApp.
Open your file by using 7-ZIP or WinRAR. Look for Manifest.MF in the META-INF folder.
Your manifest file should look something like..
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.6.0_03-b05 (Sun Microsystems Inc.)
Main-Class:
Class-Path: lib/.jar lib/.jar lib/.jar ...
I remember having that issue, and I think the problem was that you can't reference a jar in a jar. For your external libraries, they need to be expanded inside your jar or live elsewhere on the system CLASSPATH. So since you don't specify what all you're putting in your jar, I'm betting on this one.
You can NOT use the -cp command line parameter if you run your jar using the -jar parameter. One or the other.
When using the Maven Assembly Plugin with the predefined jar-with-dependencies descriptor, you get a jar archive which contains the binary output of your project, along its the unpacked dependencies. So one possible problem I can think of would be multiple JARs with persistence.xml, in which case I'm not sure which one you'll get in the final assembly.
Since you get an error message complaining about "No Persistence provider for EntityManager named testPU", I would open the megajar and:
check that the persistence.xml is present (it should)
check that it contains the expected persistence unit testPU
if it doesn't, find the origin of the conflicting file and rewrite the assembly descriptor to exclude it