-->
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.  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: SQL to HQL
PostPosted: Fri Mar 26, 2004 5:31 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
Hi!

I do have some problems understanding HQL...

This is the SQL statement:

SELECT urls.url, urls.id, html.html FROM html INNER JOIN urls
ON urls.id = html.urlid
WHERE url LIKE 'http://www.domain1.com/%'
OR url LIKE 'http://www.domain2.de/%'
OR url LIKE ......

And this is my idea of a corresponding HQL statement (there no ON for JOINs?):

SELECT urls.url, urls.id, html.html FROM f4t.hibernate.Html INNER JOIN f4t.hibernate.Urls
WHERE urls.id = html.urlid
AND url LIKE 'http://www.domain1.com/%'"
OR url LIKE 'http://www.domain2.de/%'
OR url LIKE ......

-> net.sf.hibernate.QueryException: outer or full join must be followed by path expression

??


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 5:45 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
Ok, got it :-)

SELECT urls.url, urls.id, html.html FROM f4t.hibernate.Urls as urls JOIN urls.html as html
WHERE urls.id = html.urlid
AND url LIKE...

But what class is it what the query returns, i.e. what do I have the iterator items to cast to?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 5:54 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
it returns a list of Object[]
Object[] myResult = (Object[])list.get(0) --> returns first result
String myUrl = (String)myResult[0] --> returns the url of the first result

but you might think different if you want to retrieve full objects, to update them later


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 6:00 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
delpouve wrote:
it returns a list of Object[]
but you might think different if you want to retrieve full objects, to update them later


And how would I think? :-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 6:06 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
delpouve wrote:
it returns a list of Object[]
Object[] myResult = (Object[])list.get(0) --> returns first result
String myUrl = (String)myResult[0] --> returns the url of the first result


Thanks. But this way I will run into strange problems when the order is changed. Can't hibernate return an HashMap or something? In JDBC I can access the columns by name:

resultSet.getString("url");


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 6:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
maybe this
SELECT urls, html FROM f4t.hibernate.Urls as urls JOIN urls.html as html
WHERE urls.id = html.urlid
AND url LIKE...

:)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 6:47 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
delpouve wrote:
maybe this
SELECT urls, html FROM f4t.hibernate.Urls as urls JOIN urls.html as html
WHERE urls.id = html.urlid
AND url LIKE...


Isn't this slower? Besides it seems that Hibernate runs 2 queries in order to execute this statement.

[java] Hibernate: select urls0_.id as x0_0_, html1_.urlid as x1_0_ from urls urls0_ inner join html html1_ on urls0_.id=html1_.urlid where (urls0_.id=html1_.urlid )AND((lower(urls0_.url)LIKE 'http://www.domain1.com/%' )OR(lower(urls0_.url)LIKE 'http://www.domain2.com/%' )OR(lower(urls0_.url)LIKE 'http://www.domain3.com/%' )) limit ?
[java] Hibernate: select urls0_.id as id2_, urls0_.url as url2_, urls0_.valid as valid2_, urls0_.timestamp as timestamp2_, text1_.urlid as urlid0_, text1_.html as html0_, text1_.title as title0_, text1_.summary as summary0_, html2_.urlid as urlid1_, html2_.html as html1_, html2_.title as title1_ from urls urls0_ left outer join text text1_ on urls0_.id=text1_.urlid left outer join html html2_ on urls0_.id=html2_.urlid where urls0_.id=?

Anyway, it doesn't work:

net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of f4t.hibernate.Html.setUrlid


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 6:48 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
do you use proxy + outer join = false?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 6:52 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
delpouve wrote:
do you use proxy + outer join = false?


Well...I don't know. This is actually my second day using hibernate. Where do I have to look at? :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 6:55 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
tcn wrote:
delpouve wrote:
do you use proxy + outer join = false?


Well...I don't know. This is actually my second day using hibernate. Where do I have to look at? :)


Outer Join in generated (!) .hbm is set to auto.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 6:55 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
just show your hibernate.cfg.xml and your mapping files


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 6:58 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/f4t</property>
<property name="dialect">net.sf.hibernate.dialect.PostgreSQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="f4t/hibernate/Urls.hbm.xml"/>
<mapping resource="f4t/hibernate/Html.hbm.xml"/>
</session-factory>
</hibernate-configuration>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class name="f4t.hibernate.Html" table="html" >
<id name="urlid" type="java.lang.Long" column="urlid" >
<generator class="assigned" />
</id>
<property name="html" type="java.lang.String" column="html" not-null="true" length="-1" />
<property name="title" type="java.lang.String" column="title" />
<!-- associations -->
<!-- bi-directional one-to-one association to Url -->
<one-to-one name="urlid" class="f4t.hibernate.Urls" outer-join="auto" constrained="true" />
</class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->
<class name="f4t.hibernate.Urls" table="urls" >
<id name="id" type="java.lang.Long" column="id" >
<generator class="assigned" />
</id>
<property name="url" type="java.lang.String" column="url" not-null="true" unique="true" length="1024" />
<property name="valid" type="boolean" column="valid" not-null="true" length="1" />
<property name="timestamp" type="java.sql.Timestamp" column="timestamp" length="8" />
<!-- associations -->
<!-- bi-directional one-to-one association to Text -->
<one-to-one name="text" class="f4t.hibernate.Text" outer-join="auto" />
<!-- bi-directional one-to-one association to Html -->
<one-to-one name="html" class="f4t.hibernate.Html" outer-join="auto" />
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 7:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
oh maybe i don't understand but it seems easy.
change query to
FROM f4t.hibernate.Urls as urls WHERE urls.id = html.urlid
AND url LIKE...

you'll have a list of url, and be able to do url.getHtml()

hibernate will do the link between url and html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 7:14 am 
Beginner
Beginner

Joined: Tue Mar 23, 2004 4:18 pm
Posts: 45
delpouve wrote:
oh maybe i don't understand but it seems easy.
change query to
FROM f4t.hibernate.Urls as urls WHERE urls.id = html.urlid
AND url LIKE...

you'll have a list of url, and be able to do url.getHtml()

hibernate will do the link between url and html


net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of f4t.hibernate.Html.setUrlid

:-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 7:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
the query and mapping are ok,
just try to change your one-to-one to many-to-one...

i've read problem with one to one


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 21 posts ]  Go to page 1, 2  Next

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.