Issue 1:
Hans Desmet opened SPR-6871 and commented

When you submit a form, with databinding on a command object with indexed properties, the indexed properties of the command object are not filled in.

I think this bug is related to the correction that happened in #11506.

The characters [ and ] are not only removed from the id attrbute of the HTML elements, but also (unnessesary) from the name attribute of the HTML elements.

You can see it happening via next example.

When you type 10 and 20 in the input fields, the output to the console is: {Belgium=null, Switzerland=null}

while the output should have been {Belgium=10, Switzerland=20}

The class that acts as command object:

package org.example.entities;

import java.util.LinkedHashMap;

import java.util.Map;

public class Continent {

// key = name of country

// Integer= number of inhabitants

private Map<String, Integer> countries = new LinkedHashMap<String, Integer>();

public Continent() {

countries.put("Belgium", null);

countries.put("Switzerland", null);

}

public void setCountries(Map<String, Integer> countries) {

this.countries = countries;

}

public Map<String, Integer> getCountries() {

return countries;

}

}

The Controller class:

package org.example.web;

import org.example.entities.Continent;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

@Controller

public class ContinentController {

@RequestMapping(value = "/continent.htm", method = RequestMethod.GET)

public String continentForm(Model model) {

Continent continent = new Continent();

model.addAttribute(continent);

return "continent.jsp";

}

@RequestMapping(value = "/continent.htm", method = RequestMethod.POST)

public String continentForm(@ModelAttribute Continent continent,

BindingResult bindingResult) {

System.out.println(continent.getCountries()); // Here you can see the bug

return "continent.jsp";

}

}

continent.jsp

<?xml version="1.0" encoding="UTF-8"?>

<%@page contentType="text/html" pageEncoding="UTF-8" session="false"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">

<head>

<title>Continent example</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

</head>

<body>

<form:form commandName="continent">

<c:forEach items="${continent.countries}" var="entry">

<div>

<form:label path="countries[${entry.key}]">${entry.key}</form:label>

<form:input path="countries[${entry.key}]" />

<form:errors path="countries[${entry.key}]" />

</div>

</c:forEach>

<div><input type="submit" /></div>

</form:form>

</body>

</html>

Affects: 3.0.1

Issue Links:

#11586 Indexed properties not set correctly in form tags ("is duplicated by")

#11702 form:select 3.0.1 regression bug when binding to a map ("is duplicated by")

#11506 form:checkbox tag creates invalid HTML id when bound to indexed property

#10055 org.springframework.web.servlet.tags.form.TagIdGenerator doesn't remove characters [] and double-quotes

Referenced from: commits 67b342d

3 votes, 8 watchers

Issue 2:
Koen Serneels opened SPR-9480 and commented

Overview

There seems yet again another issue when using the Spring 3.1 - Hibernate4(.1.3) integration.

This issue pops up so late since it only occurs in special circumstances: it has to do with the hibernate "smart" flushing which is not working.

Hibernate guarantees that every modification you make (CUD) is flushed to the database prior any other operation that can possibly be related to the outstanding queries. This is to ensure that you don't work with stale data within the same transaction.

For example:

Save an entity

Update the property of an entity

Query the entity using the value of the property changed in the previous step in the where clause

Hibernate will make sure that the changes by step2 (and possibly also the insert of step1 - if not done already) are flushed before step3 is executed (smart flush). If this didn't happen we would never be able to retrieve the entity in step3.

The problem is that this smart flushing is not happening any more, since Hibernate does not detect that it is in a transaction.

Taken from SessionImpl L1178:

protected boolean autoFlushIfRequired(Set querySpaces) throws HibernateException {
errorIfClosed();
if ( ! isTransactionInProgress() ) {
// do not auto-flush while outside a transaction
return false;
}
AutoFlushEvent event = new AutoFlushEvent( querySpaces, this );
for ( AutoFlushEventListener listener : listeners( EventType.AUTO_FLUSH ) ) {
listener.onAutoFlush( event );
}
return event.isFlushRequired();
}

What happens is before step3 is excecuted 'autoFlushIfRequired' is called (good). However isTransactionInProgress() will returns false. If you drill down in the code, you will see that it will call: transactionCoordinator.isTransactionInProgress() which will then call getTransaction().isActive(), which delegates to JtaTransaction L237:

@Override
public boolean isActive() throws HibernateException {
if ( getLocalStatus() != LocalStatus.ACTIVE ) {
return false;
}

final int status;
try {
status = userTransaction.getStatus();
}
catch ( SystemException se ) {
throw new TransactionException( "Could not determine transaction status: ", se );
}
return JtaStatusHelper.isActive( status );
}

The LocalStatus will be "NOT_ACTIVE" and the userTransaction is null. Why? Because no one called "begin" on the JtaTransaction.

In case of the HibernateTransactionManager it will call begin() once a transaction is started (in that case it will be JdbcTransaction rather then JtaTransaction).

So while there is a transaction started and everything is working nicely there is still a part of Hibernate which is unaware that a transaction is indeed active, which results in strange behavior like illustrated here. Note that everything else is working OK, the session will get flushed before transaction completion and everything will be in the database.

However, within the transactions we now have a stale data problem. AFAIK this is a bug in the integration, since there are no more secret properties we can use to fix this one on hibernate level.

Examples

I supplied again 2 sample applications, one with hibernate3 and the same with hibernate4 to illustrate the issue.

You can deploy the apps under context root hibernate3/hibernate4 and then point the browser to http://<host>:<port>/hibernate3/Persist or http://<host>:<port>/hibernate4/Persist.

The Servlet looks up a bean from the application context. It will then call two transactional methods on the bean.

Method 1

Start transaction 1

Save an entity of type 'TestEntity'

Change the property 'value' to literal 'SomeValue' on the saved entity

Perform a query which selects all entities of type 'TestEntity' where their 'value' property matches 'SomeValue'

return result

display result

End transaction 1

Method 2

Start transaction 2

Perform a query which selects all entities of type 'TestEntity' where their 'value' property matches 'SomeValue'

return result

display result

End transaction 2

With hibernate3 you will see this output:

Saving... Done.
Result from read in TX: 1 Value:SomeValue
Read from table in separate TX: 1 Value:SomeValue

Which means that both in the same transaction and in the new transaction the data was found in database after saving/updating.

In hibernate4 however:

Saving... Done.
Result from read in TX:
Read from table in separate TX: 1 Value:SomeValue

You see that in the same transaction the query did not return any results in the second output line. This is because the save of the entity and/or the update of the property where not flushed to database prior executing the query.

Note: in the output we show two properties of the 'TestEntity'.

The value '1' in the output is the value of the 'id' property which is the PK of the entity and auto-increment.

'SomeValue' is the literal value we assigned to the 'value' property of the entity after we saved the entity.

Affects: 3.1.1

Reference URL: http://forum.springsource.org/showthread.php?126363-Session-not-flushed-with-Hibernate-4-1-3-JTA-and-Spring-transaction-management-integ&highlight=koen+serneels

Attachments:

hibernate3.zip (7.31 kB)

hibernate4.zip (7.36 kB)

Issue Links:

#14040 Session not flushed with Hibernate 4.1.3, JTA and Spring transaction management integration ("is duplicated by")

#18421 Hibernate 4 Autoflush does not work with Spring OpenSessionInViewInterceptor

Referenced from: commits efabc6b, dda3197

5 votes, 8 watchers