Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Using a Factory to create entity object instances
Using a Factory to create entity object instances [message #508064] Fri, 15 January 2010 17:55 Go to next message
Andre Kullmann is currently offline Andre Kullmann
Messages: 33
Registered: September 2009
Member
Hi,

by default eclipselink use the default constructor to create new instances of entity objects. All my entity class sources will be generated, so I can't place business code there. My idea is to create
business object classes extends the entity object and place the business code there. I try to use the ClassDescriptor.useFactoryInstantiationPolicy method to tell eclipselink not to use the default constructor. Without success Sad

        EntityManager em = emf.createEntityManager();

        ClassDescriptor desc = ((EntityManagerImpl) em).getServerSession().getClassDescriptor(Kunde.class);
        desc.useFactoryInstantiationPolicy( KundeFactory.class,  "create2");

        em.getTransaction().begin();


This Nullpointer is thrown Sad

Exception in thread "main" java.lang.NullPointerException
at org.eclipse.persistence.internal.descriptors.InstantiationPo licy.buildNewInstanceUsingFactory(InstantiationPolicy.java:1 76)
at org.eclipse.persistence.internal.descriptors.InstantiationPo licy.buildNewInstance(InstantiationPolicy.java:105)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b uildNewInstance(ObjectBuilder.java:428)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b uildObject(ObjectBuilder.java:649)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b uildWorkingCopyCloneNormally(ObjectBuilder.java:580)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b uildObjectInUnitOfWork(ObjectBuilder.java:549)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b uildObject(ObjectBuilder.java:489)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b uildObject(ObjectBuilder.java:441)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildOb ject(ObjectLevelReadQuery.java:635)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.conform IndividualResult(ObjectLevelReadQuery.java:799)
at org.eclipse.persistence.queries.ReadObjectQuery.conformResul t(ReadObjectQuery.java:346)


Does anybody known how to use the useFactoryInstantiationPolicy method ?

Thanx,
André
Re: Using a Factory to create entity object instances [message #508134 is a reply to message #508064] Sat, 16 January 2010 12:59 Go to previous messageGo to next message
Andre Kullmann is currently offline Andre Kullmann
Messages: 33
Registered: September 2009
Member
Currently my workaround looks like

  
private static synchronized  Map<Class<?>,Class<?>> loadDomainObjectMap( Session session ) {

        if( domainObjectMap != null )
            return domainObjectMap;

        Map<Class<?>,Class<?>> map = new HashMap<Class<?>, Class<?>>();

        for( Object e : session.getDescriptors().keySet() ) {
            Class<?> c = (Class<?>) e;

            String name = domainObjectPackage + c.getSimpleName() + "BO";

            try {
                Class<?> d = Tx.class.getClassLoader().loadClass(name);
                map.put(c, d);
            } catch( ClassNotFoundException ex ) {
            }

        }

        return domainObjectMap = map;
    }

    private static Map<Class<?>,Class<?>> getDomainObjectMap( Session session ) {
        if( domainObjectMap != null )
            return domainObjectMap;

        return loadDomainObjectMap(session);
    }

        
EntityManager em = emf.createEntityManager();
        
Session session = ((EntityManagerImpl) em).getActiveSession();
        
for( Map.Entry< Class<?>, Class<?> > e : getDomainObjectMap(session).entrySet() ) {

  ClassDescriptor desc = session.getClassDescriptor( e.getKey() );
  desc.setInstantiationPolicy( new InstantiationPolicy() );
  desc.setJavaClass( e.getValue() );

  session.getDescriptors().put( e.getValue(), desc );
}

Re: Using a Factory to create entity object instances [message #508140 is a reply to message #508064] Sat, 16 January 2010 14:08 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom Eugelink
Messages: 807
Registered: July 2009
Senior Member
I have something similar:

custom-entity extends generated-entity extends generic-bean

The generated entities are reversed engineered from the DB, they implement @MappedSuperclass.
The custom entities (they initially contain only the class name and @Entity) are only created once and never changed by the reverse engineering logic.

If you do everything around the custom entity classes, you can use regular EMF and EM's

Tom


On 2010-01-15 18:55, Andre Kullmann wrote:
> Hi,
>
> by default eclipselink use the default constructor to create new
> instances of entity objects. All my entity class sources will be
> generated, so I can't place business code there. My idea is to create
> business object classes extends the entity object and place the business
> code there. I try to use the
> ClassDescriptor.useFactoryInstantiationPolicy method to tell eclipselink
> not to use the default constructor. Without success :(
>
>
> EntityManager em = emf.createEntityManager();
>
> ClassDescriptor desc = ((EntityManagerImpl)
> em).getServerSession().getClassDescriptor(Kunde.class);
> desc.useFactoryInstantiationPolicy( KundeFactory.class, "create2");
>
> em.getTransaction().begin();
>
>
> This Nullpointer is thrown :(
>
> Exception in thread "main" java.lang.NullPointerException
> at org.eclipse.persistence.internal.descriptors.InstantiationPo
> licy.buildNewInstanceUsingFactory(InstantiationPolicy.java:1 76)
> at org.eclipse.persistence.internal.descriptors.InstantiationPo
> licy.buildNewInstance(InstantiationPolicy.java:105)
> at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b
> uildNewInstance(ObjectBuilder.java:428)
> at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b
> uildObject(ObjectBuilder.java:649)
> at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b
> uildWorkingCopyCloneNormally(ObjectBuilder.java:580)
> at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b
> uildObjectInUnitOfWork(ObjectBuilder.java:549)
> at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b
> uildObject(ObjectBuilder.java:489)
> at org.eclipse.persistence.internal.descriptors.ObjectBuilder.b
> uildObject(ObjectBuilder.java:441)
> at org.eclipse.persistence.queries.ObjectLevelReadQuery.buildOb
> ject(ObjectLevelReadQuery.java:635)
> at org.eclipse.persistence.queries.ObjectLevelReadQuery.conform
> IndividualResult(ObjectLevelReadQuery.java:799)
> at org.eclipse.persistence.queries.ReadObjectQuery.conformResul
> t(ReadObjectQuery.java:346)
>
> Does anybody known how to use the useFactoryInstantiationPolicy method ?
>
> Thanx,
> André
Re: Using a Factory to create entity object instances [message #508360 is a reply to message #508064] Mon, 18 January 2010 14:50 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

You cannot configure descriptors after your session is already connected as they will not be initialized. You need to perform this configuration before login(), either using a DescriptorCustomizer (@Customizer) or a SessionCustomizer.



James : Wiki : Book : Blog : Twitter
Previous Topic:javax.persistence.cache.retrieveMode does not work with Query
Next Topic:Schema name from orm.xml
Goto Forum:
  


Current Time: Wed Oct 22 03:21:31 GMT 2014

Powered by FUDForum. Page generated in 0.01923 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software