Hi Emmanuel,
The modifications you requested are implemented and tested successfully,
I have tested an anotation like this:
@NamedQuery(name="findTargetByName", queryString="...", cacheable=true, cacheRegion="core:model")
and hibernate is working as expected (obtaining the cached results from the correspondent region).
There are three properties (obtained from the hint definition), "comment", "cacheModeType" and "readOnly" that cannot be integrated with the core, because the NamedQueryDefinition doesn't allow them, and the only way now would be to do a "setHint" on a query (runtime, not design time). I appreciate that these properties are interesting enough to be included also under annotations (but these require modifications in the NamedQueryDefinition).
Unfortunately, due to corporative firewall restrictions, I cannot access to the CVS to obtain the latest HEAD, so I cannot make the patch for you. I will paste the code and the modifications needed. Sorry for the inconvenience.
Please give me a reply to this message as an ACK.
Do you see any factible solution for these three attributes cited above (the ones related with the hints)?
TIA,
Kind Regards,
Carlos González-Cadenas
-------------------------------------------------------------------
package org.hibernate.annotations;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* Extends {@link javax.persistence.NamedQueries} to hold hibernate NamedQuery
* objects
* @author Emmanuel Bernard
* @author Carlos González-Cadenas
*/
@Target(TYPE) @Retention(RUNTIME)
public @interface NamedQueries {
NamedQuery[] value();
}
----------------------------------------------------
package org.hibernate.annotations;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* Extends {@link javax.persistence.NamedQuery} with Hibernate features
* @author Emmanuel Bernard
* @author Carlos González-Cadenas
*/
@Target(TYPE) @Retention(RUNTIME)
public @interface NamedQuery {
/** the name of the NamedQuery */
String name();
/** the Query String for the NamedQuery */
String queryString();
/** the flush mode for the query */
FlushModeType flushMode() default FlushModeType.AUTO;
/** mark the query as cacheable or not */
boolean cacheable() default false;
/** the cache region to use */
String cacheRegion() default "";
/**the number of rows fetched by the JDBC Driver per roundtrip*/
int fetchSize() default 50;
/**the query timeout in seconds*/
int timeout() default 10;
//TODO: For the moment parameters associated to hints (see below) cannot be
//bound to the NamedQueryDefinition using the QueryBinder
/**comment added to the SQL query, useful for the DBA */
//String comment() default "";
/**the cache mode used for this query*/
//CacheModeType cacheMode() default CacheModeType.NORMAL;
/**marks whether the results are fetched in read-only mode or not*/
//boolean readOnly() default false;
}
----------------------------------------------------------------------
package org.hibernate.annotations;
/**
* Enumeration extending javax.persistence flush modes.
* @author Emmanuel Bernard
* @author Carlos González-Cadenas
*/
public enum FlushModeType {
ALWAYS,
AUTO,
COMMIT,
NEVER,
}
-------------------------------------------------------------------------
package org.hibernate.annotations;
/**
* Enumeration for the different interaction modes between the session and
* the Level 2 Cache.
* @author Emmanuel Bernard
* @author Carlos González-Cadenas
*/
public enum CacheModeType {
GET,
IGNORE,
NORMAL,
PUT,
REFRESH
}
--------------------------------------------------------------------------
Modifications for AnnotationBinder: replace method bindQueries by the one pasted below
private static void bindQueries(AnnotatedElement annotatedElement, ExtendedMappings mappings) {
{
SqlResultSetMapping ann = annotatedElement.getAnnotation(SqlResultSetMapping.class);
QueryBinder.bindSqlResultsetMapping(ann, mappings);
}
{
NamedQuery ann = annotatedElement.getAnnotation(NamedQuery.class);
QueryBinder.bindQuery(ann, mappings);
}
{
org.hibernate.annotations.NamedQuery ann = annotatedElement.getAnnotation(org.hibernate.annotations.NamedQuery.class);
QueryBinder.bindQuery(ann, mappings);
}
{
NamedQueries ann = annotatedElement.getAnnotation(NamedQueries.class);
QueryBinder.bindQueries(ann, mappings);
}
{
org.hibernate.annotations.NamedQueries ann = annotatedElement.getAnnotation(org.hibernate.annotations.NamedQueries.class);
QueryBinder.bindQueries(ann, mappings);
}
{
NamedNativeQuery ann = annotatedElement.getAnnotation(NamedNativeQuery.class);
QueryBinder.bindNativeQuery(ann, mappings);
}
{
NamedNativeQueries ann = annotatedElement.getAnnotation(NamedNativeQueries.class);
QueryBinder.bindNativeQueries(ann, mappings);
}
}
-------------------------------------------------------------
Modifications for QueryBinder: Add these two methods
public static void bindQuery(org.hibernate.annotations.NamedQuery queryAnn, ExtendedMappings mappings){
if (queryAnn == null) return;
if ( AnnotationBinder.isDefault( queryAnn.name() ) ) {
throw new AnnotationException("A named query must have a name when used in class or package level");
}
FlushMode flushMode;
if (queryAnn.flushMode() == FlushModeType.ALWAYS) flushMode = FlushMode.ALWAYS;
else if (queryAnn.flushMode() == FlushModeType.AUTO) flushMode = FlushMode.AUTO;
else if (queryAnn.flushMode() == FlushModeType.COMMIT) flushMode = FlushMode.COMMIT;
else if (queryAnn.flushMode() == FlushModeType.NEVER) flushMode = FlushMode.NEVER;
else throw new AnnotationException("Unknown flushModeType: "+queryAnn.flushMode());
NamedQueryDefinition query = new NamedQueryDefinition(
queryAnn.queryString(),
queryAnn.cacheable(),
queryAnn.cacheRegion(),
queryAnn.timeout(),
queryAnn.fetchSize(),
flushMode,
null);
mappings.addQuery(queryAnn.name(), query);
log.debug( "Named query " + queryAnn.name() + " => " + queryAnn.queryString() );
}
public static void bindQueries(org.hibernate.annotations.NamedQueries queriesAnn, ExtendedMappings mappings) {
if (queriesAnn == null) return;
for ( org.hibernate.annotations.NamedQuery q : queriesAnn.value() ) {
bindQuery(q, mappings);
}
}
|