-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: How to restrict a given property has to be possitive?
PostPosted: Thu Sep 21, 2006 10:13 am 
Beginner
Beginner

Joined: Mon Nov 28, 2005 11:03 am
Posts: 39
Location: Madrid
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

_________________
David Leal


Top
 Profile  
 
 Post subject: Correction
PostPosted: Thu Sep 21, 2006 10:39 am 
Beginner
Beginner

Joined: Mon Nov 28, 2005 11:03 am
Posts: 39
Location: Madrid
Dear member,

I have added this pre-condition, because I have found that some values where negatives, so Hibernate works fine, because the negative value I get comes from an value already exist on database. It is not transient object, it is a persistent object it get's from database.

So everything is ok,

Thanks any way,

David

_________________
David Leal


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 21, 2006 11:12 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
Why Oracle version do you use?
As far as I rememer even Oracle 8 got check constraints for this kind of constraints.

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 21, 2006 3:49 pm 
Beginner
Beginner

Joined: Mon Nov 28, 2005 11:03 am
Posts: 39
Location: Madrid
I am using the 8.x, it is true, that is possible to specify an unsigned integer, but my boss didn't allow me to do, just they are not used to apply such constraints into data, they prefer to control this via source code.

Any way, it should be a good idea, in the same way there is not-null, to specify positive or any other condition.

Thanks any way for yuor interest,

David

_________________
David Leal


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.