-->
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.  [ 3 posts ] 
Author Message
 Post subject: Validator in "SELECT ... FROM ... WHERE" query par
PostPosted: Mon Feb 16, 2009 8:20 am 
Beginner
Beginner

Joined: Fri Jan 23, 2009 10:34 am
Posts: 25
Location: Switzerland
Hi all,

I'm using Seam 2.0.0.GA and Hibernate 3.3.1.GA / Hibernate Validator 3.1.0.GA. Persistent objects are annotated POJOs.

My persistent object looks like:

Code:
@Table(...)
class MyTable {
  String myField;

  @Column(...)
  @Length(max=3)
  public String getMyField() { return myField; }

   ...
}


When persisting/updating, Hibernate Validator are correctly applied, so that the following code raises an InvalidStateException because the 'myField' field is limited to 3 characters:

Code:
MyTable myTable = new MyTable();
myTable.setMyField("foo bar foo bar foo bar foo bar");
entityManager.persist(myTable);


However, when I'm doing the following query, a JDBC exception is raised because the length constraint is violated in the database:

Code:
MyTable myTable = (MyTable)entityManager.
    createQuery("select p from MyTable p where p.myField=:myField").
    setParameter("myField", "foo bar foo bar foo bar").getSingleResult();


According to the documentation, this is the correct behavior, the Hibernate validators being applied only for insert/update queries.

So this is the question: is it possible to enable Hibernate validator also for SELECT query parameters ?
i.e. in the above query, I'm expecting an "InvalidStateException: field MyTable.myField is limited to 3 characters" exception.

Currently, my solution is to check the parameter before the SELECT query is done:

Code:
String param = "foo bar foo bar foo bar foo bar";
int maxLen = MyTable.class.getDeclaredMethod("getMyField").getAnnotation(Length.class).max();
if (param.length()>maxLen) {
  throw new IllegalArgumentException("max len is "+maxLen);
}
MyTable myTable = (MyTable)entityManager.
    createQuery("select p from MyTable p where p.myField=:myField").
    setParameter("myField", param).getSingleResult();


However, this solution is neither elegant, nor reusable. So it would be great if Hibernate would validate the parameters for me.

Does such feature exist?

Thanks,
Julien


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 8:58 am 
Beginner
Beginner

Joined: Fri Jan 23, 2009 10:34 am
Posts: 25
Location: Switzerland
Anyone?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 9:20 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
No, I don't think it is possible to make Hibernate Validators check query parameters.

You should evaluate using Critieria, especially Query-By-Example. That way you could construct example objects which you could validate easily.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.