Dear members,
I have some property (settled_position) that has to be possitive, but the Oracle version I am ussing can't restrict it on column definition, so I have defined the property settled_position that belong to class/table Position, like this:
Code:
<property name="settledPosition" type="java.lang.Integer">
<meta attribute="use-in-tostring">true</meta>
<meta attribute="pre-cond"><![CDATA[
if ((settledPosition != null) && (settledPosition.intValue() < 0)) {
throw new IllegalArgumentException(settledPosition.toString());
}
]]>
</meta>
<column name="settled_position" />
</property>
so, I have added a pre-condition in order to be generated by hbm2java task from HibernateTools (I have redefined the Freemarker template for accepting meta attribute pre-cond). The result will be something like this for this property:
Code:
public void setSettledPosition(Integer settledPosition) {
if ((settledPosition != null) && (settledPosition.intValue() < 0)) {
throw new IllegalArgumentException(settledPosition.toString());
}
this.settledPosition = settledPosition;
}
so, I am controlling via Java that no negative value can be assigned.
The problem comes because I have an association with contract, so a contract have 0..n positions.
and before saving the new possition into database I do:
Code:
Contract contract = position.getContract();
and
Code:
contract.getPositions().add(position);
Hibernate for doing contract.getPositions(), it internally creates new possitions assigning to the settled_position value an arbitrary value, and some of them are negatives.
I am pretty sure I am not creating positions with negatives values, before such sentence I know I have to save only two positions with possitive settled_position.
Why this happened?, so there is no way to restrict a given subset of values for a property mapped via Hibernate?The original code is (I am ussing Spring + Hibernate):
Code:
public void savePositions(Collection positions) throws DataAccessException {
int counter = 0;
Position position;
HibernateTemplate hbmTemplate = getHibernateTemplate();
for (Iterator it = positions.iterator(); it.hasNext(); counter++) {
position = (Position) it.next();
Session session = bindTransactionOnCurrentThread();
Contract contract = position.getContract();
hbmTemplate.update(contract);
contract.setUpdated(true);
contract.getPositions().add(position); // <---HERE
unbindTransanctionOnCurrentThread(session);
savePosition(position, counter);
}
}
public Session bindTransactionOnCurrentThread() {
Session session = SessionFactoryUtils.getSession(getHibernateTemplate()
.getSessionFactory(), true);
TransactionSynchronizationManager.bindResource(getHibernateTemplate()
.getSessionFactory(), new SessionHolder(session));
return session;
}
public void unbindTransanctionOnCurrentThread(Session session) {
TransactionSynchronizationManager.unbindResource(getHibernateTemplate()
.getSessionFactory());
SessionFactoryUtils.releaseSession(session, getHibernateTemplate()
.getSessionFactory());
}
Note: It is a batch process so I need to flush only at the end of transaction, which is long time process, so following the recomendation from:
http://today.java.net/pub/a/today/2005/10/11/testing-hibernate-mapping.html?page=last
I have added the methods: bindTransactionOnCurrentThread, unbindTransanctionOnCurrentThread
Thanks in advance,
David Leal