I steped a little bit deeper into my problem.
Each time a load an object, it will be saved into the hibernate session.
Now, when I change an attribute to an empty value, the object is not valid any more.
The spring validator throws an error und shows my error page, but the object is written wrong to the session (with the empty attribute).
Going to an other navigation point in my app without saving the form, the session says the object is dirty and wants to write it to my database.
Is there a way, to remove the wrong object from the session, if the validator says it is wrong?
I allready tried the session.evict() method and it works.
But I can't imagine that I have to evict all objects from the session if I want to validate them.
the info statements show the session.getStatistics().getEntityKeys(), my CacheMode and my session dirty state. The rest is the hibernate debug output.
I try to change a appellation object.
Code:
DEBUG [btpool0-1] (ConnectionManager.java:439) - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.Appellation
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.Appellation
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.User
INFO [btpool0-1] (HibernateUtil.java:63) - IGNORE
INFO [btpool0-1] (HibernateUtil.java:64) - DIRTY?: false
DEBUG [btpool0-1] (JDBCTransaction.java:54) - begin
DEBUG [btpool0-1] (ConnectionManager.java:419) - opening JDBC connection
DEBUG [btpool0-1] (JDBCTransaction.java:59) - current autocommit status: false
DEBUG [btpool0-1] (JDBCContext.java:210) - after transaction begin
DEBUG [btpool0-1] (AbstractBatcher.java:358) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [btpool0-1] (AbstractBatcher.java:393) - select this_.appellationId as appellat1_39_0_, this_.appellation as appellat2_39_0_, this_.active as active39_0_ from Appellation this_ where this_.appellationId=
?
Hibernate: select this_.appellationId as appellat1_39_0_, this_.appellation as appellat2_39_0_, this_.active as active39_0_ from Appellation this_ where this_.appellationId=?
DEBUG [btpool0-1] (AbstractBatcher.java:476) - preparing statement
DEBUG [btpool0-1] (NullableType.java:133) - binding '1' to parameter: 1
DEBUG [btpool0-1] (AbstractBatcher.java:374) - about to open ResultSet (open ResultSets: 0, globally: 0)
DEBUG [btpool0-1] (NullableType.java:172) - returning '1' as column: appellat1_39_0_
DEBUG [btpool0-1] (AbstractBatcher.java:381) - about to close ResultSet (open ResultSets: 1, globally: 1)
DEBUG [btpool0-1] (AbstractBatcher.java:366) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [btpool0-1] (AbstractBatcher.java:525) - closing statement
DEBUG [btpool0-1] (JDBCTransaction.java:103) - commit
DEBUG [btpool0-1] (JDBCContext.java:201) - before transaction completion
DEBUG [btpool0-1] (JDBCTransaction.java:116) - committed JDBC Connection
DEBUG [btpool0-1] (JDBCContext.java:215) - after transaction completion
DEBUG [btpool0-1] (ConnectionManager.java:402) - aggressively releasing JDBC connection
DEBUG [btpool0-1] (ConnectionManager.java:439) - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.Appellation
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.Appellation
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.State
INFO [btpool0-1] (HibernateUtil.java:60) - net.bobo.app.model.User
INFO [btpool0-1] (HibernateUtil.java:63) - IGNORE
INFO [btpool0-1] (HibernateUtil.java:64) - DIRTY?: true
my appellation object mapping.
Code:
<?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>
<class name="net.bobo.app.model.Appellation" table="Appellation">
<comment></comment>
<id name="appellationId" type="int">
<column name="appellationId" />
<generator class="increment" />
</id>
<property name="appellation" type="string">
<column name="appellation" not-null="true">
<comment></comment>
</column>
</property>
<property name="active" type="boolean">
<column name="active" not-null="true">
<comment></comment>
</column>
</property>
</class>
</hibernate-mapping>
appellation manager
Code:
public class AppellationManager {
public void saveAppellation (Appellation appellation) {
Session session = HibernateUtil.getSession();
session.beginTransaction();
try {
session.saveOrUpdate(appellation);
session.getTransaction().commit();
} catch (HibernateException e) {
session.getTransaction().rollback();
throw e;
}
}
public Appellation getAppellation(int appellationId) {
Appellation app = null;
Session session = HibernateUtil.getSession();
session.beginTransaction();
//session.setFlushMode(FlushMode.MANUAL);
try {
Criteria crit = session.createCriteria(Appellation.class);
crit.add(Restrictions.eq("appellationId", appellationId));
app = (Appellation)crit.uniqueResult();
//session.evict(app);
session.getTransaction().commit();
} catch (HibernateException e) {
session.getTransaction().rollback();
throw e;
}
return app;
}
...
}
my appellation validator
Code:
public class AppellationEditValidator implements org.springframework.validation.Validator {
public boolean supports(Class clazz)
{
return Appellation.class.isAssignableFrom(clazz);
}
/**
* Validates an Appellation command object. Ensures that the name of
* the appellation is not empty.
* @see Appellation
*/
public void validate(Object command, Errors errors)
{
Appellation appellation = (Appellation) command;
if (appellation == null) return;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "appellation", "appellation.name", "error.empty.value");
}
}