-->
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.  [ 6 posts ] 
Author Message
 Post subject: named parameters and components
PostPosted: Thu Feb 19, 2004 3:50 am 
Beginner
Beginner

Joined: Wed Feb 18, 2004 1:28 pm
Posts: 20
Location: Rovereto - Italy
I'm using "named parameters" and the method Query.setProperties to bind variables in SQL statement.
This method doesn't bind the components of my persistent object but only the properties.
Is this Correct? Is there another way to bind dynamically the components and properties without having to bind manually each variables (obviously not with setString(String name, String val), setInteger(String name, String val)..., I know that) ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 5:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Could you give an example of what you are using this for?


Top
 Profile  
 
 Post subject: Example
PostPosted: Thu Feb 19, 2004 6:43 am 
Beginner
Beginner

Joined: Wed Feb 18, 2004 1:28 pm
Posts: 20
Location: Rovereto - Italy
// my persistent object
public class PersistentObject implements Serializable {
// my property
private String prop;

// my component
private Period period



public String getProp() {...};
public void setProp(String prop) {...};

public Period getPeriod() {...};
public void setPeriod(Period period) {...};

...

}

// my component
public class Period implements Serializable {
private Date begin;
private Date end;

public Date getBegin() {...};
public void setBegin(Date d) {...};

public Date getEnd() {...};
public void setEnd(Date d) {...};

...

}

// Data access object (DAO):
PersistentObjectDAO {
...
public static List search(PersistentObject po) {
...
// open an hibernate session
Session session = factory.openSession();
String sql = "from PersistentObject as p where p.prop = :prop and p.period.begin = :period.begin and p.period.end = :period.end";

Query query = session.createQuery(sql);
query.setMaxResults(20);
query.setFirstResult(100);

// bind all properties dynamically
query.setProperties(po);

// Now I must bind the components "not dynamically"
query.setDate("period.begin", po.getPeriod.getBegin());
query.setDate("period.end", po.getPeriod.getEnd());


...
List result = query.list();
...
return result;
}
...
}

Is there another way to bind "dynamically" the components?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 6:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No :)


Top
 Profile  
 
 Post subject: ok
PostPosted: Thu Feb 19, 2004 6:49 am 
Beginner
Beginner

Joined: Wed Feb 18, 2004 1:28 pm
Posts: 20
Location: Rovereto - Italy
Thank you

Lorenzo


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2004 2:14 pm 
Beginner
Beginner

Joined: Sun Oct 26, 2003 11:21 pm
Posts: 27
You might want to take a look at the Query by Example system in section 14.6 of the Hibernate Reference. I think that this does what you're looking for.

Also, check out setProperties(Object bean) method in Query -

Bind the property values of the given bean to named parameters of the query, matching property names with parameter names and mapping property types to Hibernate types using hueristics.


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