Setting property-ref="server" was my first attempt, but I tried again with the same result. Let me expand a little bit on what I'm doing, and show in more detail the clauses hibernate is generating:
An Organization owns zero or more Servers. A Server may have zero or one Agent (although the database model supports a one-to-many relationship; for other reasons Server and Agent do not have a shared primary key, the primary reason is that in the future the limitation of a single Agent per Server may be lifted). I'll provide the complete mapping files:
Organization
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.twocoast.tcsc.model.Organization" table="SUPPORTED_ORGANIZATION">
<id name="organizationId" column="ORGANIZATION_ID" type="long" unsaved-value="0">
<generator class="sequence">
<param name="sequence">OBJECT_SEQ</param>
</generator>
</id>
<property name="organizationPrefix" column="ORGANIZATION_PREFIX"/>
<property name="organizationShortName" column="ORGANIZATION_SHORT_NAME"/>
<property name="organizationLegalName" column="ORGANIZATION_LEGAL_NAME"/>
<property name="isActive" column="IS_ACTIVE" type="yes_no"/>
<property name="isRootOrganization" column="IS_ROOT_ORGANIZATION" type="yes_no"/>
<set name="addresses" inverse="true" lazy="true" cascade="all" order-by="address_type asc">
<key column="organization_id"/>
<one-to-many class="com.twocoast.tcsc.model.Address"/>
</set>
<set name="siteUsers" inverse="true" lazy="true" cascade="all" order-by="last_name asc">
<key column="organization_id"/>
<one-to-many class="com.twocoast.tcsc.model.SiteUser"/>
</set>
<set name="servers" inverse="true" lazy="true" cascade="all" order-by="host_name asc">
<key column="organization_id"/>
<one-to-many class="com.twocoast.tcsc.model.Server"/>
</set>
</class>
</hibernate-mapping>
ServerCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.twocoast.tcsc.model.Server" table="SERVER">
<id name="serverId" column="SERVER_ID" type="long" unsaved-value="0">
<generator class="sequence">
<param name="sequence">OBJECT_SEQ</param>
</generator>
</id>
<property name="hostName" column="HOST_NAME"/>
<property name="domainName" column="DOMAIN_NAME"/>
<property name="physicalLocation" column="PHYSICAL_LOCATION"/>
<property name="isActive" column="IS_ACTIVE" type="yes_no"/>
<property name="isDbServer" column="IS_DB_SERVER" type="yes_no"/>
<property name="isWebServer" column="IS_WEB_SERVER" type="yes_no"/>
<one-to-one name="agent" class="com.twocoast.tcsc.model.Agent" property-ref="server"/>
</class>
</hibernate-mapping>
AgentCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.twocoast.tcsc.model.Agent" table="AGENT">
<id name="agentId" column="AGENT_ID" type="long" unsaved-value="0">
<generator class="sequence">
<param name="sequence">AGENT_SEQ</param>
</generator>
</id>
<property name="smtpHost" column="SMTP_HOST"/>
<property name="rmiPort" column="RMI_PORT"/>
<property name="threadCount" column="THREAD_COUNT"/>
<property name="startupDelay" column="STARTUP_DELAY"/>
<property name="agentState" column="AGENT_STATE"/>
<property name="agentVersion" column="AGENT_VERSION"/>
<property name="lastMessage" column="LAST_MESSAGE"/>
<property name="startedOn" column="STARTED_ON"/>
<property name="isActive" column="IS_ACTIVE" type="yes_no"/>
<many-to-one name="server" class="com.twocoast.tcsc.model.Server" column="SERVER_ID"/>
</class>
</hibernate-mapping>
When an server for a given organization is loaded from the database, hibernate generates the following where clause:
where server0_.organization_id=? and server0_.SERVER_ID=agent1_.AGENT_ID(+)
Again, this should be:
where server0_.organization_id=? and server0_.SERVER_ID=agent1_.SERVER_ID(+)
Then, after discovering that the Server's Agent is 'null', hibernate tries to instantiate the Agent, generating the following where clause:
where agent0_.AGENT_ID=? and agent0_.SERVER_ID=server1_.SERVER_ID(+)
This clause is correct, however, the value for the bind variable that hibernate chooses is not; it binds the primary key value from Server. Does that shed any more light on what could be causing the problem?
Thanks again...