Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » AdditionalCriteria Annotation(AdditionalCriteria Annotation)
AdditionalCriteria Annotation [message #1383931] Tue, 27 May 2014 09:16
Dan Cebotarenco is currently offline Dan CebotarencoFriend
Messages: 2
Registered: May 2014
Junior Member
Hava a question. I have a class mapped as entity.. i use this class both on Hibernate and EclipseLink. I need to filter the query for this entity. I have 2 classes both extends specific ORM PersistenceProvider.These classes are passed as arguments to Vendors.On hibernate i inserted filtering annotation directy to persistence provider.. There is a posibillity to add "@AdditionalCriteria" in EclipseLink Persistence provider.i dont want to add it in entity or in XML.. I tried to add MetadataAnnotation to EntityManagerSetupImpl.. didn't work.. I tried to add it to ClassTransformer nothing. Here is my class:

-------
package org.eclipse.persistence.internal.jpa;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.persistence.spi.ClassTransformer;
import javax.persistence.spi.PersistenceUnitInfo;
import org.eclipse.persistence.exceptions.PersistenceUnitLoadingException;
import org.eclipse.persistence.internal.jpa.deployment.JPAInitializer;
import org.eclipse.persistence.internal.jpa.deployment.JavaSECMPInitializer;
import org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor;
import org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor;
import org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAnnotation;
import org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataClass;
import org.eclipse.persistence.jpa.PersistenceProvider;

/**
*
* @since May 22, 2014
* @author dcebotarenco
*/
public class IntherEclipsePersistence extends PersistenceProvider
{

private Collection<ClassAccessor> classAccesssorList;

@Override
public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties)
{// Record that we are inside a JEE container to allow weaving for non managed persistence units.
JavaSECMPInitializer.setIsInContainer(true);

Map nonNullProperties = (properties == null) ? new HashMap() : properties;

EntityManagerSetupImpl emSetupImpl = null;
if (EntityManagerSetupImpl.mustBeCompositeMember(info))
{
// persistence unit cannot be used standalone (only as a composite member).
// still the factory will be created but attempt to createEntityManager would cause an exception.
emSetupImpl = new EntityManagerSetupImpl(info.getPersistenceUnitName(), info.getPersistenceUnitName());
// predeploy assigns puInfo and does not do anything else.
// the session is not created, no need to add emSetupImpl to the global map.
emSetupImpl.predeploy(info, nonNullProperties);
}
else
{
boolean isNew = false;
ClassTransformer transformer = null;
String uniqueName = PersistenceUnitProcessor.buildPersistenceUnitName(info.getPersistenceUnitRootUrl(), info.getPersistenceUnitName());
String sessionName = EntityManagerSetupImpl.getOrBuildSessionName(nonNullProperties, info, uniqueName);
synchronized (EntityManagerFactoryProvider.emSetupImpls)
{
emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(sessionName);
if (emSetupImpl == null)
{
emSetupImpl = new EntityManagerSetupImpl(uniqueName, sessionName);
isNew = true;
emSetupImpl.setIsInContainerMode(true);

// if predeploy fails then emSetupImpl shouldn't be added to FactoryProvider
transformer = emSetupImpl.predeploy(info, nonNullProperties);
EntityManagerFactoryProvider.addEntityManagerSetupImpl(sessionName, emSetupImpl);
}
}

if (!isNew)
{
if (!uniqueName.equals(emSetupImpl.getPersistenceUnitUniqueName()))
{
throw PersistenceUnitLoadingException.sessionNameAlreadyInUse(sessionName, uniqueName, emSetupImpl.getPersistenceUnitUniqueName());
}
// synchronized to prevent undeploying by other threads.
boolean undeployed = false;
synchronized (emSetupImpl)
{
if (emSetupImpl.isUndeployed())
{
undeployed = true;
}
else
{
// emSetupImpl has been already predeployed, predeploy will just increment factoryCount.
transformer = emSetupImpl.predeploy(emSetupImpl.getPersistenceUnitInfo(), nonNullProperties);
}
}
if (undeployed)
{
// after the emSetupImpl has been obtained from emSetupImpls
// it has been undeployed by factory.close() in another thread - start all over again.
return createContainerEntityManagerFactory(info, properties);
}
}
if (transformer != null)
{
info.addTransformer(transformer);
}
}

EntityManagerFactoryImpl factory = null;
try
{
getAllClassAccessorList(emSetupImpl);
adjustEntities(emSetupImpl);

factory = new EntityManagerFactoryImpl(emSetupImpl, nonNullProperties);

// This code has been added to allow validation to occur without actually calling createEntityManager
if (emSetupImpl.shouldGetSessionOnCreateFactory(nonNullProperties))
{
factory.getDatabaseSession();
}
return factory;
}
catch (RuntimeException ex)
{
if (factory != null)
{
factory.close();
}
else
{
emSetupImpl.undeploy();
}
throw ex;
}
}

@Override
public EntityManagerFactory createEntityManagerFactory(String emName, Map properties)
{
Map nonNullProperties = (properties == null) ? new HashMap() : properties;

if (checkForProviderProperty(nonNullProperties))
{
String name = (emName == null) ? "" : emName;
JPAInitializer initializer = getInitializer(name, nonNullProperties);
return createEntityManagerFactoryImpl(initializer.findPersistenceUnitInfo(name, nonNullProperties), nonNullProperties, true);
}

// Not EclipseLink so return null;
return null;
}

private void getAllClassAccessorList(EntityManagerSetupImpl emSetupImpl)
{
classAccesssorList = emSetupImpl.processor.getProject().getAllAccessors();
}

private void adjustEntities(EntityManagerSetupImpl emSetupImpl)
{
MetadataAnnotation additionalCriteria = new MetadataAnnotation();
additionalCriteria.setName("org.eclipse.persistence.annotations.AdditionalCriteria");
additionalCriteria.addAttribute("value", "this.client.id='SHARED'");

for (ClassAccessor clazz : classAccesssorList)
{
String clazzJavaName = clazz.getJavaClassName();
if (clazzJavaName.contains("UserCore"))
{
System.out.println("");
MetadataClass metadataClazz = emSetupImpl.processor.getMetadataFactory().getMetadataClass(clazzJavaName);
metadataClazz.addAnnotation(additionalCriteria);
}
else
{
MetadataClass metadataClazz = emSetupImpl.processor.getMetadataFactory().getMetadataClass(clazzJavaName);
metadataClazz.addAnnotation(additionalCriteria);
}
}
}
}
Previous Topic:Stored Procedures
Next Topic:positional parameter in query
Goto Forum:
  


Current Time: Sun Aug 02 14:26:01 GMT 2015

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

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