Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Create CriteriaQuery against DynamicEntity
Create CriteriaQuery against DynamicEntity [message #1716776] Tue, 08 December 2015 07:38 Go to next message
Eugene Logunov is currently offline Eugene LogunovFriend
Messages: 1
Registered: December 2015
Junior Member
EclipseLink version 2.6.0.1
I try to create CreteriaQuery against DynamicEntity but faced with exception
java.lang.IllegalArgumentException: No [EntityType] was found for the key class [MyDynamicEntityClass] in the Metamodel


Here is how I create Query:
DynamicHelper helper = new DynamicHelper(JpaHelper.getEntityManager(entityManager).getServerSession());
Class<? extends DynamicEntity> clazz = helper.getType("MyDynamicEntityClass").getJavaClass();
CriteriaQuery query = criteriaBuilder.createQuery(clazz);
Root<? extends DynamicEntity> root = query.from(clazz);


As I figured out CriteriaQuery try to find EntityType in the Metamodel.
I looked into the source code of new dynamic entity class creation and I didn't find where DynamicEnitity is added into Metamodel.

Here is how I create the DynamicEntity class:


Session session = JpaHelper.getEntityManager(entityManager).getServerSession();
        DynamicClassLoader dynamicClassLoader = DynamicClassLoader.lookup(session);
        Class<?> dynamicEntityClass =
                dynamicClassLoader.createDynamicClass(dynamicClassName);
        JPADynamicTypeBuilder newType = new JPADynamicTypeBuilder(dynamicEntityClass, null, "dynamic_table_name");
        newType.setPrimaryKeyFields("id");
        newType.addDirectMapping("id", String.class, "id");
        
        newType.addDirectMapping("objectId", String.class, "object_id");
        newType.addDirectMapping("description", String.class, "desc");
          jpaDynamicHelper.addTypes(false, false, newType.getType());


Is it possble to create CriteriaQuery against DynamicEntity?
Re: Create CriteriaQuery against DynamicEntity [message #1734043 is a reply to message #1716776] Fri, 03 June 2016 10:14 Go to previous messageGo to next message
Sebastien Libert is currently offline Sebastien LibertFriend
Messages: 5
Registered: June 2016
Junior Member
Hello all

Same need and same behavior here.
Did you have some success with this piece of code ?

Can somebody validate that DynamicEntity can work together with CriteriaQuery ?

I also found this :
stackoverflow.com/questions/32369182/eclipselink-jpa-dynamic-model-and-criteria-query
But working with latest version give same behavior.


Regards
Sebastien
Re: Create CriteriaQuery against DynamicEntity [message #1734068 is a reply to message #1716776] Fri, 03 June 2016 13:26 Go to previous messageGo to next message
Sebastien Libert is currently offline Sebastien LibertFriend
Messages: 5
Registered: June 2016
Junior Member
Hi all,

Can't figure out why this is not working. I found multiple samples on the net, but it's still not working.

JPL queries are working nicely but can't achieve anything with Criteria Api.
Digging in the code, I found that MetamodelImpl is not storing DynamicEntity list. So it's not suppose to work... or I'm completely misunderstanding... ?

The code throwing the "No Entity found" :

    /**
     *  Return the metamodel entity type representing the entity.
     *  @param clazz  the type of the represented entity
     *  @return the metamodel entity type
     *  @throws IllegalArgumentException if not an entity
     */
    public <X> EntityType<X> entity(Class<X> clazz) {
        String key = clazz == null ? null : clazz.getName();
        Object aType = this.entities.get(key);
        if(!(aType instanceof EntityType)) { // will only be true for null or non-metamodel types - as the map only stores EntityType values 
        	entityEmbeddableManagedTypeNotFound(entities, aType, clazz, "Entity", "EntityType");
        }
        return (EntityType<X>) aType;
    }



Regards
Re: Create CriteriaQuery against DynamicEntity [message #1734680 is a reply to message #1734068] Fri, 10 June 2016 09:28 Go to previous messageGo to next message
Sebastien Libert is currently offline Sebastien LibertFriend
Messages: 5
Registered: June 2016
Junior Member


Hello all,

Is really nobody using this kind of solution ?

Any clue ?


Regards
icon7.gif  [SOLVED] Re: Create CriteriaQuery against DynamicEntity [message #1735264 is a reply to message #1734680] Thu, 16 June 2016 16:43 Go to previous message
Sebastien Libert is currently offline Sebastien LibertFriend
Messages: 5
Registered: June 2016
Junior Member
I finally found what was missing, thanks to http://stackoverflow.com/questions/36003800/jpa-eclipselink-how-to-refresh-metadata-of-dynamic-entities-created

So you need to manually refresh metamodel after addType()

	Metamodel metamodel = new MetamodelImpl((AbstractSession) helper.getSession());
	((EntityManagerFactoryDelegate) currentEm.getEntityManagerFactory()).setMetamodel(metamodel);


Should be included in the wiki Wink
https://wiki.eclipse.org/EclipseLink/Examples/JPA/Dynamic

Previous Topic:eclipselink batch write is disabled when use history policy or DescriptorEventAdapter
Next Topic:EL-2.5.2 - Creating a single table takes 15 seconds.
Goto Forum:
  


Current Time: Mon Mar 30 14:45:47 GMT 2020

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

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

Back to the top