EclipseLink: Entities not loaded - java

I have a JPA-Project, which I'm trying to update to JPA 2.2.0 and EclipseLink 5.7.1 since I ran into bug 429992 of EclipseLink. With the new versions in place, I'm not able to execute my application anymore – EclipseLink throws an exception similar to the following (Short variant from my example below):
[EL Warning]: metamodel: 2018-06-20 22:38:14.1--Thread(Thread[main,5,main])--The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units. Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
[...]
Exception in thread "main" java.lang.IllegalArgumentException: Object: Artifact#17d919b6 is not a known Entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4324)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:593)
at Main.main(Main.java:12)
Before the update, everything worked fine (besides the aforementioned bug), and also if I checkout an earlier commit, there are no problems.
I have reproduced this behaviour with the minimal setup attached below.
The project is compiled using Java SE 10, as IDE I'm using Eclipse, but in my project properties, I only have the option to select "Generic 2.1" as JPA-Platform. May this be an problem?
Are you able to reproduce this error?
As far as I can see, the Entity-class is listed in the persistence.xml and also annotated with #Entity, but not loaded by EclipseLink. Cleaning the project or even creating a new one does not solve the problem.
Do you have an idea, what my mistake might be? Am I missing any fundamental point about the usage of JPA 2.2/EclipseLink 2.7.1?
Thank you for any hints or comments!
main method in the main class:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Example");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
Artifact artifact = new Artifact();
entityManager.persist(artifact);
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
Entity Artifact:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
#Entity
#NamedQuery(name = "Artifact.findAll", query = "SELECT a FROM Artifact a")
public class Artifact {
private int id;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
}
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="Example">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>Artifact</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:./inventory;create=true"/>
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
</properties>
</persistence-unit>
</persistence>
pom.xml for Maven dependencies:
<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>
<groupId>JPATest</groupId>
<artifactId>JPATest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>10</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
</dependencies>
</project>

I had the same problem too. finally, it was solved with an update in pom.xml.
Eclipselink 2.7.1 version has some bugs that fixed at newer versions.
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.7.1</version>
</dependency>
to
<version>2.7.5</version>
before the update, entities are not loaded and em.getMetamodel() was empty.
after the update, all of my entities are loaded successfully
issues and the fixed bugs can be accessed from this link.

I have pretty much the same environment and experience the same problem. I hardly dare to say but in eclipse a "project/maven/update project ..." helped (so far).

Related

Can not obtain connection to DB using JPA - derby embedded driver class missing

I am trying to create local derby database using jpa. As JPA implementation i am using openjpa and as sql implementation derby.
This is persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2"
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_2.xsd">
<persistence-unit name="DataLayer"
transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>DataSource</non-jta-data-source>
<properties>
<property
name="javax.persistence.schema-generation.database.action"
value="create" />
<property name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:testdb" />
</properties>
</persistence-unit>
</persistence>
This is my main:
public static void main(String[] args)
{
Persistence.generateSchema("DataLayer", null);
EntityManagerFactory factory = Persistence.createEntityManagerFactory("DataLayer");
EntityManager manager = factory.createEntityManager(); // <- Exception here
}
This is starting part of exception i get:
Exception in thread "main" <openjpa-3.1.2-r66d2a72 nonfatal general error> org.a
pache.openjpa.persistence.PersistenceException: There were errors initializing y
our configuration: <openjpa-3.1.2-r66d2a72 fatal user error> org.apache.openjpa.
util.UserException: A connection could not be obtained for driver class "org.apa
che.derby.jdbc.EmbeddedDriver" and URL "jdbc:derby:testdb". You may have spe
cified an invalid URL.
...
saying that i may have specified an invalid URL.
Complete stack trace: https://justpaste.it/3kmgo
Root exception: java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
I have specified EmbeddedDriver in persistence.xml, also included Maven dependency.
Also i think my connection url and driver is correct according to docs about derby:
https://db.apache.org/derby/docs/10.15/devguide/rdevdvlp22102.html#rdevdvlp22102
https://db.apache.org/derby/docs/10.15/devguide/cdevdvlp40653.html
Dependencies included (using Maven):
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.2.0</version>
</dependency>
I tried to change property javax.persistence.jdbc.url to jdbc:derby:testdb;create=true, but same exception. Also tried to set javax.persistence.schema-generation.database.action property to none.
Found out that org.apache.derby.jdbc.EmbeddedDriver class is not in derby artifactid dependency (it has derby and derbyshared jar's). It is in derbytools, so including derbytools to dependencies solve issue abbout missing EmbeddedDriver class:
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbytools</artifactId>
<version>10.15.2.0</version>
</dependency>
Using Debry 10.15.2.0 the Embedded driver is located in Derbytools.jar. Thus must be in your libraries and classpath. Created a new driver(I'd deleted any drivers that say anything Embedded). In the Services tab right click Drivers and Create New. Add Derbytools.jar Highlight it and click the Find Button. Give it a meaningful name and it works as expected. Just to be sure I also added Derby.jar and DerbyShared.jar to avoid problems.

ServiceMix unable to find OSGI data source

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.

DataNucleus / Kundera with HBase with ElementCollection - Unable to find the object with a null id

To start with, I use HBase 0.94.5 (also tried 0.92.2 with the same results).
I have a situation like this:
Variable class:
#Entity
#Table(name = "variable", schema = "keyspace#hbase-pu")
public class Variable {
#Id
private String Id;
#Column(name = "Name")
private String Name;
#ElementCollection(fetch = FetchType.EAGER)
//#CollectionTable(name = "datavalues") // Doesn't work with or without.
private List<DataValue> DataValues;
// Getters and setters omitted.
}
DataValue class:
#Embeddable
public class DataValue {
#Column(name = "Value")
private Object Value;
// Getters and setters omitted.
}
Now, storing this is no problem at all, but when I want to retrieve it, boom goes the dynamite. This exception is thrown:
Caused by: org.datanucleus.exceptions.NucleusUserException: Unable to find the object with a null id!
Exception in thread "main" javax.persistence.PersistenceException: Unable to find the object with a null id!
at org.datanucleus.api.jpa.NucleusJPAHelper.getJPAExceptionForNucleusException(NucleusJPAHelper.java:414)
at org.datanucleus.api.jpa.JPAQuery.getResultList(JPAQuery.java:203)
at Main.main(Main.java:73)
Caused by: org.datanucleus.exceptions.NucleusUserException: Unable to find the object with a null id!
at org.datanucleus.ExecutionContextImpl.findObject(ExecutionContextImpl.java:3441)
at org.datanucleus.store.hbase.fieldmanager.FetchFieldManager.fetchObjectField(FetchFieldManager.java:287)
at org.datanucleus.state.JDOStateManager.replacingObjectField(JDOStateManager.java:2178)
at Variable.jdoReplaceField(Variable.java)
at Variable.jdoReplaceFields(Variable.java)
at org.datanucleus.state.JDOStateManager.replaceFields(JDOStateManager.java:3415)
at org.datanucleus.state.JDOStateManager.replaceFields(JDOStateManager.java:3442)
at org.datanucleus.store.hbase.query.HBaseQueryUtils$2.fetchFields(HBaseQueryUtils.java:270)
at org.datanucleus.state.JDOStateManager.loadFieldValues(JDOStateManager.java:2547)
at org.datanucleus.state.JDOStateManager.initialiseForHollow(JDOStateManager.java:298)
at org.datanucleus.state.ObjectProviderFactoryImpl.newForHollow(ObjectProviderFactoryImpl.java:89)
at org.datanucleus.ExecutionContextImpl.newObjectProviderForHollowPopulated(ExecutionContextImpl.java:1237)
at org.datanucleus.ExecutionContextImpl.findObject(ExecutionContextImpl.java:3053)
at org.datanucleus.store.hbase.query.HBaseQueryUtils.getObjectUsingApplicationIdForResult(HBaseQueryUtils.java:265)
at org.datanucleus.store.hbase.query.HBaseQueryUtils.getObjectsOfType(HBaseQueryUtils.java:194)
at org.datanucleus.store.hbase.query.HBaseQueryUtils.getObjectsOfCandidateType(HBaseQueryUtils.java:93)
at org.datanucleus.store.hbase.query.JPQLQuery.performExecute(JPQLQuery.java:327)
at org.datanucleus.store.query.Query.executeQuery(Query.java:1786)
at org.datanucleus.store.query.Query.executeWithMap(Query.java:1690)
at org.datanucleus.api.jpa.JPAQuery.getResultList(JPAQuery.java:186)
... 1 more
This is because of the ElementCollection, as when I remove this it retreives the variable class perfectly from HBase. I also tried Kundera 2.4 (same example code), which is a lot easier to setup than this DataNucleus 'enhance' thing, but it throws something with a Stream exception. Both sites show code samples similar to my code above, so I'm very curious to what I'm doing wrong here.
Relevant pom.xml:
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jpa_2.0_spec</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.jdo</groupId>
<artifactId>jdo-api</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-api-jpa</artifactId>
<version>3.2.0-release</version>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-hbase</artifactId>
<version>3.2.0-release</version>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-enhancer</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.1.2</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</exclusion>
<exclusion>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase</artifactId>
<version>0.94.5</version>
</dependency>
<plugin>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-maven-plugin</artifactId>
<version>3.2.0-release</version>
<configuration>
<api>JPA</api>
<persistenceUnitName>hbase-pu</persistenceUnitName>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
persistance.xml for DataNucleus:
<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">
<!-- JPA tutorial "unit" -->
<persistence-unit name="hbase-pu" transaction-type="RESOURCE_LOCAL">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<class>Variable</class>
<class>DataValue</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="datanucleus.storeManagerType" value="hbase" />
<property name="datanucleus.ConnectionURL" value="hbase:localhost:2281"/>
<property name="datanucleus.ConnectionUserName" value=""/>
<property name="datanucleus.ConnectionPassword" value=""/>
<property name="datanucleus.autoCreateSchema" value="true"/>
<property name="datanucleus.autoCreateTables" value="true" />
<property name="datanucleus.autoCreateColumns" value="true" />
<property name="datanucleus.validateTables" value="true"/>
<property name="datanucleus.validateConstraints" value="false"/>
<property name="datanucleus.Optimistic" value="false"/>
<property name="datanucleus.Multithreaded" value="true" />
</properties>
</persistence-unit>
</persistence>
persistence.xml for Kundera:
<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
https://raw.github.com/impetus-opensource/Kundera/Kundera-2.0.4/kundera-core/src/test/resources/META-INF/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="hbase-pu">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<properties>
<property name="kundera.nodes" value="localhost" />
<property name="kundera.port" value="2182" />
<property name="kundera.keyspace" value="keyspace" />
<property name="kundera.dialect" value="hbase" />
<property name="kundera.client.lookup.class" value="com.impetus.client.hbase.HBaseClientFactory" />
<property name="kundera.cache.provider.class" value="com.impetus.kundera.cache.ehcache.EhCacheProvider" />
<property name="kundera.cache.config.resource" value="/ehcache-test.xml" />
<property name="kundera.ddl.auto.prepare" value="update" />
</properties>
</persistence-unit>
</persistence>
I'm kinda shooting blanks here with this whole thing and I have a feeling I pretty much tried everything at this moment and have no idea how to continue.
-- See comment below.
New Variable class:
#Entity
#Table(name = "variable", schema = "keyspace#hbase-pu")
public class Variable {
#Id
private String Id;
#Column(name = "Name")
private String Name;
#ElementCollection(fetch = FetchType.EAGER)
private List<Object> DataValues;
#Column(name = "Value")
#Lob
private Object Value;
// Getters and setters omitted.
}
So you want to have an embedded collection ? But then as this page shows clearly enough, this is not supported for HBase at the moment (supported for MongoDB, but then that has a much more flexible datastore structure to allow it). You can have a Collection of non-Entities (persisted into the same HTable as the owner), and you can have a Collection of (non-embedded) Entities, but not an embedded Collection. Obviously you could contribute your time to provide support for that should it be important to you (and I'll even provide you with starting points in the code to look at).
No reason why "CollectionTable" should make a difference ... it's an annotation for RDBMS for the schema (but then JPA is designed solely for RDBMS).
PS 1, this "enhance thing" is as simple as adding the "plugin" block to pom.xml. So unless you're prepared to express to the DataNucleus project what your difficulty with it is, then how can they know it.
PS 2, if reporting a problem I'd expect you to show the exception + stack trace no matter what software is involved, because from an exception message you tell people very little.

Unable to get JPA working with Hibernate

For a weekend project I was trying to run JPA 2 with Hibernate 3.5. Please note that I am not getting any compile errors or runtime exceptions (when I deploy the war on Tomcat). Below is my code -
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="postage" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"></property>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postage"></property>
<property name="hibernate.connection.username" value="postage"></property>
<property name="hibernate.connection.password" value="postage"></property>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"></property>
<property name="hibernate.hbm2ddl.auto" value="create-drop"></property>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"></property>
</properties>
</persistence-unit>
</persistence>
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="current_session_context_class">thread</property>
<!-- mapping files -->
<mapping class="net.rocky.postage.domain.Post"/>
<mapping class="net.rocky.postage.domain.User"/>
</session-factory>
</hibernate-configuration>
I have 2 simple Entity classes -
#Entity
#Table(name="Post")
public class Post implements Serializable{
#Id
#GeneratedValue
private long id;
private String description;
private String comments;
#OneToOne
private User postedBy;
And
#Entity
#Table(name="PostageUser")
public class User implements Serializable{
#Id
#GeneratedValue
private long id;
private String username;
private String password;
Am I missing something here. I have spent my whole saturday trying all combinations. Please help me.
Follow-up 1: By not working I mean - When I deploy the app on Tomcat, I do not see 2 tables created in Postgres (as I have given create-drop in hbm2ddl).
Follow-up 2: Thanks for your response. I cannot even get Hibernate to log messages. Here is my config:
log4j.appender.S=org.apache.log4j.ConsoleAppender
log4j.appender.S.layout=org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
log4j.rootCategory=DEBUG, S
Also, in maven I have added -
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
still no log messages from hibernate
I couldn't spot any obvious mistake in the mappings or configuration (although you should typically use a datasource when running in container but your configuration can work). But here are some suggestions or ideas:
Tell us more about the structure and the packaging of your app (where is the persistence.xml?).
Enable DEBUG logging :
for the category org.hibernate.tool.hbm2ddl
and maybe even the whole org.hibernate category until you get it working
Run a JUnit test (out-container), it will make debugging easier.
Follow-up: I don't see any logger for Hibernate defined in your logging configuration. You need to add the following for the category org.hibernate.tool.hbm2ddl:
log4j.logger.org.hibernate.hbm2ddl=debug
I'd also like to know where your persistence.xml is located in your Maven project.
References
3.5. Logging
I finally got this working .. i changed my dependencies to -
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.3.2.GA</version>
</dependency> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version>
</dependency>
earlier it was -
org.hibernate
hibernate-core
3.5.x
Remember 3.5 doc says that it has merged EM and Annotations in core, which is why i wanted to use it.
When I configured the datasource through spring, it gave me class not found org.hibernate.ejb.HibernatePersistence (this was my JPA provider)
On further investigation i found that the JBoss maven repo (https://repository.jboss.org/nexus/content/groups/public) had the hibernate 3.5 jars but the jar is wrong (say size X MB) but when you download hibernate binaries from the site they are of size Y MB. Also, the pom of this jar does not define the correct dependencies (which you will get if you download the bundled distribution directly from Hibernate site).
For me this was kinda deal-breaker and I had to switch back to 3.3 to get this working flawlessly.
Maybe this will help someone and possibly Hibernate 3.5 on Maven will be fixed. This issue was also raised in Hibernate forums.

Do I need <class> elements in persistence.xml?

I have very simple persistance.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<class>pl.michalmech.eventractor.domain.User</class>
<class>pl.michalmech.eventractor.domain.Address</class>
<class>pl.michalmech.eventractor.domain.City</class>
<class>pl.michalmech.eventractor.domain.Country</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
and it works.
But when I remove <class> elements application doesn't see entities (all classes are annotated with #Entity).
Is there any automatic mechanism to scan for #Entity classes?
The persistence.xml has a jar-file that you can use. From the Java EE 5 tutorial:
<persistence>
<persistence-unit name="OrderManagement">
<description>This unit manages orders and customers.
It does not rely on any vendor-specific features and can
therefore be deployed to any persistence provider.
</description>
<jta-data-source>jdbc/MyOrderDB</jta-data-source>
<jar-file>MyOrderApp.jar</jar-file>
<class>com.widgets.Order</class>
<class>com.widgets.Customer</class>
</persistence-unit>
</persistence>
This file defines a persistence unit
named OrderManagement, which uses a
JTA-aware data source jdbc/MyOrderDB. The jar-file and class elements specify managed persistence classes: entity classes, embeddable classes, and mapped superclasses. The jar-file element specifies JAR files that are visible to the packaged persistence unit that contain managed persistence classes, while the class element explicitly names managed persistence classes.
In the case of Hibernate, have a look at the Chapter2. Setup and configuration too for more details.
EDIT: Actually, If you don't mind not being spec compliant, Hibernate supports auto-detection even in Java SE. To do so, add the hibernate.archive.autodetection property:
<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<!-- This is required to be spec compliant, Hibernate however supports
auto-detection even in JSE.
<class>pl.michalmech.eventractor.domain.User</class>
<class>pl.michalmech.eventractor.domain.Address</class>
<class>pl.michalmech.eventractor.domain.City</class>
<class>pl.michalmech.eventractor.domain.Country</class>
-->
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
In Java SE environment, by specification you have to specify all classes as you have done:
A list of all named managed persistence classes must be specified in Java SE environments to insure portability
and
If it is not intended that the annotated persistence classes contained in the root of the persistence unit be included in the persistence unit, the exclude-unlisted-classes element should be used. The exclude-unlisted-classes element is not intended for use in Java SE environments.
(JSR-000220 6.2.1.6)
In Java EE environments, you do not have to do this as the provider scans for annotations for you.
Unofficially, you can try to set <exclude-unlisted-classes>false</exclude-unlisted-classes> in your persistence.xml. This parameter defaults to false in EE and truein SE. Both EclipseLink and Toplink supports this as far I can tell. But you should not rely on it working in SE, according to spec, as stated above.
You can TRY the following (may or may not work in SE-environments):
<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
Do I need Class elements in persistence.xml?
No, you don't necessarily. Here is how you do it in Eclipse (Kepler tested):
Right click on the project, click Properties, select JPA, in the Persistence class management tick Discover annotated classes automatically.
For those running JPA in Spring, from version 3.1 onwards, you can set packagesToScan property under LocalContainerEntityManagerFactoryBean and get rid of persistence.xml altogether.
Here's the low-down
You can provide for jar-file element path to a folder with compiled classes. For example I added something like that when I prepared persistence.xml to some integration tests:
<jar-file>file:../target/classes</jar-file>
for JPA 2+ this does the trick
<jar-file></jar-file>
scan all jars in war for annotated #Entity classes
Hibernate doesn't support <exclude-unlisted-classes>false</exclude-unlisted-classes> under SE, (another poster mentioned this works with TopLink and EclipseLink).
There are tools that will auto-generate the list of classes to persistence.xml e.g. the Import Database Schema wizard in IntelliJ. Once you've got your project's initial classes in persistence.xml it should be simple to add/remove single classes by hand as your project progresses.
Not sure if you're doing something similar to what I am doing, but Im generating a load of source java from an XSD using JAXB in a seperate component using Maven. Lets say this artifact is called "base-model"
I wanted to import this artifact containing the java source and run hibernate over all classes in my "base-model" artifact jar and not specify each explicitly. Im adding "base-model" as a dependency for my hibernate component but the trouble is the tag in persistence.xml only allows you to specify absolute paths.
The way I got round it is to copy my "base-model" jar dependency explictly to my target dir and also strip the version of it. So whereas if I build my "base-model" artifact it generate "base-model-1.0-SNAPSHOT.jar", the copy-resources step copies it as "base-model.jar".
So in your pom for the hibernate component:
<!-- We want to copy across all our artifacts containing java code
generated from our scheams. We copy them across and strip the version
so that our persistence.xml can reference them directly in the tag
<jar-file>target/dependency/${artifactId}.jar</jar-file> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>process-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
</execution>
</executions>
<configuration>
<includeArtifactIds>base-model</includeArtifactIds>
<stripVersion>true</stripVersion>
</configuration>
</plugin>
Then I call the hibernate plugin in the next phase "process-classes":
<!-- Generate the schema DDL -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>generate-ddl</id>
<phase>process-classes</phase>
<goals>
<goal>hbm2ddl</goal>
</goals>
</execution>
</executions>
<configuration>
<components>
<component>
<name>hbm2java</name>
<implementation>annotationconfiguration</implementation>
<outputDirectory>/src/main/java</outputDirectory>
</component>
</components>
<componentProperties>
<persistenceunit>mysql</persistenceunit>
<implementation>jpaconfiguration</implementation>
<create>true</create>
<export>false</export>
<drop>true</drop>
<outputfilename>mysql-schema.sql</outputfilename>
</componentProperties>
</configuration>
</plugin>
and finally in my persistence.xml I can explicitly set the location of the jar thus:
<jar-file>target/dependency/base-model.jar</jar-file>
and add the property:
<property name="hibernate.archive.autodetection" value="class, hbm"/>
It's not a solution but a hint for those using Spring:
I tried to use org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean with setting persistenceXmlLocation but with this I had to provide the <class> elements (even if the persistenceXmlLocation just pointed to META-INF/persistence.xml).
When not using persistenceXmlLocation I could omit these <class> elements.
I'm not sure this solution is under the spec but I think I can share for others.
dependency tree
my-entities.jar
Contains entity classes only. No META-INF/persistence.xml.
my-services.jar
Depends on my-entities. Contains EJBs only.
my-resources.jar
Depends on my-services. Contains resource classes and META-INF/persistence.xml.
problems
How can we specify <jar-file/> element in my-resources as the version-postfixed artifact name of a transient dependency?
How can we sync the <jar-file/> element's value and the actual transient dependency's one?
solution
direct (redundant?) dependency and resource filtering
I put a property and a dependency in my-resources/pom.xml.
<properties>
<my-entities.version>x.y.z-SNAPSHOT</my-entities.version>
</properties>
<dependencies>
<dependency>
<!-- this is actually a transitive dependency -->
<groupId>...</groupId>
<artifactId>my-entities</artifactId>
<version>${my-entities.version}</version>
<scope>compile</scope> <!-- other values won't work -->
</dependency>
<dependency>
<groupId>...</groupId>
<artifactId>my-services</artifactId>
<version>some.very.sepecific</version>
<scope>compile</scope>
</dependency>
<dependencies>
Now get the persistence.xml ready for being filtered
<?xml version="1.0" encoding="UTF-8"?>
<persistence ...>
<persistence-unit name="myPU" transaction-type="JTA">
...
<jar-file>lib/my-entities-${my-entities.version}.jar</jar-file>
...
</persistence-unit>
</persistence>
Maven Enforcer Plugin
With the dependencyConvergence rule, we can assure that the my-entities' version is same in both direct and transitive.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce</id>
<configuration>
<rules>
<dependencyConvergence/>
</rules>
</configuration>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
</plugin>
Not necessarily in all cases.
I m using Jboss 7.0.8 and Eclipselink 2.7.0. In my case to load entities without adding the same in persistence.xml, I added the following system property in Jboss Standalone XML:
<property name="eclipselink.archive.factory" value="org.jipijapa.eclipselink.JBossArchiveFactoryImpl"/>

Categories