JPA: adding entities to EntityManagerFactory programmatically - Apache

This is a discussion on JPA: adding entities to EntityManagerFactory programmatically - Apache ; 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. ...

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11

JPA: adding entities to EntityManagerFactory programmatically

  1. Default JPA: adding entities to EntityManagerFactory programmatically

    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;
    }
    }


  2. Default Re: JPA: adding entities to EntityManagerFactory programmatically


    Hi,

    The configuration property name is "openjpa.RuntimeUnenhancedClasses" and
    possible values are "supported", "unsupported" and "warn". With "supported",
    OpenJPA will be able to work with normal user-defined classes i.e. without
    any byte-code enhancement.

    > MetaDataRepository#addMetaData() or
    > MetaDataRepository#addPersistenceAware() (no idea what the
    > difference is)


    PersistenceCapable refers to classes that are managed by JPA provider. To
    manage them properly, JPA provider needs to intercept any read/write access
    to fields of PersistenceCapable class. Different techniques (e.g. proxying,
    subclassing, byte-code enhancement) can be used to intercept without
    impacting user-defined classes. OpenJPA traditionally used build-time (i.e.
    post-compile, pre-runtime) byte-code enhancement as the only mechanics for
    interception. Later, more choices have been added that allows you to choose
    when byte-code enhancement takes place [1] or even using other technique
    such as subclassing rather than byte-code enhancement [2].

    PersistenceAware refers to classes that *directly* access/mutate fields of
    PersistenceCapable instances. This can happen when fields of
    PersistenceCapable classes are declared at public or package level access.
    OpenJPA requires PersistenceAware classes also to be enhanced.

    > I couldn't find further information on how to disable class enhancement.


    This is not good news for our documentation effort



    [1]
    http://openjpa.apache.org/docs/lates...ide_pc_enhance
    [2]
    http://openjpa.apache.org/docs/lates...enhanced_types



    RĂ¼diger Herrmann wrote:
    >
    > 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;
    > }
    > }
    >
    >


    --
    View this message in context: http://www.nabble.com/JPA%3A-adding-...p17514281.html
    Sent from the OpenJPA Users mailing list archive at Nabble.com.



  3. Default Re: JPA: adding entities to EntityManagerFactory programmatically

    Hi,

    thanks for your reply. I found your documentation pretty good. I
    came across the RuntimeUnenhancedClasses property but overlooked the
    UN of Unenhanced and thus didn't give it a try.

    However the adjusted example (see attachment) still gives me:
    The type "...Person" has not been enhanced.
    - the properties passed to Persistence.createEntityManagerFactory()
    now have the openjpa.RuntimeUnenhancedClasses=supported
    setting.
    - on the MetaDataRepsoitory of the EntityManagerFactory, I
    call addMetaData( Person.class ).
    what else am I missing?

    Cheers,
    RĂ¼diger

    Pinaki Poddar wrote:
    > Hi,
    >
    > The configuration property name is "openjpa.RuntimeUnenhancedClasses" and
    > possible values are "supported", "unsupported" and "warn". With "supported",
    > OpenJPA will be able to work with normal user-defined classes i.e. without
    > any byte-code enhancement.
    >
    >> MetaDataRepository#addMetaData() or
    >> MetaDataRepository#addPersistenceAware() (no idea what the
    >> difference is)

    >
    > PersistenceCapable refers to classes that are managed by JPA provider. To
    > manage them properly, JPA provider needs to intercept any read/write access
    > to fields of PersistenceCapable class. Different techniques (e.g. proxying,
    > subclassing, byte-code enhancement) can be used to intercept without
    > impacting user-defined classes. OpenJPA traditionally used build-time (i.e.
    > post-compile, pre-runtime) byte-code enhancement as the only mechanics for
    > interception. Later, more choices have been added that allows you to choose
    > when byte-code enhancement takes place [1] or even using other technique
    > such as subclassing rather than byte-code enhancement [2].
    >
    > PersistenceAware refers to classes that *directly* access/mutate fields of
    > PersistenceCapable instances. This can happen when fields of
    > PersistenceCapable classes are declared at public or package level access.
    > OpenJPA requires PersistenceAware classes also to be enhanced.
    >
    >> I couldn't find further information on how to disable class enhancement.

    >
    > This is not good news for our documentation effort
    >
    >
    >
    > [1]
    > http://openjpa.apache.org/docs/lates...ide_pc_enhance
    > [2]
    > http://openjpa.apache.org/docs/lates...enhanced_types
    >
    >
    >
    > RĂ¼diger Herrmann wrote:
    >> 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;
    >> }
    >> }
    >>
    >>

    >


    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.RuntimeUnenhancedClasses", "supported" );
    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;
    }
    }

  4. Default Re: JPA: adding entities to EntityManagerFactory programmatically


    Hi,
    Can you post the entire stack trace?


    --
    View this message in context: http://www.nabble.com/JPA%3A-adding-...p17523591.html
    Sent from the OpenJPA Users mailing list archive at Nabble.com.



  5. Default Re: JPA: adding entities to EntityManagerFactory programmatically

    If it is more convenient, I can also send the example projects,
    which consists of two classes, hsqldb.jar and the openJPA jars
    (which I can leave aside)

    Here goes the stack trace:

    Exception in thread "main" <openjpa-1.0.2-r420667:627158 fatal user
    error> org.apache.openjpa.persistence.ArgumentException: The type
    "class org.example.openjpatest.Person" has not been enhanced.
    at
    org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java:1639)
    at
    org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1613)
    at
    org.apache.openjpa.meta.MetaDataRepository.processBuffer(MetaDataRepository.java:675)
    at
    org.apache.openjpa.meta.MetaDataRepository.resolveMeta(MetaDataRepository.java:575)
    at
    org.apache.openjpa.meta.MetaDataRepository.resolve(MetaDataRepository.java:500)
    at
    org.apache.openjpa.meta.MetaDataRepository.getMetaData(MetaDataRepository.java:302)
    at
    org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:237)
    at
    org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:101)
    at org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3160)
    at
    org.apache.openjpa.kernel.DelegatingBroker.attach(DelegatingBroker.java:1142)
    at
    org.apache.openjpa.persistence.EntityManagerImpl.merge(EntityManagerImpl.java:665)
    at
    org.example.openjpatest.NoEnhancerExample.main(NoEnhancerExample.java:22)


    Pinaki Poddar wrote:
    > Hi,
    > Can you post the entire stack trace?
    >
    >



  6. Default Re: JPA: adding entities to EntityManagerFactory programmatically


    Hi,
    If the list of persistent classes are known *before* EntityManagerFactory
    is created, then a possible
    solution is as below:

    public class TestDynamic extends TestCase {
    public void testDynamic() {
    // create a semicolon-separated list of persistent class
    names
    Class[] classes = { Unenhanced.class};
    StringBuffer classNames = new StringBuffer();
    for (Class c : classes) {
    if (classNames.length() > 0)
    classNames.append(";");
    classNames.append(c.getName());
    }

    Map<String, String> properties = new HashMap<String, String>();
    properties.put("openjpa.RuntimeUnenhancedClasses", "supported");
    properties.put("openjpa.ConnectionUserName", "sa");
    properties.put("openjpa.ConnectionPassword", "");
    properties.put("openjpa.ConnectionURL", "jdbc:mysql://localhost/test");
    properties.put("openjpa.ConnectionDriverName", "com.mysql.jdbc.Driver");

    // specify list of class names as a property
    properties.put("openjpa.MetaDataFactory", "jpa(Types="+classNames+")");

    // allow table definitions on-the-fly
    properties.put("openjpa.jdbc.SynchronizeMappings",
    "buildSchema(ForeignKeys=true)");

    // now create factory
    EntityManagerFactory factory =
    Persistence.createEntityManagerFactory("test", properties);

    // create a EM to trigger dynamic class management
    EntityManager em = factory.createEntityManager();
    em.close();

    // hence things should behave normally
    em = factory.createEntityManager();
    Unenhanced pc = new Unenhanced();
    em.getTransaction().begin();
    em.persist(pc);
    em.getTransaction().commit();
    }



    --
    View this message in context: http://www.nabble.com/JPA%3A-adding-...p17526048.html
    Sent from the OpenJPA Users mailing list archive at Nabble.com.



  7. Default Re: JPA: adding entities to EntityManagerFactory programmatically

    HURRAY, the example is working!

    But making the same changes to my PDE TestCases fails. I am pretty
    sure this is because of different class loaders.

    Is it possible to also specify a distinct class loader for each
    entity class?

    Background:
    I am running in an OSGi environment in which the code is modularized
    into bundles. Each bundle has its own class loader.
    The EntityManager lives in bundle A that has class loader CA. The
    entity class lives in bundle B that has class loader CB. Further
    entities could live in bundle C with class loader CC. And so on...
    For now, the class loaders are already known when the the
    EntityManagerFactory properties need to be constructed.

    Is there a way to do something like this?
    Map<String, String> properties = new HashMap<String, String>();
    properties.put( "openjpa.MetaDataFactory",
    "jpa(Types=org.ex.Personrg.osgi.BundleLoader)");

    Thanks in advance
    Rüdiger

    Pinaki Poddar wrote:
    > Hi,
    > If the list of persistent classes are known *before* EntityManagerFactory
    > is created, then a possible
    > solution is as below:
    >
    > public class TestDynamic extends TestCase {
    > public void testDynamic() {
    > // create a semicolon-separated list of persistent class
    > names
    > Class[] classes = { Unenhanced.class};
    > StringBuffer classNames = new StringBuffer();
    > for (Class c : classes) {
    > if (classNames.length() > 0)
    > classNames.append(";");
    > classNames.append(c.getName());
    > }
    >
    > Map<String, String> properties = new HashMap<String, String>();
    > properties.put("openjpa.RuntimeUnenhancedClasses", "supported");
    > properties.put("openjpa.ConnectionUserName", "sa");
    > properties.put("openjpa.ConnectionPassword", "");
    > properties.put("openjpa.ConnectionURL", "jdbc:mysql://localhost/test");
    > properties.put("openjpa.ConnectionDriverName", "com.mysql.jdbc.Driver");
    >
    > // specify list of class names as a property
    > properties.put("openjpa.MetaDataFactory", "jpa(Types="+classNames+")");
    >
    > // allow table definitions on-the-fly
    > properties.put("openjpa.jdbc.SynchronizeMappings",
    > "buildSchema(ForeignKeys=true)");
    >
    > // now create factory
    > EntityManagerFactory factory =
    > Persistence.createEntityManagerFactory("test", properties);
    >
    > // create a EM to trigger dynamic class management
    > EntityManager em = factory.createEntityManager();
    > em.close();
    >
    > // hence things should behave normally
    > em = factory.createEntityManager();
    > Unenhanced pc = new Unenhanced();
    > em.getTransaction().begin();
    > em.persist(pc);
    > em.getTransaction().commit();
    > }
    >
    >
    >



  8. Default Re: JPA: adding entities to EntityManagerFactory programmatically


    Hi,
    Configuring a ClassResolver[1] with MultiLoaderClassResolver[2] may be
    your answer.

    [1]
    http://openjpa.apache.org/docs/lates....ClassResolver
    [2]
    http://openjpa.apache.org/docs/lates...sResolver.html


    --
    View this message in context: http://www.nabble.com/JPA%3A-adding-...p17536186.html
    Sent from the OpenJPA Users mailing list archive at Nabble.com.



  9. Default Re: JPA: adding entities to EntityManagerFactory programmatically

    Hi,

    again, your documentation is good, and your support as well. It's
    just me too dumb to read it. Sorry for bothering you, this I
    could've found out myself.

    But now for something completely different (hopefully not again a
    not-read-documentation-issue

    I get the stack trace below when constructing the
    EntityManagerFactory in an OSGi environment.

    My entities have the following inheritance hierarchy:

    class Entity {
    // get/set methods common to all entity-classes
    // like id and version. Methods are JP-annotated,
    // but not the class itself
    }

    // not JPA-annotated
    interface IProject {
    // some get/set methods
    }

    @Entity
    class Project implements IProject extends Entity {
    // implemented get/set methods of IProject
    // with JPA annotations
    }

    The class loaders of all these classes are passed to
    MultiClassLoader I regsistered in the openjpa.ClassResolver property.
    It seems that the custom openjpa.ClassResolver I specified is not
    asked when preparing the unenhanced classes. Could that be?

    Not sure what further information to add, I could of course send the
    test- and implementation plug-in-projects. They run out-of-the-box,
    provided that an Eclipse >= 3.3 is installed.

    -- Rüdiger


    Stack trace
    -----------
    java.lang.NoClassDefFoundError: org/rhoeta/core/plan/entity/IProject
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:465)
    at serp.bytecode.BCClassLoader.findClass(BCClassLoader.java:50)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:465)
    at serp.bytecode.BCClassLoader.findClass(BCClassLoader.java:50)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:242)
    at
    org.apache.openjpa.util.GeneratedClasses.loadBCClass(GeneratedClasses.java:67)
    at
    org.apache.openjpa.enhance.ManagedClassSubclasser.write(ManagedClassSubclasser.java:259)
    at
    org.apache.openjpa.enhance.ManagedClassSubclasser.access$000(ManagedClassSubclasser.java:53)
    at
    org.apache.openjpa.enhance.ManagedClassSubclasser$1.write(ManagedClassSubclasser.java:123)
    at org.apache.openjpa.enhance.PCEnhancer.record(PCEnhancer.java:526)
    at org.apache.openjpa.enhance.PCEnhancer.record(PCEnhancer.java:514)
    at
    org.apache.openjpa.enhance.ManagedClassSubclasser.prepareUnenhancedClasses(ManagedClassSubclasser.java:145)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at
    org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes(AbstractBrokerFactory.java:297)
    at
    org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:199)
    at
    org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:142)
    at
    org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:192)
    at
    org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:145)
    at
    org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:56)
    at
    org.rhoeta.core.internal.model.JPAModelStore.executeSql(JPAModelStore.java:95)
    at
    org.rhoeta.core.internal.model.JPAModelStore.doCreate(JPAModelStore.java:26)
    at
    org.rhoeta.core.internal.model.AbstractModelStore.create(AbstractModelStore.java:36)
    at
    org.rhoeta.core.plan.internal.jpastore.JPAModelStore_Test.testCreate(JPAModelStore_Test.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at
    org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
    at
    org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
    at
    org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at
    org.eclipse.pde.internal.junit.runtime.RemotePluginTestRunner.main(RemotePluginTestRunner.java:62)
    at
    org.eclipse.pde.internal.junit.runtime.CoreTestApplication.run(CoreTestApplication.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at
    org.eclipse.equinox.internal.app.EclipseAppContainer.callMethod(EclipseAppContainer.java:565)
    at
    org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:195)
    at
    org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at
    org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at
    org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:362)
    at
    org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:175)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:549)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:504)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1236)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1212)



    Pinaki Poddar wrote:
    > Hi,
    > Configuring a ClassResolver[1] with MultiLoaderClassResolver[2] may be
    > your answer.
    >
    > [1]
    > http://openjpa.apache.org/docs/lates....ClassResolver
    > [2]
    > http://openjpa.apache.org/docs/lates...sResolver.html
    >
    >



  10. Default Re: JPA: adding entities to EntityManagerFactory programmatically


    Hi,
    To narrow the problem, I will suggest to suppress/remove IProject to see
    if that changes anything.

    Are the following statements true for your environment?
    1. the persistent classes are not specified at <class> tag of
    persistence.xml but specified as Map before construction of EMF.
    2. The persistent classes are not enhanced at bulid-time. -javaagent is also
    not activated. OpenJPA should subclass these persistent classes to manage
    them.
    3. the persistent classes may be loaded by different classloaders

    If they are, you have an interesting requirement -- your experience will
    be useful to others.




    --
    View this message in context: http://www.nabble.com/JPA%3A-adding-...p17548472.html
    Sent from the OpenJPA Users mailing list archive at Nabble.com.



+ Reply to Thread
Page 1 of 2 1 2 LastLast