|
Hibernate example source code file (readonly.xml)
The Hibernate readonly.xml source code
<?xml version='1.0' encoding="UTF-8"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
~ indicated by the @author tags or express copyright attribution
~ statements applied by the authors. All third-party contributions are
~ distributed under license by Red Hat Middleware LLC.
~
~ This copyrighted material is made available to anyone wishing to use, modify,
~ copy, or redistribute it subject to the terms and conditions of the GNU
~ Lesser General Public License, as published by the Free Software Foundation.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
~ for more details.
~
~ You should have received a copy of the GNU Lesser General Public License
~ along with this distribution; if not, write to:
~ Free Software Foundation, Inc.
~ 51 Franklin Street, Fifth Floor
~ Boston, MA 02110-1301 USA
-->
<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY % BOOK_ENTITIES SYSTEM "../HIBERNATE_-_Relational_Persistence_for_Idiomatic_Java.ent">
%BOOK_ENTITIES;
]>
<chapter id="readonly">
<title>Read-only entities
<important>
<para>
Hibernate's treatment of <emphasis>read-only entities may
differ from what you may have encountered elsewhere. Incorrect usage
may cause unexpected results.
</para>
</important>
<para>
When an entity is read-only:
<itemizedlist>
<listitem>
<para>
Hibernate does not dirty-check the entity's simple
properties or single-ended associations;
</para>
</listitem>
<listitem>
<para>
Hibernate will not update simple properties or updatable
single-ended associations;
</para>
</listitem>
<listitem>
<para>
Hibernate will not update the version of the read-only
entity if only simple properties or single-ended
updatable associations are changed;
</para>
</listitem>
</itemizedlist>
</para>
<para>
In some ways, Hibernate treats read-only entities the same as entities that are
not read-only:
<itemizedlist>
<listitem>
<para>
Hibernate cascades operations to associations as
defined in the entity mapping.
</para>
</listitem>
<listitem>
<para>
Hibernate updates the version if the entity has a
collection with changes that dirties the entity;
</para>
</listitem>
<listitem>
<para>
A read-only entity can be deleted.
</para>
</listitem>
</itemizedlist>
</para>
<para>
Even if an entity is not read-only, its collection association can
be affected if it contains a read-only entity.
</para>
<para>
For details about the affect of read-only entities on different
property and association types, see
<xref linkend="readonly-proptypes"/>.
</para>
<para>
For details about how to make entities read-only, see
<xref linkend="readonly-api"/>
</para>
<para>
Hibernate does some optimizing for read-only entities:
</para>
<itemizedlist>
<listitem>
<para>
It saves execution time by not dirty-checking simple properties or
single-ended associations.
</para>
</listitem>
<listitem>
<para>
It saves memory by deleting database snapshots.
</para>
</listitem>
</itemizedlist>
<section id="readonly-api">
<title>Making persistent entities read-only
<para>
Only persistent entities can be made read-only. Transient and
detached entities must be put in persistent state before they
can be made read-only.
</para>
<para>
Hibernate provides the following ways to make persistent entities read-only:
</para>
<itemizedlist>
<listitem>
<para>
you can map an entity class as <emphasis>immutable;
when an entity of an immutable class is made persistent,
Hibernate automatically makes it read-only.
see <xref linkend="readonly-api-immutable"/> for details
</para>
</listitem>
<listitem>
<para>
you can change a default so that entities loaded
into the session by Hibernate are automatically
made read-only; see <xref linkend="readonly-api-loaddefault"/> for details
</para>
</listitem>
<listitem>
<para>
you can make an HQL query or criteria read-only so
that entities loaded when the query or criteria executes,
scrolls, or iterates, are automatically
made read-only; see <xref linkend="readonly-api-querycriteria"/> for details
</para>
</listitem>
<listitem>
<para>
you can make a persistent entity that is already in the
in the session read-only; see
<xref linkend="readonly-api-entity"/> for details
</para>
</listitem>
</itemizedlist>
<section id="readonly-api-immutable">
<title>Entities of immutable classes
<para>
When an entity instance of an immutable class is made
persistent, Hibernate automatically makes it read-only.
</para>
<para>
An entity of an immutable class can created
and deleted the same as an entity of a mutable class.
</para>
<para>
Hibernate treats a persistent entity of an immutable
class the same way as a read-only persistent entity
of a mutable class. The only exception is that
Hibernate will not allow an entity of an immutable
class to be changed so it is not read-only.
</para>
</section>
<section id="readonly-api-loaddefault">
<title>Loading persistent entities as read-only
<note>
<para>
Entities of immutable classes are automatically loaded
as read-only.
</para>
</note>
<para>
To change the default behavior so Hibernate loads entity
instances of mutable classes into the session and automatically
makes them read-only, call:
</para>
<programlisting role="Java">Session.setDefaultReadOnly( true );
<para>
To change the default back so entities loaded by Hibernate are not
made read-only, call:
</para>
<programlisting role="Java">Session.setDefaultReadOnly( false );
<para>
You can determine the current setting by calling:
</para>
<programlisting role="Java">Session.isDefaultReadOnly();
<para>
If Session.isDefaultReadOnly() returns true, entities loaded by
the following are automatically made read-only:
</para>
<itemizedlist>
<listitem>
<para>
Session.load()
</para>
</listitem>
<listitem>
<para>
Session.get()
</para>
</listitem>
<listitem>
<para>
Session.merge()
</para>
</listitem>
<listitem>
<para>
executing, scrolling, or iterating HQL queries and
criteria; to override this setting for a particular
HQL query or criteria see
<xref linkend="readonly-api-querycriteria"/>
</para>
</listitem>
</itemizedlist>
<para>
Changing this default has no effect on:
</para>
<itemizedlist>
<listitem>
<para>
persistent entities already in the session when the
default was changed
</para>
</listitem>
<listitem>
<para>
persistent entities that are refreshed via
Session.refresh(); a refreshed persistent
entity will only be read-only if it was
read-only before refreshing
</para>
</listitem>
<listitem>
<para>
persistent entities added by the application via
Session.persist(), Session.save(), and Session.update()
Session.saveOrUpdate()
</para>
</listitem>
</itemizedlist>
</section>
<section id="readonly-api-querycriteria">
<title>Loading read-only entities from an HQL query/criteria
<note>
<para>
Entities of immutable classes are automatically loaded
as read-only.
</para>
</note>
<para>
If Session.isDefaultReadOnly() returns false (the default)
when an HQL query or criteria executes, then entities
and proxies of mutable classes loaded by the query will
not be read-only.
</para>
<para>
You can override this behavior so that entities and proxies loaded
by an HQL query or criteria are automatically made read-only.
</para>
<para>
For an HQL query, call:
</para>
<programlisting role="Java">Query.setReadOnly( true );
<para>
<literal>Query.setReadOnly( true ) must be called before
<literal>Query.list(),
Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate readonly.xml source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.