Hi,
using OpenJPA in a JavaSE (Eclipse RCP) environment, I would like to
register JPA-annotated classes that will participate in the
persistence mechanism at runtime.
Background:
Presistence-aware classes are contributed via Eclipse Extension
Points. All these classes have JPA annotations.
Furthermore I don't want to use class enhancement as I don't have
control over the VM startup parameters (like -javaagent) in which
the application is running in.
Though there seems to be an effort to make class enhancement
optional ([1]), I couldn't find further information on how to
disable class enhancement.
I played around with the attached code (persist one instance of
class Person that has no further dependencies/references) but I
couldn't get it to work.
The various attempts resulted either in
Attempt to cast instance "...Person..." to
PersistenceCapable failed. Ensure that it
has been enhanced.
or
The type "class org.example.openjpatest.Person"
has not been enhanced.
depending on wether I called MetaDataRepository#addMetaData() or
MetaDataRepository#addPersistenceAware() (no idea what the
difference is) and wether I called mernge() before persist().
If desired I can send the full (Eclipse) project with all required
jars (HSQL, OpenJPA plus dependencies, JTA) that runs out-of-the-box.
Thanks in advance
Rüdiger
[1] https://issues.apache.org/jira/browse/OPENJPA-293
package org.example.openjpatest;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.openjpa.meta.MetaDataRepository;
import org.apache.openjpa.persistence.EntityManagerFactoryImpl;
public class NoEnhancerExample {
public static void main( String[] args ) {
NoEnhancerExample example = new NoEnhancerExample();
EntityManagerFactory factory = example.createEntityManagerFactory();
EntityManager manager = factory.createEntityManager();
Person person = new Person();
person.setName( "first person" );
manager.merge( person );
manager.persist( person );
factory.close();
}
private EntityManagerFactory createEntityManagerFactory() {
Map<String, String> properties = new HashMap<String, String>();
properties.put( "openjpa.ConnectionUserName", "sa" );
properties.put( "openjpa.ConnectionPassword", "" );
properties.put( "openjpa.ConnectionURL",
"jdbc:hsqldb:mem:SqlModelStore_Test" );
properties.put( "openjpa.ConnectionDriverName", "org.hsqldb.jdbcDriver" );
properties.put( "openjpa.ConnectionFactoryProperties",
"PrettyPrint=true, PrettyPrintLineLength=80" );
properties.put( "openjpa.Log",
"DefaultLevel=WARN, Runtime=INFO, Tool=INFO, SQL=TRACE" );
EntityManagerFactory result
= Persistence.createEntityManagerFactory( "model", properties );
initOpenJpa( result );
return result;
}
private void initOpenJpa( EntityManagerFactory factory ) {
EntityManagerFactoryImpl factoryImpl
= ( EntityManagerFactoryImpl )factory;
MetaDataRepository metaDataRepository
= factoryImpl.getConfiguration().getMetaDataRepositoryInstance();
// metaDataRepository.addPersistenceAware( Person.class );
metaDataRepository.addMetaData( Person.class );
}
}
package org.example.openjpatest;
import java.util.UUID;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Person {
@Id
private String id;
private String name;
public Person() {
id = UUID.randomUUID().toString();
}
public String getId() {
return id;
}
public void setName( String name ) {
this.name = name;
}
public String getName() {
return name;
}
}