I have several questions on two tables mapping in java & Hibernate.
Maybe someone has a good link to an example, which describes a similar situation.
suppose, I have tables "TEST" and "TESTRESULT":
table "TEST"
long ig - PK
String name
table "TESTRESULT"
long test_id -PK
datetime testingdate -PK
String result
--some other string columns--
So, a test can have 0 or more test results, which are referenced by a
primary key (test_id, testingdate).
What I need to have is -
be able to get all results for a test,
add a new test result for a test,
search for results using some criterias.
I have read the tutorial, but I still do not have answers for some questions and
constantly receive java Exceptions.
my questions are:
1. should I use a "set" or a "one-to-many" association between test - testresult?
2. how do I specify that "testresult" table has two-column primary key?
do I need to create a separate class like TestResultPK and use it?
I'm using Hibernate 3.0
The log is:
Hibernate: select suitebean0_.id as id0_, suitebean0_.name as name0_ from suite suitebean0_ where suitebean0_.name=?
Hibernate: select testbean0_.id as id2_, testbean0_.name as name2_ from test testbean0_, suite_has_test suites1_, suite suitebean2_ where testbean0_.id=suites1_.test_id and suites1_.suite_id=suitebean2_.id and testbean0_.name=? and suitebean2_.id=?
test not found: hello, suite_id=5006
Hibernate: insert into test (name, id) values (?, null)
Hibernate: select tests0_.suite_id as suite1_1_, tests0_.test_id as test2_1_, testbean1_.id as id2_0_, testbean1_.name as name2_0_ from suite_has_test tests0_ left outer join test testbean1_ on tests0_.test_id=testbean1_.id where tests0_.suite_id=?
created test: hello, id=130016
Hibernate: select testresult0_.test_id as test1_3_0_, testresult0_.testingdate as testingd2_3_0_, testresult0_.result as result3_0_, testresult0_.optlevel as optlevel3_0_, testresult0_.message as message3_0_ from testresult testresult0_ where testresult0_.test_id=? and testresult0_.testingdate=?
loaded result from DB: null using key: testId=130016 testingDate=Thu Aug 30 10:00:31 NOVST 2007
Hibernate: insert into testresult (result, optlevel, message, test_id, testingdate) values (?, ?, ?, ?, ?)
Hibernate: insert into suite_has_test (suite_id, test_id) values (?, ?)
Hibernate: update testresult set id=? where test_id=? and testingdate=?
18:22:12,988 WARN JDBCExceptionReporter:71 - SQL Error: 1054, SQLState: 42S22
18:22:12,988 ERROR JDBCExceptionReporter:72 - Unknown column 'id' in 'field list'
18:22:12,998 ERROR AbstractFlushingEventListener:300 - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
Code:
<hibernate-mapping>
<class name="testresults.beans.TestBean" table="test">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="suites" table="suite_has_test">
<key column="test_id"/>
<many-to-many column="suite_id" class="testresults.beans.SuiteBean"/>
</set>
<set name="testResults" table="testresult">
<key column="id"/>
<one-to-many class="testresults.beans.TestResultBean"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="testresults.beans.TestResultBean"
table="testresult">
<composite-id name="id" class="testresults.beans.TestResultPK">
<key-property name="testId" column="test_id"/>
<key-property name="testingDate" column="testingdate"/>
</composite-id>
<property name="result"/>
<property name="optlevel"/>
<property name="message"/>
</class>
</hibernate-mapping>
Code:
public class TestResultPK implements Serializable {
private Long testId;
private Date testingDate;
public TestResultPK() {
}
public Long getTestId() {
return testId;
}
public void setTestId(Long testId) {
this.testId = testId;
}
public Date getTestingDate() {
return testingDate;
}
public void setTestingDate(Date testingDate) {
this.testingDate = testingDate;
}
public boolean equals(Object obj) {
boolean result = false;
TestResultPK anotherPK = (TestResultPK) obj;
if (
(testId.equals(anotherPK.getTestId())) &&
(testingDate.equals(anotherPK.getTestingDate()))) {
result = true;
}
return result;
}
public String toString() {
return "testId=" + testId + " testingDate=" + testingDate;
}
}
public class TestResultBean implements Serializable {
private TestResultPK id;
private String optlevel;
private String message;
private int result;
public TestResultBean() {
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getOptlevel() {
return optlevel;
}
public void setOptlevel(String optlevel) {
this.optlevel = optlevel;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
public TestResultPK getId() {
return id;
}
public void setId(TestResultPK id) {
this.id = id;
}
}
public class TestBean {
private String name;
private Long id;
private Set testResults = new HashSet();
private Set suites = new HashSet();
public TestBean() {
}
public TestBean(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Long getId() {
return id;
}
public void setName(String value) {
this.name = value;
}
public void setId(Long id) {
this.id = id;
}
public Set getTestResults() {
return testResults;
}
public void setTestResults(Set set) {
this.testResults = set;
}
public Set getSuites() {
return suites;
}
public void setSuites(Set set) {
this.suites = set;
}
}