I tried this pattern, but when inserting records in the primary (not lookup)
table I get a constraint violation because Hibernate is trying to insert
null for the foreign key to the lookup.
Error:
Code:
Hibernate:
/* get current state com.wholefoods.ittoolkit.ws.code.ReqState */ select
reqstate_.REQ_STATE_ID,
reqstate_.DESCRIPTION as DESCRIPT2_12_
from
ITTOOLKIT.CODE_REQ_STATE reqstate_
where
reqstate_.REQ_STATE_ID=?
Hibernate:
/* get current state com.wholefoods.ittoolkit.ws.ccf.ReqTeam */ select
reqteam_.TEAM_ID,
reqteam_.NAME as NAME6_
from
ITTOOLKIT.CR_TEAM reqteam_
where
reqteam_.TEAM_ID=?
Hibernate:
/* insert com.wholefoods.ittoolkit.ws.ccf.Request
*/ insert
into
ITTOOLKIT.CR_REQUEST
(VERSION, ALT_ID, REQ_STATE_ID, BREAK_FIX, TITLE, DESCRIPTION, URGENCY, REQ_TEAM_ID, REQ_USER_ID, TEST_USER_ID, PROJECT_NAME, START_DATE, SCHED_DATE, MIG_PATH_FROM, MIG_PATH_TO, ON_COMPL_NOTIFY, CHANGE_DOC, TEST_DOC, BACKOUT_DOC, CHANGE_TS, CHANGE_USER, REQ_ID)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
11:48:15,765 WARN org.hibernate.util.JDBCExceptionReporter:77 - SQL Error: 1400, SQLState: 23000
11:48:15,781 ERROR org.hibernate.util.JDBCExceptionReporter:78 - ORA-01400: cannot insert NULL into ("ITTOOLKIT"."CR_REQUEST"."REQ_STATE_ID")
11:48:15,781 WARN org.hibernate.util.JDBCExceptionReporter:77 - SQL Error: 1400, SQLState: 23000
11:48:15,781 ERROR org.hibernate.util.JDBCExceptionReporter:78 - ORA-01400: cannot insert NULL into ("ITTOOLKIT"."CR_REQUEST"."REQ_STATE_ID")
Main table mapping:
Code:
<hibernate-mapping package="com.wholefoods.ittoolkit.ws.ccf">
<class name="Request" table="CR_REQUEST">
<id name="reqId" column="REQ_ID" type="long">
<generator class="sequence">
<param name="sequence">CR_REQUEST_ID</param>
</generator>
</id>
.
.
.
<many-to-one name="reqState" column="REQ_STATE_ID" class="com.wholefoods.ittoolkit.ws.code.ReqState" />
.
.
.
</class>
</hibernate-mapping>
Lookup table mapping:
Code:
<hibernate-mapping package="com.wholefoods.ittoolkit.ws.code">
<class name="ReqState" table="CODE_REQ_STATE">
<id name="id" column="REQ_STATE_ID"/>
<property name="description" column="DESCRIPTION" not-null="true" />
</class>
</hibernate-mapping>
Calling Java:
Code:
Session sess = HibernateUtil.getSessionFactory().openSession();
Transaction tx = sess.beginTransaction();
Request req = new Request();
.
.
.
req.setReqState(new ReqState(1));
.
.
.
sess.save(req);
tx.commit();
Request.java
Code:
package com.wholefoods.ittoolkit.ws.ccf;
import java.util.*;
import com.wholefoods.ittoolkit.ws.code.ReqState;
public class Request
{
.
.
.
private ReqState reqState = null;
.
.
.
public ReqState getReqState()
{
return reqState;
}
public void setReqState(ReqState reqState)
{
this.reqState = reqState;
}
.
.
.
}
ReqState.java
Code:
package com.wholefoods.ittoolkit.ws.code;
public class ReqState
{
private int id = 0;
private String description = null;
public ReqState() { }
public ReqState(int id)
{
this.id = id;
}
public ReqState(int id, String description)
{
this.id = id;
this.description = description;
}
.
.
.
}