Hi receive this error when I try to saveOrUpdate an Hibernate Object.
Code:
org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.intesasanpaolo.tps.model.ECurrenciesDiffmin#com.intesasanpaolo.tps.model.ECurrenciesDiffmin@f5bad0]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.intesasanpaolo.tps.model.ECurrenciesDiffmin#com.intesasanpaolo.tps.model.ECurrenciesDiffmin@f5bad0]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:661)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:339)
at com.intesasanpaolo.tps.dao.GenericDAO.update(Unknown Source)
at com.intesasanpaolo.tps.manager.configuration.ConfiguratorManager.saveOrUpdateConfiguration(ConfiguratorManager.java:75)
at com.intesasanpaolo.tps.form.configuration.ConfiguratorController.processFormSubmission(ConfiguratorController.java:114)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:265)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:595)
The object I try to save is:
Code:
public class EConfiguration {
protected String name;
private Date validFrom;
private ETimeGridConfiguration timeGridPrototype;
private List externalPortfolios;
private List internalPortfolios;
private List externalCounterparts;
private List internalCounterparts;
private BigDecimal operatingRangeHigh;
private BigDecimal operatingRangeLow;
private BigDecimal maxOperatingDelta;
private Set currenciesDiffMin;
protected EConfiguration() {}
public EConfiguration(String name) {
this.name = name;
}
/**
* Configuration name
* @return The name of the configuration
*/
public String getName() {
return name;
}
/**
* Validity date
* @return The first date for which this configuration is valid
*/
public Date getValidFrom() {
return validFrom;
}
public void setValidFrom(Date validFrom) {
this.validFrom = validFrom;
}
/**
* Time grid prototype
* @return A {@link ETimeGridConfiguration} object
*/
public ETimeGridConfiguration getTimeGridPrototype() {
return timeGridPrototype;
}
public void setTimeGridPrototype(ETimeGridConfiguration timeGridPrototype) {
this.timeGridPrototype = timeGridPrototype;
}
/**
* External deal portfolios that must be used for this hedging process
* @return A List of String representing the names of the portfolios
*/
public List getExternalPortfolios() {
return externalPortfolios;
}
public void setExternalPortfolios(List externalPortfolios) {
this.externalPortfolios = externalPortfolios;
}
/**
* Internal deal portfolios that must be hedged by this hedging process
* @return A List of String representing the names of the portfolios
*/
public List getInternalPortfolios() {
return internalPortfolios;
}
public void setInternalPortfolios(List internalPortfolios) {
this.internalPortfolios = internalPortfolios;
}
/**
* External deal counterparts that must be used for this hedging process
* @return A List of String representing the names of the counterparts
*/
public List getExternalCounterparts() {
return externalCounterparts;
}
public void setExternalCounterparts(List externalCounterparts) {
this.externalCounterparts = externalCounterparts;
}
/**
* Internal deal counterparts that must be hedged by this hedging process
* @return A List of String representing the names of the counterparts
*/
public List getInternalCounterparts() {
return internalCounterparts;
}
public void setInternalCounterparts(List internalCounterparts) {
this.internalCounterparts = internalCounterparts;
}
/**
* High limit of hedging ratio
* @return A BigDecimal representing a hedge/risk ratio
*/
public BigDecimal getOperatingRangeHigh() {
return operatingRangeHigh;
}
public void setOperatingRangeHigh(BigDecimal operatingRangeHigh) {
this.operatingRangeHigh = operatingRangeHigh;
}
/**
* Low limit of hedging ratio
* @return A BigDecimal representing a hedge/risk ratio
*/
public BigDecimal getOperatingRangeLow() {
return operatingRangeLow;
}
public void setOperatingRangeLow(BigDecimal operatingRangeLow) {
this.operatingRangeLow = operatingRangeLow;
}
/**
* Absolute value of the maximum risk amount that can exceed hedging for every time bucket
* @return A BigDecimal representing an amount
*/
public BigDecimal getMaxOperatingDelta() {
return maxOperatingDelta;
}
public void setMaxOperatingDelta(BigDecimal maxOperatingDelta) {
this.maxOperatingDelta = maxOperatingDelta;
}
public void setName(String name) {
this.name = name;
}
public Set getCurrenciesDiffMin() {
return currenciesDiffMin;
}
public void setCurrenciesDiffMin(Set currenciesDiffMin) {
this.currenciesDiffMin = currenciesDiffMin;
}
}
The object above have a one to many relation mapped in this way:
Code:
<set name="currenciesDiffMin" lazy="true" inverse="true" cascade="all">
<key column="TPS16_ID"/>
<one-to-many class="com.intesasanpaolo.tps.model.ECurrenciesDiffmin"/>
</set>
The child object is
ECurrenciesDiffMin and I mapped the relation in this way:
Code:
<composite-id>
<key-many-to-one name="configuration" class="com.intesasanpaolo.tps.model.EConfiguration" column="TPS16_ID" />
<key-property name="currency" type="string">
<column name="TPS25_ISO_CURRENCY"></column>
</key-property>
</composite-id>
When I try to save or update the object
EConfiguration and I remove or I add a new object
ECurrenciesDiffMin first of all I create an empty
EConfiguration object and I insert a new Set in this way
Code:
Set currDiff = new HashSet();
for(int i=0; i<valute.length; i++){
ECurrenciesDiffmin ecd = new ECurrenciesDiffmin();
ecd.setConfiguration(econf);
ecd.setCurrency(valute[i]);
ecd.setDiffMin(new BigDecimal(diffs[i]));
currDiff.add(ecd);
}
econf.setCurrenciesDiffMin(currDiff); //econ is an EConfiguration object
The after a check I choose If update a create a new object.
If I update the object I execute this snippet of code:
Code:
Iterator currDiff = econf.getCurrenciesDiffMin().iterator();
while(currDiff.hasNext()){
ECurrenciesDiffmin a = (ECurrenciesDiffmin) currDiff.next();
a.setConfiguration(econPersisted);
econPersisted.getCurrenciesDiffMin().add(a);
}
Where econPersisted is the object
EConfiguration I want update.
The I call the update and I pass the object econPersisted.
In practice I remove from econPersisted all the children
ECurrenciesDiffMin and than I insert another group of children that can contains some previous children. In this moment I receive the exception.
How can I solve?
Thanks, bye bye.