Yes it is possible. There are a number of options. For instance, you may use
named queries. In this case you write the query in your Entity class then
access the query from your DAO when needed. Lets see if I could construct an
example:

Suppose you have an Entity class called Landlord, then you might do this:

@NamedQueries({
    @NamedQuery(name="landlord.details",
    query = "Select a from Landlord a where a.someProperty = :var")
})
@Entity
@Table(name="landlord")
public class Landlord {

...
}//end of Landlord class

You may have many queries defined like this. Notice the :var parameter that
I included.

Now the LandlordDaoHibernate implementation may access this query like this:

public List<Landord> findAllLandlords(String param) {
        String args[] = {"var"};
        Object vals[] = {param};
        return
getHibernateTemplate().findByNamedQueryAndNamedParam("landlord.details",
args, vals);
 }

This would be an example of a method in your Dao. The HibernateTemplate
class has many other methods for executing queries so if this is not
suitable you can look up some others.

If you prefer a Native Query (using SQL) rather than JPSQL then you can use
@NamedNativeQuery instead. It looks like this:

@NamedNativeQuery(
   name="nameofyourquery",
  query="actual sql query here",
  resultClass=YourClass.class
)

Though I am sure this would not be necessary.

Now the trick is to write the query itself in such a way that it eliminates
the duplicates for you.



On Fri, Sep 12, 2008 at 5:28 PM, Joey122 <[EMAIL PROTECTED]> wrote:

>  Thanks for the reply -
>
> I guess the problem is that in the DAO we do something like: 
> getHibernateTemplates.findByCriteria......
> This doesnt work for me :( - I d much rather write the sql statement from
> scratch like select colA from myTable.... Is this possible?
>
> Vincent Ramdhanie wrote:
>  I don't think that a group by is designed to remove duplicates, you
> probably want to select distinct. However, in Hibernate if you are using
> eager fetching you might still end up with duplicates so you may have to
> remove them yourself. This topic might provide some clues:
> http://www.javalobby.org/java/forums/t68781.html Vincent On Fri, Sep 12,
> 2008 at 6:42 AM, Joey122 wrote: > I ve been blown away by how neat Appfuse
> is and its been a great > kickstart. > > I m trying to ensure that
> duplicates are removed from the data returned by > hibernate by forcing a
> group by - So far I ve tried to use the Projection > group property on the
> criteria as follows: > >
> criteria.setProjection(Projections.projectionList().add( >
> Projections.groupProperty("landlord_details").as( "landlord_details")).add(
> > Projections.groupProperty("landlord_agent_details").as( >
> "landlord_agent_details"))); > > However this no longer returns all the data
> I need and only returns the > columns which I have grouped by which is wrong
> :( > > Can anyone give a pointer on how to force a group by on certain
> columns > whilst still keeping all the other columns that I need? >
> ------------------------------ > View this message in context: Forcing A
> Group By In Hibernate > Sent from the AppFuse - User mailing list archiveat
> Nabble.com. >
>
>
> ------------------------------
> View this message in context: Re: Forcing A Group By In 
> Hibernate<http://www.nabble.com/Forcing-A-Group-By-In-Hibernate-tp19453450s2369p19464442.html>
> Sent from the AppFuse - User mailing list 
> archive<http://www.nabble.com/AppFuse---User-f2370.html>at Nabble.com.
>



-- 
Vincent Ramdhanie
Vinette Technologies

Reply via email to