-->
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.  [ 8 posts ] 
Author Message
 Post subject: collection.contains filter?
PostPosted: Sun Dec 12, 2004 10:18 pm 
Beginner
Beginner

Joined: Fri Oct 10, 2003 10:12 am
Posts: 39
hi ng,

i was wondering if it is possible to filter a collection to find out if it contains an object without initializing the collection, eg

Code:
Collection results = session .filter(collection, "select count() where this = ?", obj, Hibernate.OBJECT);


if this is not possible wouldn't it be a good feature to have or how about reimplementing Collection.contains() so that it can do this for you?

cam


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 13, 2004 3:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Doesnt this work for you ?

Code:
session.filter(collection, "where this = ?", obj, Hibernate.OBJECT).size()==1

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 7:54 pm 
Beginner
Beginner

Joined: Fri Oct 10, 2003 10:12 am
Posts: 39
i've tried your suggestion but i get the same error as before:

Code:
   
[junit] The collection was unreferenced; nested exception is net.sf.hibernate.QueryException: The collection was unreferenced
    [junit] org.springframework.orm.hibernate.HibernateQueryException: The collection was unreferenced; nested exception is net.sf.hibernate.QueryException: The collection was unreferenced
    [junit] net.sf.hibernate.QueryException: The collection was unreferenced
    [junit] at net.sf.hibernate.impl.SessionImpl.getFilterTranslator(SessionImpl.java:3450)
    [junit] at net.sf.hibernate.impl.SessionImpl.filter(SessionImpl.java:3513)
    [junit] at net.sf.hibernate.impl.SessionImpl.filter(SessionImpl.java:3412)


thanks,
cam


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 8:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
looks like you are referencing a collection which is not loaded/managed by hibernate...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 15, 2004 8:19 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
show more code


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 7:46 pm 
Beginner
Beginner

Joined: Fri Oct 10, 2003 10:12 am
Posts: 39
Hi guys,

sorry for my departure, i was tied up with other issues. ignore the previous problem that was my error, but the underlying issue remains, how to filter a a collection to find out if it contains an object...

here's a simple test case for the suggestion you gave:

Code:
<class name="eg.Parent">
      <id name="id">
         <generator class="native"/>
      </id>
      <set name="children" lazy="true" table="childset">
         <key column="parent_id"/>
         <many-to-many class="eg.Child"
            column="child_id"/>
      </set>
   </class>
   <class name="eg.Child">
      <id name="id">
         <generator class="native"/>
      </id>
      <property name="name"/>
   </class>


Code:
Session session = factory.openSession();
        Transaction transaction = null;

        try {
            session = factory.openSession();
            transaction = session.beginTransaction();

            Child child = new Child();
            session.save(child);

            Parent parent = new Parent();
            Set children = new HashSet();
            children.add(child);
            parent.setChildren(children);
            session.save(parent);

            session.flush();
            transaction.commit();
            session.close();

            session = factory.openSession();
            transaction = session.beginTransaction();

            parent = (Parent) session.load(Parent.class, parent.getId());
            child = (Child) session.load(Child.class, child.getId());

            Collection results = session.filter(parent.getChildren(), "where this = ?", child, Hibernate.OBJECT);

             System.out.println("Success, child is in collection");

            session.flush();
            transaction.commit();

        } catch (HibernateException e) {
            e.printStackTrace();
            if (transaction != null) {
                transaction.rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }


And the error:

Code:
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Child (name, id) values (?, ?)
Hibernate: insert into Parent (id) values (?)
Hibernate: insert into childset (parent_id, child_id) values (?, ?)
Hibernate: select parent0_.id as id0_ from Parent parent0_ where parent0_.id=?
Hibernate: select child0_.id as id0_, child0_.name as name0_ from Child child0_ where child0_.id=?
Hibernate: select this.id as id, this.name as name from childset children0_ inner join Child this on children0_.child_id=this.id where children0_.parent_id = ? and ((this.id=? ))
2004-12-22 10:39:35,515 WARN net.sf.hibernate.util.JDBCExceptionReporter - SQL Error: 1006, SQLState: 72000
2004-12-22 10:39:35,515 ERROR net.sf.hibernate.util.JDBCExceptionReporter - ORA-01006: bind variable does not exist

2004-12-22 10:39:35,515 WARN net.sf.hibernate.util.JDBCExceptionReporter - SQL Error: 1006, SQLState: 72000
2004-12-22 10:39:35,515 ERROR net.sf.hibernate.util.JDBCExceptionReporter - ORA-01006: bind variable does not exist

net.sf.hibernate.exception.GenericJDBCException: Could not execute query
   at net.sf.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:90)
   at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:79)
   at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
   at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4110)
   at net.sf.hibernate.impl.SessionImpl.filter(SessionImpl.java:3561)
   at net.sf.hibernate.impl.SessionImpl.filter(SessionImpl.java:3447)
   at eg.Test.main(Test.java:86)
Caused by: java.sql.SQLException: ORA-01006: bind variable does not exist

   at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
   at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
   at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:582)
   at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1986)
   at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:880)
   at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:2516)
   at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2850)
   at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:609)
   at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:537)
   at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:88)
   at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
   at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
   at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
   at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
   at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
   at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
   at net.sf.hibernate.impl.SessionImpl.filter(SessionImpl.java:3558)
   ... 2 more


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 4:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
how about using Hibernate.entity(yourclass) instead of Hibernate.OBJECT ...saying this without testing it ,)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 5:57 pm 
Beginner
Beginner

Joined: Fri Oct 10, 2003 10:12 am
Posts: 39
fantastic!! all good, thank-you!!


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