java


Is it possible to retrieve an object that is split into three tables with only one query?


EDIT: The answers works, but slow down the query a lot. Is there another solution?
I have the following object in my program.
Page haves various TextBlock that haves various Token.
The three are stored in the database in three tables with the same namas of the objects.
I want to know if is possible to recover all the data needed to fill the object in the program with only one query.
I want only one query because I'm using a CURSOR to iterate over all the objects stored in the database.
The database scheme is this:
Page(id, someAttributes)
TextBlock(id,pageId, someAttributes)
Token(textblockId, someAttributes)
Assuming that every Page has at least one Textblock with at least one Token each try
SELECT * FROM
Page AS P
INNER JOIN TextBlock T ON T.pageId = P.id
INNER JOIN Token TK ON TK.textblockId = T.id;
Alternatively (old syntax):
SELECT * FROM
Page AS P, TextBlock T, Token TK WHERE
T.pageId = P.id AND
TK.textblockId = T.id;
IF it is possible that the above condition is not always true then you need to use OUTER JOIN instead of INNER JOIN.
Yes it is possible with a 3 way join.
You'll get one row per Token in this case.
So you'll need to loop and fetch all rows from the cursor and appropriately create the objects as you see new Page ids, TextBlock ids, etc.
NOTE: You'll need to use outer joins, in case a TextBlock has no Tokens, so the TextBlock would still be retrieved (with null values for Token attributes). Same for Pages with no TextBlock.
Select
Page.someAttributes,
TextBlock.someAttributes,
Token.someAttributes
From
Page,
TextBlock,
Token
Where
Page.id = TextBlock.pageId
AND
TextBlock.id = Token.textblockId
Group By
Page.id

Related Links

How can I best initialize constants from a database or servlet context?
JPOX JDO and Castor JDO compared to Hibernate persistence
Running 2 threads simultaneously
ORA-01654: unable to extend index
Why does my Java class fail to compile when compiled from inside the package directory?
jpa inheritance and a OneToMany Relation
Is there a way to display a HSSFWorkbook Object in a JSP page?
Reading from Java JTextArea
Java NIO: Relationship between OP_ACCEPT and OP_READ?
Is there a SIMPLE example of How to use buckminster
Using subclassing to replace a Java class that doesn't implement an interface
Multiple rows per record in JSF?
DataTable containing inputText: is it possible with JSF Custom Component
How do I use MiG Layout with Eclipse?
retrieve matlab result in java
Dom4j rule does not match all expected nodes

Categories

HOME
deployment
keycloak
activiti
drivers
fft
raspberry-pi
grep
objectgears
getelementsbytagname
filtering
google-oauth
elm
paw-app
amortized-analysis
serverspec
opengl-es-2.0
azure-media-services
autotools
gorm
kibana-4
tomcat6
spring-tool-suite
n-gram
hex-editors
dosgi
jqwidget
textfield
swingx
chromium-embedded
format-specifiers
android-widget
sparse-matrix
django-storage
virtualdub
primitive
nat
http-get
vsts-build-task
hybridauth
language-concepts
http-referer
mmenu
hexo
bytecode-manipulation
appfabric
serve
web-mining
swift3.0.2
abstract-class
c11
sqlite2
angularjs-factory
lumberjack
tasker
trim
android-tabhost
reportingservices-2005
node-gyp
celery-task
datastax-startup
diagnostics
feeds
windows-mobile-6.5
lync-client-sdk
url-pattern
infix-notation
intrusion-detection
contact-list
pagedlist
prerequisites
teamcity-8.0
django-unittest
rgeo
browser-bugs
ideamart
twirl
php-ci
vstest.console.exe
ami
system32
graph-api-explorer
apache-commons-net
typekit
coldbox
cdt
markers
jboss-weld
resty-gwt
java.nio.file
multiprocessor
ruby-datamapper
ftps
batterylevel
back-stack
ocunit
datawindow
qtkit
netbeans-6.9
stage
cxxtest
dmx512
brewmp
sustainable-pace
zend-decorators
ajax-forms

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App