I have a general question about mapping data in Hibernate. I think that's an easy question to answer. I just need a general idea.
I have 3 linked tables:
---------------------
table test: (stores some tests)
id - primary key
name
--------------------
table testresult: (stores the testing results for each test)
(test_id, testingdate) - composite primary key
message,
server_id
----------------------
table server: (stores the servers where the testing was performed)
(id) - primary key
servername
---------------------
I was able to build an application, which uses all this, but I see it is very inefficient.
I have classes TestBean, TestResultBean, TestResultBeanPK, ServerBean.
What I'd like to have is when I query test results, I get a full object using a single sql "select" operation.
But what I see in logs is - separate sql "select" operations are executed - to get the test result, then to get server, etc (I have other linked tables similar to this "server" table).
Code:
that's a piece of my current TestResultBean.hbm.xml file:
<many-to-one name="server" class="ServerBean"
column="server_id" lazy="false"/>
I had to use "lazy=false" to avoid "session is already closed" error because I need to display the results on a web-page,
my questions are:
1. is it possible to have a "join" construction for testresult mapping?
I see this paragraph in the tutorial, but it does not describe my situation well:
Quote:
5.1.18. join
Using the <join> element, it is possible to map properties of one class to several tables.
Unfortunately, there's no clear explanation what this "join" element should be used for.
The problem with this "join" is that when I try to apply this, I'm getting error:
"Foreign key must have same number of columns as the referenced primary key".
That's the join construction I'm trying to experiment with:
Code:
piece of TestResultBean.hbm.xml
<join table="server">
<key column="server_id"/>
<many-to-one name="server"
column="id"
not-null="true"/>
</join>-->
so, should I use this "join" if I want to get the test results to be fetched using a single sql query instead of current multiple "selects"?
if yes, then how to use it properly?
2. should I add "id" column to "testresult" table? Section "5.1.4" of the tutorial says:
Quote:
Mapped classes must declare the primary key column of the database table.
of course, I'd prefer to use my current composite ID as see no real need in new "id".