google Analytics

Thursday, July 8, 2010

Hibernate versioning Example

The <version> element is optional and indicates that the table contains versioned data. This is
particularly useful if you plan to use long transactions.

<version
column="version_column"
name="propertyName"
type="typename"
access="field|property|ClassName"
unsaved-value="null|negative|undefined"
generated="never|always"
insert="true|false"
node="element-name|@attribute-name|element/@attribute|."
/>

Version numbers can be of Hibernate type long, integer, short, timestamp or calendar.
A version or timestamp property should never be null for a detached instance. Hibernate will detect
any instance with a null version or timestamp as transient, irrespective of what other unsavedvalue
strategies are specified. Declaring a nullable version or timestamp property is an easy way
to avoid problems with transitive reattachment in Hibernate. It is especially useful for people using
assigned identifiers or composite key

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="session.Writer" table="WRITER" schema="SMC">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="increment" />
</id>
<version name="version" type="java.lang.Long">
<column name="VERSION" precision="22" scale="0" />
</version>
<property name="name" type="java.lang.String">
<column name="NAME" length="20" />
</property>
</class>
</hibernate-mapping>

=============================================================

=============================================================

public class Writer implements java.io.Serializable {

// Fields

private Long id;
private Long version;
private String name;

// Constructors

/** default constructor */
public Writer() {
}

/** full constructor */
public Writer(String name) {
this.name = name;
}

// Property accessors

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public Long getVersion() {
return this.version;
}

public void setVersion(Long version) {
this.version = version;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

}


=============================================================

=============================================================
Hibernate Automatic Versioning
How to do optimistic locking in Spring
Optimistic Locking and Versioning in Hibernate
StaleObjectState Excption

2 comments:

  1. <hibernate-mapping>
        <class name="session.Writer" table="WRITER" schema="SMC">
            <id name="id" type="java.lang.Long">
                <column name="ID" precision="22" scale="0" />
                <generator class="increment" />
            </id>
            <version name="version" type="java.lang.Long">
                <column name="VERSION" precision="22" scale="0" />
            </version>
            <property name="name" type="java.lang.String">
                <column name="NAME" length="20" />
            </property>
        </class>
    </hibernate-mapping>

    ReplyDelete
  2. create table WRITER
    (
    ID NUMBER not null,
    NAME VARCHAR2(20),
    VERSION NUMBER
    );

    alter table WRITER
    add constraint PK_ID primary key (ID)
    using index
    tablespace SYSTEM
    pctfree 10
    initrans 2
    maxtrans 255
    storage
    (
    initial 64K
    minextents 1
    maxextents unlimited
    );

    ReplyDelete