-->
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.  [ 11 posts ] 
Author Message
 Post subject: Mapping error while using simple filter
PostPosted: Mon Aug 14, 2006 5:25 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Hi all,
How do I use a filter in the mapping mapping file??? I took the syntax directly off the Hibernate ref guide and I believe I'm using the correct DTD... But still I get an exception... If I remove the filter usage, all is ok...

Hibernate version:
3.1

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <filter-def name="activeSession">
    </filter-def>

    <class name="test.Cls" table="CLS">
        <id name="id" column="CLS_ID">
            <generator class="native"/>
        </id>
        <map name="sess" cascade="all-delete-orphan" inverse="true">
            <key column="CLS_ID" not-null="true"/>
            <map-key formula="name" type="string"/>
            <one-to-many class="test.Session"/>
            <filter name="activeSession" condition="endtime is null"/>
        </map>

    </class>
</hibernate-mapping>


Name and version of the database you are using:
HSQL DB 1.8.0.4

Full stack trace of any exception that occurs:
12:07:36,265 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(11) Element "hibernate-mapping" does not allow "class" here.
Initial SessionFactory creation failed.org.hibernate.MappingException: Could not read mappings from resource: test/Cls.hbm.xml
java.lang.ExceptionInInitializerError
at test.HibUtil.<clinit>(HibUtil.java:29)
at test.Main.main(Main.java:48)
Caused by: org.hibernate.MappingException: Could not read mappings from resource: test/Cls.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:485)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1310)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1296)
at test.HibUtil.<clinit>(HibUtil.java:25)
... 1 more
Caused by: org.hibernate.MappingException: invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:425)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:482)
... 8 more
Caused by: org.xml.sax.SAXParseException: Element "hibernate-mapping" does not allow "class" here.
at org.apache.crimson.parser.Parser2.error(Parser2.java:3354)
at org.apache.crimson.parser.ValidatingParser$ChildrenValidator.consume(ValidatingParser.java:349)
at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1497)
at org.apache.crimson.parser.Parser2.content(Parser2.java:1963)
at org.apache.crimson.parser.Parser2.maybeElement(Parser2.java:1691)
at org.apache.crimson.parser.Parser2.parseInternal(Parser2.java:667)
at org.apache.crimson.parser.Parser2.parse(Parser2.java:337)
at org.apache.crimson.parser.XMLReaderImpl.parse(XMLReaderImpl.java:448)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:422)
... 9 more


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 6:43 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Try with:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="test.Cls" table="CLS">
        <id name="id" column="CLS_ID">
            <generator class="native"/>
        </id>
        <map name="sess" cascade="all-delete-orphan" inverse="true">
            <key column="CLS_ID" not-null="true"/>
            <map-key formula="name" type="string"/>
            <one-to-many class="test.Session"/>
            <filter name="activeSession" condition="endtime is null"/>
        </map>

    <filter-def name="activeSession">
    </filter-def>

    </class>
</hibernate-mapping>

Please, don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 7:28 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Error again, this time, that 'class' does not allow 'filter-def'... so I tried putting it AFTER the </class> tag and this time it compiled... only the filter did not take effect (I enabled it using enableFilter). Am I writing it correctly? I've heard that filters use SQL and not HQL...

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 7:38 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
My mistake.

Yes, filters must be explicitly enabled through use of the Session.enabledFilter() method.

Yes, filters do use SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 7:40 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Do you know why the filter does not take effect, then? It seems that I get all objects, regardless of the condition of the filter....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 7:45 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
post your java code


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 7:53 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
Code:
        Session session = HibUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.enableFilter("activeSession");

        List list = session.createQuery("from Cls").list();
        for (int i = 0; i < list.size(); ++i) {
            Cls c1 = (Cls)list.get(i);
            Map m = c1.getSessions();
            // .. rest of code iterates and displays map contents
        }


In my case, the DB holds one session with endtime=14-aug-2006 14:40:00, but still it appears in the map above. It should not have appears since the filter requires that only endtime=null are selected.
NOTE: even if I write "blahblahblah" as the filter's condition, there is no error... as if the filter is not even used :-(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 8:06 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Well, if sess map should ALWAYS have active sessions only then I think it's easiest to use property 'where' in the map tag:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="test.Cls" table="CLS">
      <id name="id" column="CLS_ID">
         <generator class="native"/>
      </id>
      <map name="sess" cascade="all-delete-orphan" inverse="true" where="endtime is null">
         <key column="CLS_ID" not-null="true"/>
         <map-key formula="name" type="string"/>
         <one-to-many class="test.Session"/>
      </map>
   </class>
</hibernate-mapping>

Please, rate again if it works


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 8:11 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
No go. I did as you said:

Code:
        <map name="sess" cascade="all-delete-orphan" inverse="true" access="field" where="endtime is null">
            <key column="SITE_ID" not-null="true"/>
            <map-key formula="name" type="string"/>
            <one-to-many class="hib4.AbstractSes"/>
        </map>


And still, all session entires appear in the map. Again, even if I write where="blahblahblah", there is no error and I get all session entires.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 8:16 am 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
It seems as if you were executing an out of synch version of your program. What happens if you put:
Code:
      <id name="blablabla" column="CLS_ID">
         <generator class="native"/>
      </id>

does it still work?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 14, 2006 8:20 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
it fails. I get:
org.hibernate.PropertyNotFoundException: field not found: blablabla


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