I am having a problem in that everytime a record is loaded and viewed it gets updated back to the database even if no changes (I think) were made.
Hibernate version:3.0.5
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
SYSTEM "/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.archiviststoolkit.model.Subjects" table="subjects" lazy="false">
<id name="subjectId" column="SubjectId">
<generator class="increment"/>
</id>
<version name="version" type="java.lang.Long" column="version"/>
<property name="creationdate" type="java.util.Calendar" column="creationdate"/>
<property name="modifieddate" type="java.util.Calendar" column="modifieddate"/>
<property name="createdBy" type="string" column="createdBy"/>
<property name="modifiedBy" type="string" column="modifiedBy"/>
<property name="term" column="term"/>
<property name="termType" column="termType" type="subjectTermType"/>
<property name="termSource" column="termSource"/>
<property name="scopeNote" column="scopeNote"/>
</class>
</hibernate-mapping>
The generated SQL (show_sql=true):
Hibernate: update subjects set version=?, creationdate=?, modifieddate=?, createdBy=?, modifiedBy=?, term=?, termType=?, termSource=?, scopeNote=? where SubjectId=? and version=?
Hibernate: select subjects0_.SubjectId as SubjectId0_, subjects0_.version as version0_0_, subjects0_.creationdate as creation3_0_0_, subjects0_.modifieddate as modified4_0_0_, subjects0_.createdBy as createdBy0_0_, subjects0_.modifiedBy as modifiedBy0_0_, subjects0_.term as term0_0_, subjects0_.termType as termType0_0_, subjects0_.termSource as termSource0_0_, subjects0_.scopeNote as scopeNote0_0_ from subjects subjects0_ where subjects0_.SubjectId=?
Code:
/*
* Subject.java
*
* Created on July 19, 2005, 11:54 AM
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package org.archiviststoolkit.model;
import org.archiviststoolkit.mydomain.DomainObject;
import com.jgoodies.binding.beans.ExtendedPropertyChangeSupport;
import java.beans.PropertyChangeListener;
/**
* @author leemandell
*/
public class Subjects extends DomainObject {
private Long subjectId;
private String term = "";
private SubjectTermType termType = null;
private String termSource = "";
private String scopeNote = "";
private ExtendedPropertyChangeSupport changeSupport = new ExtendedPropertyChangeSupport(this);
/**
* Creates a new instance of Subject
*/
public Subjects() {
}
/**
* @return Returns the identifier.
*/
public Long getIdentifier() {
return getSubjectId();
}
/**
* @param identifier The identifier to set.
*/
public void setIdentifier(Long identifier) {
this.setSubjectId(identifier);
}
public void addPropertyChangeListener(PropertyChangeListener x) {
changeSupport.addPropertyChangeListener(x);
}
public void removePropertyChangeListener(PropertyChangeListener x) {
changeSupport.removePropertyChangeListener(x);
}
public Long getSubjectId() {
return subjectId;
}
public void setSubjectId(Long subjectId) {
this.subjectId = subjectId;
}
public String getTerm() {
return term;
}
public void setTerm(String term) {
this.term = term;
}
public SubjectTermType getTermType() {
return termType;
}
public void setTermType(SubjectTermType termType) {
this.termType = termType;
}
public String getTermSource() {
return termSource;
}
public void setTermSource(String termSource) {
this.termSource = termSource;
}
public String getScopeNote() {
return scopeNote;
}
public void setScopeNote(String scopeNote) {
this.scopeNote = scopeNote;
}
}
[code]
package org.archiviststoolkit.mydomain;
//==============================================================================
// Import Declarations
//==============================================================================
import java.util.Calendar;
import java.util.GregorianCalendar;
import org.hibernate.classic.Lifecycle;
import org.hibernate.Session;
import org.archiviststoolkit.ApplicationFrame;
import java.io.Serializable;
import com.jgoodies.binding.beans.Model;
/**
* The DomainObject should be the base class of all data types which
* we wish to store in our hibernate based database.
* TODO: Integer should be a Long identifier within hibernate
*/
public abstract class DomainObject implements Lifecycle, Comparable {
/**
* Hibernate Version tracker.
*/
private Long version = new Long(0);
/**
* When the object was created.
*/
private Calendar creationdate = new GregorianCalendar();
/**
* When the object was last modified.
*/
private Calendar modifieddate = new GregorianCalendar();
private String createdBy = "";
private String modifiedBy = "";
/**
* @return Returns the creationdate.
*/
public final Calendar getCreationdate() {
return creationdate;
}
/**
* @param creationdate The creationdate to set.
*/
public final void setCreationdate(Calendar creationdate) {
this.creationdate = creationdate;
}
/**
* @return Returns the identifier.
*/
public abstract Long getIdentifier();
/**
* @param identifier The identifier to set.
*/
public abstract void setIdentifier(Long identifier);
/**
* @return Returns the modifieddate.
*/
public final Calendar getModifieddate() {
return modifieddate;
}
/**
* @param modifieddate The modifieddate to set.
*/
public final void setModifieddate(Calendar modifieddate) {
this.modifieddate = modifieddate;
}
/**
* @return Returns the version.
*/
public final Long getVersion() {
return version;
}
/**
* @param version The version to set.
*/
public final void setVersion(Long version) {
this.version = version;
}
/**
* Tests objects for equality.
*
* @param object the object to test against
* @return true if the object equals this object
*/
public boolean equals(final Object object) {
if (object == null) {
return (false);
}
if (this == object) {
return true;
}
if (this.getClass() == object.getClass())
{
if (this.getIdentifier() != null && ((DomainObject) object).getIdentifier() != null
&& (this.getIdentifier().equals(((DomainObject) object).getIdentifier())))
{
return true;
}
}
return false;
}
/**
* Returns the hashcode for this object.
*
* @return the hashcode
*/
public int hashCode() {
if (this.getIdentifier() == null) {
return (-1);
}
return (this.getIdentifier().intValue());
}
/**
* Called when an entity is deleted.
*
* @param session the session to delete on
* @return true to veto delete
*/
public final boolean onDelete(final Session session) {
return (false);
}
/**
* Called after an entity is loaded.
*
* @param session the session which loaded the entity
* @param serialid the identifier of the entity
*/
public void onLoad(final Session session, final Serializable serialid) {
}
/**
* Called when an entity is saved.
*
* @param session the session to save on
* @return true to veto save
*/
public final boolean onSave(final Session session) {
GregorianCalendar calendar = new GregorianCalendar();
this.setCreationdate(calendar);
this.setModifieddate(calendar);
this.setCreatedBy(ApplicationFrame.currentUser);
this.setModifiedBy(ApplicationFrame.currentUser);
return (false);
}
/**
* Called when an entity is updated.
*
* @param session the session to update on
* @return true to veto update
*/
public final boolean onUpdate(final Session session) {
GregorianCalendar calendar = new GregorianCalendar();
this.setModifieddate(calendar);
this.setModifiedBy(ApplicationFrame.currentUser);
return (false);
}
/**
* Compares this object to another.
*
* @param object the object to compare this to.
* @return a integer result of the comparison.
*/
public int compareTo(Object object) {
if (this.getClass() != object.getClass()) {
throw (new ClassCastException("Cannot compare unlike objects"));
}
return (toString().compareToIgnoreCase(object.toString()));
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getModifiedBy() {
return modifiedBy;
}
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
}
[code][/code]