HHH-7426 Setting cacheable='true' in orm.xml does not enable caching of entity in 2lc

This commit is contained in:
Strong Liu 2012-07-12 15:00:44 +08:00
parent 973451d5e8
commit 97ac8311d4
5 changed files with 41 additions and 4 deletions

View File

@ -62,7 +62,7 @@ public class JPAMetadataProvider implements MetadataProvider, Serializable {
delegate = new JavaMetadataProvider();
cache = new HashMap<AnnotatedElement, AnnotationReader>(100);
}
@Override
public AnnotationReader getAnnotationReader(AnnotatedElement annotatedElement) {
AnnotationReader reader = cache.get( annotatedElement );
if (reader == null) {
@ -76,7 +76,7 @@ public class JPAMetadataProvider implements MetadataProvider, Serializable {
}
return reader;
}
@Override
public Map<Object, Object> getDefaults() {
if ( defaults == null ) {
defaults = new HashMap<Object, Object>();

View File

@ -44,6 +44,7 @@ import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Basic;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
@ -225,6 +226,7 @@ public class JPAOverriddenAnnotationReader implements AnnotationReader {
annotationToXml.put( MapKeyJoinColumn.class, "map-key-join-column" );
annotationToXml.put( MapKeyJoinColumns.class, "map-key-join-column" );
annotationToXml.put( OrderColumn.class, "order-column" );
annotationToXml.put( Cacheable.class, "cacheable" );
}
private XMLContext xmlContext;
@ -343,6 +345,7 @@ public class JPAOverriddenAnnotationReader implements AnnotationReader {
addIfNotNull( annotationList, getSecondaryTables( tree, defaults ) );
addIfNotNull( annotationList, getPrimaryKeyJoinColumns( tree, defaults, true ) );
addIfNotNull( annotationList, getIdClass( tree, defaults ) );
addIfNotNull( annotationList, getCacheable( tree, defaults ) );
addIfNotNull( annotationList, getInheritance( tree, defaults ) );
addIfNotNull( annotationList, getDiscriminatorValue( tree, defaults ) );
addIfNotNull( annotationList, getDiscriminatorColumn( tree, defaults ) );
@ -833,6 +836,21 @@ public class JPAOverriddenAnnotationReader implements AnnotationReader {
return mergeAttributeOverrides( defaults, attributes, false );
}
private Cacheable getCacheable(Element element, XMLContext.Default defaults){
if(element==null)return null;
String attValue = element.attributeValue( "cacheable" );
if(attValue!=null){
AnnotationDescriptor ad = new AnnotationDescriptor( Cacheable.class );
ad.setValue( "value", Boolean.valueOf( attValue ) );
return AnnotationFactory.create( ad );
}
if ( defaults.canUseJavaAnnotations() ) {
return getJavaAnnotation( Cacheable.class );
}
else {
return null;
}
}
/**
* Adds a @MapKeyEnumerated annotation to the specified annotationList if the specified element
* contains a map-key-enumerated sub-element. This should only be the case for

View File

@ -48,7 +48,7 @@ import org.hibernate.internal.CoreMessageLogger;
*/
public class ConnectionProxyHandler
extends AbstractProxyHandler
implements InvocationHandler, NonDurableConnectionObserver {
implements NonDurableConnectionObserver {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class,
ConnectionProxyHandler.class.getName());

View File

@ -23,11 +23,18 @@
*/
package org.hibernate.ejb.test.xml;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.SharedCacheMode;
import junit.framework.Assert;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.ejb.AvailableSettings;
import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.persister.entity.EntityPersister;
/**
* @author Emmanuel Bernard
@ -39,11 +46,23 @@ public class XmlTest extends BaseEntityManagerFunctionalTestCase {
em.close();
}
@Test
public void testXmlMappingWithCacheable() throws Exception{
EntityManager em = getOrCreateEntityManager();
SessionImplementor session = em.unwrap( SessionImplementor.class );
EntityPersister entityPersister= session.getFactory().getEntityPersister( Lighter.class.getName() );
Assert.assertTrue(entityPersister.hasCache());
}
@Override
public Class[] getAnnotatedClasses() {
return new Class[0];
}
protected void addConfigOptions(Map options) {
options.put( AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.ENABLE_SELECTIVE );
}
@Override
public String[] getEjb3DD() {
return new String[] {

View File

@ -5,7 +5,7 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
version="2.0"
>
<entity class="org.hibernate.ejb.test.xml.Lighter" name="ALighter" access="FIELD" metadata-complete="true">
<entity class="org.hibernate.ejb.test.xml.Lighter" cacheable="true" name="ALighter" access="FIELD" metadata-complete="true">
<attributes>
<id name="name">
<column name="fld_id"/>