-->
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.  [ 2 posts ] 
Author Message
 Post subject: Joins
PostPosted: Mon Aug 11, 2014 1:02 pm 
Newbie

Joined: Thu Mar 28, 2013 11:06 am
Posts: 8
Hi,

I've just a problem with HQL and joins. I've used hibernate to create the classes and mappings from an existing database.

I've two tables where I want to retrieve data from, SubjectBox and SubBox. SubBox contains a foreinkey from SubjectBox. But there are several other tables involved. SubjectBox keeps the data and in SubBox you can find information if an entry in SubjectBox has a parent which is also an entry in SubjectBox:

e.g:
name1 (parent)
- name2 (children of name1)
- name3 (children of name1)

structure of the tables:
Code:
--
-- Tabellenstruktur für Tabelle `SUBJECT_BOX`
--

CREATE TABLE IF NOT EXISTS `SUBJECT_BOX` (
  `ID` varchar(40) NOT NULL,
  `CREATORUSERID` varchar(40) DEFAULT NULL,
  `AUTHORITYID` varchar(40) DEFAULT NULL,
  `SUBJECTBOXTYPEID` varchar(40) NOT NULL,
  `CREATIONTIME` datetime NOT NULL,
  `MODIFICATIONTIME` datetime NOT NULL,
  `NAME` varchar(255) NOT NULL,
  `ORIGINALSUBJECTBOXID` varchar(40) NOT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `UK_t42g8skasnmwamlbi4x6ngg8c` (`NAME`),
  KEY `FK_qq1x26taqmg18893rsn55m6ej` (`CREATORUSERID`),
  KEY `FK_ir380cdhmlb2rbdfh075bp8np` (`AUTHORITYID`),
  KEY `FK_d0w8mgy95lkjldnoo1oifyk0d` (`SUBJECTBOXTYPEID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Constraints der exportierten Tabellen
--

--
-- Constraints der Tabelle `SUBJECT_BOX`
--
ALTER TABLE `SUBJECT_BOX`
  ADD CONSTRAINT `FK_d0w8mgy95lkjldnoo1oifyk0d` FOREIGN KEY (`SUBJECTBOXTYPEID`) REFERENCES `SUBJECT_BOX_TYPE` (`ID`),
  ADD CONSTRAINT `FK_ir380cdhmlb2rbdfh075bp8np` FOREIGN KEY (`AUTHORITYID`) REFERENCES `AUTHORITY` (`ID`),
  ADD CONSTRAINT `FK_qq1x26taqmg18893rsn55m6ej` FOREIGN KEY (`CREATORUSERID`) REFERENCES `USER` (`ID`);

--
-- Tabellenstruktur für Tabelle `SUB_BOX`
--

CREATE TABLE IF NOT EXISTS `SUB_BOX` (
  `CHILDID` varchar(40) NOT NULL,
  `PARENTID` varchar(40) NOT NULL,
  PRIMARY KEY (`CHILDID`,`PARENTID`),
  KEY `FK_1hxmwffq9gpry0rlnarym9f8m` (`PARENTID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Constraints der exportierten Tabellen
--

--
-- Constraints der Tabelle `SUB_BOX`
--
ALTER TABLE `SUB_BOX`
  ADD CONSTRAINT `FK_1hxmwffq9gpry0rlnarym9f8m` FOREIGN KEY (`PARENTID`) REFERENCES `SUBJECT_BOX` (`ID`);



this resulted in 3 classes, SubjectBox, SubBox and SubBoxId
and mapping files for SubjectBox and SubBox

SubBox.hbm.xml looks like:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 11.06.2014 17:42:29 by Hibernate Tools 4.0.0 -->
<hibernate-mapping>
   <class name="xxx.db.hibernate.SubBox" table="SUB_BOX"
      catalog="gamedb">
      <composite-id name="id"
         class="xxx.db.hibernate.SubBoxId">
         <key-property name="childid" type="string">
            <column name="CHILDID" length="40" />
         </key-property>
         <key-property name="parentid" type="string">
            <column name="PARENTID" length="40" />
         </key-property>
      </composite-id>
      <many-to-one name="subjectBox"
         class="xxx.db.hibernate.SubjectBox" update="false"
         insert="false" fetch="select">
         <column name="PARENTID" length="40" not-null="true" />
      </many-to-one>
   </class>
</hibernate-mapping>


The sql query I want to use looks like:
Code:
"select SUBJECT_BOX.id, SUB_BOX.CHILDID from SUBJECT_BOX
         left join SUB_BOX on SUBJECT_BOX.ID = SUB_BOX.CHILDID
         where SUB_BOX.CHILDID is NULL


I've tried to use Criteria as for other simple queries, but I'm not familiar enough with all stuff to so I ended up in trying to use HQL. I'm now a little bit confused because the output of my query looks like:
Code:
QueryImpl(from SubjectBox subject, SubBox box left join box.id on subject.id = box.id.childid)

but the query hibernate created contains a cross join
Code:
Hibernate:
    select
        subjectbox0_.ID as ID1_14_0_,
        subbox1_.CHILDID as CHILDID1_16_1_,
        subbox1_.PARENTID as PARENTID2_16_1_,
        subjectbox0_.CREATORUSERID as CREATORU2_14_0_,
        subjectbox0_.AUTHORITYID as AUTHORIT3_14_0_,
        subjectbox0_.SUBJECTBOXTYPEID as SUBJECTB4_14_0_,
        subjectbox0_.CREATIONTIME as CREATION5_14_0_,
        subjectbox0_.MODIFICATIONTIME as MODIFICA6_14_0_,
        subjectbox0_.NAME as NAME7_14_0_,
        subjectbox0_.ORIGINALSUBJECTBOXID as ORIGINAL8_14_0_
    from
        gamedb.SUBJECT_BOX subjectbox0_ cross
    join
        gamedb.SUB_BOX subbox1_

and the result contains all entries of both tables. But I only want to get the entry "name1".
Could anybody give me a hint what I can do to solve my problem?
(with existing database and generated code please ... no changes possible ... )

best regards
re


Top
 Profile  
 
 Post subject: Re: Joins
PostPosted: Tue Aug 12, 2014 11:28 am 
Newbie

Joined: Thu Mar 28, 2013 11:06 am
Posts: 8
OK, I've figured out some mistakes I've made, but still got not the result I want to have.

I've used:
Code:
            final String queryString = "select subject.id from SubjectBox subject "
                    + " left join subject.subBoxes as box "
                    + " left join box.id as boxids";
            Query query = session.createQuery( queryString );
            query.list();


which creates a query:
Code:
    select
        subjectbox0_.ID as col_0_0_
    from
        gamedb.SUBJECT_BOX subjectbox0_
    left outer join
        gamedb.SUB_BOX subboxes1_
            on subjectbox0_.ID=subboxes1_.PARENTID

As I understood, this results from the mapping in SubBox.hbm.xml where PARENTID is defined with many-to-one to be fetched by select.
So far I'm using native SQLQuery to get my result. If somebody has another idea ... please tell me.

Best regards


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