HHH-4578 : Criteria is missing read-only flag

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18758 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Gail Badner 2010-02-10 07:00:59 +00:00
parent aa82fd710c
commit b39918f247
11 changed files with 1969 additions and 7 deletions

View File

@ -313,7 +313,61 @@ public interface Criteria extends CriteriaSpecification {
* @return this (for method chaining)
*/
public Criteria setFirstResult(int firstResult);
/**
* Was the read-only/modifiable mode explicitly initialized?
*
* @return true, the read-only/modifiable mode was explicitly initialized; false, otherwise.
*
* @see Criteria#setReadOnly(boolean)
*/
public boolean isReadOnlyInitialized();
/**
* Should entities and proxies loaded by this Criteria be put in read-only mode? If the
* read-only/modifiable setting was not initialized, then the default
* read-only/modifiable setting for the persistence context is returned instead.
* @see Criteria#setReadOnly(boolean)
* @see org.hibernate.engine.PersistenceContext#isDefaultReadOnly()
*
* The read-only/modifiable setting has no impact on entities/proxies returned by the
* Criteria that existed in the session before the Criteria was executed.
*
* @return true, entities and proxies loaded by the criteria will be put in read-only mode
* false, entities and proxies loaded by the criteria will be put in modifiable mode
* @throws IllegalStateException if <code>isReadOnlyInitialized()</code> returns <code>false</code>
* and this Criteria is not associated with a session.
* @see Criteria#isReadOnlyInitialized()
*/
public boolean isReadOnly();
/**
* Set the read-only/modifiable mode for entities and proxies
* loaded by this Criteria. This setting overrides the default setting
* for the persistence context.
* @see org.hibernate.engine.PersistenceContext#isDefaultReadOnly()
*
* To set the default read-only/modifiable setting used for
* entities and proxies that are loaded into the session:
* @see org.hibernate.engine.PersistenceContext#setDefaultReadOnly(boolean)
* @see org.hibernate.Session#setDefaultReadOnly(boolean)
*
* Read-only entities are not dirty-checked and snapshots of persistent
* state are not maintained. Read-only entities can be modified, but
* changes are not persisted.
*
* When a proxy is initialized, the loaded entity will have the same
* read-only/modifiable setting as the uninitialized
* proxy has, regardless of the session's current setting.
*
* The read-only/modifiable setting has no impact on entities/proxies
* returned by the criteria that existed in the session before the criteria was executed.
*
* @param readOnly true, entities and proxies loaded by the criteria will be put in read-only mode
* false, entities and proxies loaded by the criteria will be put in modifiable mode
*/
public Criteria setReadOnly(boolean readOnly);
/**
* Set a fetch size for the underlying JDBC query.
*

View File

@ -211,10 +211,9 @@ public interface Query {
* The read-only/modifiable setting has no impact on entities/proxies
* returned by the query that existed in the session before the query was executed.
*
* @return true, entities and proxies loaded by the query will be put in read-only mode
* false, entities and proxies loaded by the query will be put in modifiable mode
* @param readOnly true, entities and proxies loaded by the query will be put in read-only mode
* false, entities and proxies loaded by the query will be put in modifiable mode
*/
public Query setReadOnly(boolean readOnly);
/**

View File

@ -102,7 +102,7 @@ public final class QueryParameters {
public QueryParameters(
final Type[] positionalParameterTypes,
final Object[] postionalParameterValues) {
this( positionalParameterTypes, postionalParameterValues, null, null, false, null, null, false, null );
this( positionalParameterTypes, postionalParameterValues, null, null, false, false, false, null, null, false, null );
}
public QueryParameters(
@ -138,6 +138,8 @@ public final class QueryParameters {
final Object[] positionalParameterValues,
final LockOptions lockOptions,
final RowSelection rowSelection,
final boolean isReadOnlyInitialized,
final boolean readOnly,
final boolean cacheable,
final String cacheRegion,
//final boolean forceCacheRefresh,
@ -150,8 +152,8 @@ public final class QueryParameters {
null,
lockOptions,
rowSelection,
false,
false,
isReadOnlyInitialized,
readOnly,
cacheable,
cacheRegion,
comment,

View File

@ -81,6 +81,8 @@ public class CriteriaImpl implements Criteria, Serializable {
private FlushMode sessionFlushMode;
private CacheMode sessionCacheMode;
private Boolean readOnly;
private ResultTransformer resultTransformer = Criteria.ROOT_ENTITY;
@ -272,6 +274,36 @@ public class CriteriaImpl implements Criteria, Serializable {
return this;
}
/**
* {@inheritDoc}
*/
public boolean isReadOnlyInitialized() {
return readOnly != null;
}
/**
* {@inheritDoc}
*/
public boolean isReadOnly() {
if ( ! isReadOnlyInitialized() && getSession() == null ) {
throw new IllegalStateException(
"cannot determine readOnly/modifiable setting when it is not initialized and is not initialized and getSession() == null"
);
}
return ( isReadOnlyInitialized() ?
readOnly.booleanValue() :
getSession().getPersistenceContext().isDefaultReadOnly()
);
}
/**
* {@inheritDoc}
*/
public Criteria setReadOnly(boolean readOnly) {
this.readOnly = Boolean.valueOf( readOnly );
return this;
}
public boolean getCacheable() {
return this.cacheable;
}
@ -494,6 +526,19 @@ public class CriteriaImpl implements Criteria, Serializable {
return new Subcriteria( this, associationPath, alias, joinType, withClause );
}
public boolean isReadOnly() {
return CriteriaImpl.this.isReadOnly();
}
public boolean isReadOnlyInitialized() {
return CriteriaImpl.this.isReadOnlyInitialized();
}
public Criteria setReadOnly(boolean readOnly) {
CriteriaImpl.this.setReadOnly( readOnly );
return this;
}
public Criteria setCacheable(boolean cacheable) {
CriteriaImpl.this.setCacheable(cacheable);
return this;

View File

@ -325,6 +325,8 @@ public class CriteriaQueryTranslator implements CriteriaQuery {
valueArray,
lockOptions,
selection,
rootCriteria.isReadOnlyInitialized(),
( rootCriteria.isReadOnlyInitialized() ? rootCriteria.isReadOnly() : false ),
rootCriteria.getCacheable(),
rootCriteria.getCacheRegion(),
rootCriteria.getComment(),

View File

@ -0,0 +1,22 @@
//$Id: Course.java 5686 2005-02-12 07:27:32Z steveebersole $
package org.hibernate.test.readonly.criteria;
/**
* @author Gavin King
*/
public class Course {
private String courseCode;
private String description;
public String getCourseCode() {
return courseCode;
}
public void setCourseCode(String courseCode) {
this.courseCode = courseCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -0,0 +1,45 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.readonly.criteria">
<class name="Course">
<id name="courseCode">
<generator class="assigned"/>
</id>
<property name="description"/>
</class>
<class name="Student">
<id name="studentNumber">
<column name="studentId"/>
<generator class="assigned"/>
</id>
<property name="name" not-null="true"/>
<set name="enrolments" inverse="true" cascade="delete">
<key column="studentId"/>
<one-to-many class="Enrolment"/>
</set>
<many-to-one name="preferredCourse" column="preferredCourseCode"/>
</class>
<class name="Enrolment">
<composite-id>
<key-property name="studentNumber">
<column name="studentId"/>
</key-property>
<key-property name="courseCode"/>
</composite-id>
<many-to-one name="student" insert="false" update="false">
<column name="studentId"/>
</many-to-one>
<many-to-one name="course" insert="false" update="false">
<column name="courseCode"/>
</many-to-one>
<property name="semester" not-null="true"/>
<property name="year" not-null="true"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,63 @@
//$Id: Enrolment.java 6970 2005-05-31 20:24:41Z oneovthafew $
package org.hibernate.test.readonly.criteria;
import java.io.Serializable;
/**
* @author Gavin King
*/
public class Enrolment implements Serializable {
private Student student;
private Course course;
private long studentNumber;
private String courseCode;
private short year;
private short semester;
public String getCourseCode() {
return courseCode;
}
public void setCourseCode(String courseId) {
this.courseCode = courseId;
}
public long getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(long studentId) {
this.studentNumber = studentId;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public short getSemester() {
return semester;
}
public void setSemester(short semester) {
this.semester = semester;
}
public short getYear() {
return year;
}
public void setYear(short year) {
this.year = year;
}
public boolean equals(Object other) {
if ( !(other instanceof Enrolment) ) return false;
Enrolment that = (Enrolment) other;
return studentNumber==that.studentNumber &&
courseCode.equals(that.courseCode);
}
public int hashCode() {
return courseCode.hashCode();
}
}

View File

@ -0,0 +1,47 @@
//$Id: Student.java 9116 2006-01-23 21:21:01Z steveebersole $
package org.hibernate.test.readonly.criteria;
import java.util.HashSet;
import java.util.Set;
/**
* @author Gavin King
*/
public class Student {
private long studentNumber;
private String name;
private Course preferredCourse;
private Set enrolments = new HashSet();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getStudentNumber() {
return studentNumber;
}
public void setStudentNumber(long studentNumber) {
this.studentNumber = studentNumber;
}
public Course getPreferredCourse() {
return preferredCourse;
}
public void setPreferredCourse(Course preferredCourse) {
this.preferredCourse = preferredCourse;
}
public Set getEnrolments() {
return enrolments;
}
public void setEnrolments(Set employments) {
this.enrolments = employments;
}
}

View File

@ -0,0 +1,26 @@
/*
* Created on 28-Jan-2005
*
*/
package org.hibernate.test.readonly.criteria;
/**
* @author max
*
*/
public class StudentDTO {
private String studentName;
private String courseDescription;
public StudentDTO() { }
public String getName() {
return studentName;
}
public String getDescription() {
return courseDescription;
}
}