-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: yet another one-to-one mapping question
PostPosted: Fri Feb 27, 2004 9:22 am 
Newbie

Joined: Mon Sep 15, 2003 9:09 am
Posts: 9
I have two tables, SERVER and AGENT. The database model supports multiple agents per server, using a foreign key:

SERVER
---------
SERVER_ID
HOST_NAME
...

AGENT
--------
AGENT_ID
SERVER_ID
SMTP_HOST
RMI_PORT
...

However, in our current application I want to make this a defacto one-to-one relationship. I've defined the hibernate 2.0 mappings as follows:

Code:
<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"/>
    <one-to-one name="agent" class="com.twocoast.tcsc.model.Agent" property-ref="serverId"/>
  </class>
</hibernate-mapping>

<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"/>
    <many-to-one name="server" class="com.twocoast.tcsc.model.Server" column="SERVER_ID"/>
  </class>
</hibernate-mapping>


When I load a Server from the database, Hibernate's query contains this clause in the predicate:

server0_.SERVER_ID=agent4_.AGENT_ID(+)

Which means that a null agent (or the wrong agent) is returned. The correct clause is:

server0_.SERVER_ID=agent4_.SERVER_ID(+)

I thought the property-ref attribute in the one-to-one element is what controlls this behaviour. I've tried both of these values with the same result:

property-ref="serverId"
property-ref="server"

I'm sure I'm missing something obvious here, but am stumped. Any help is much appreciated. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 10:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Are you sure you tried property-ref="server" in exaclty the mapping you posted? This should definately work, please try again ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 11:35 am 
Newbie

Joined: Mon Sep 15, 2003 9:09 am
Posts: 9
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>

Server
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.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>

Agent
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.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...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 11:38 am 
Newbie

Joined: Mon Sep 15, 2003 9:09 am
Posts: 9
Whoops, I posted an abbreviated version of the Server mapping. The mapping I'm using really looks like this:

Code:

<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"/>
    <many-to-one name="organization" class="com.twocoast.tcsc.model.Organization" column="ORGANIZATION_ID" not-null="true"/>
    <many-to-one name="productionLevel" class="com.twocoast.tcsc.model.ProductionLevel" column="PRODUCTION_LEVEL_ID" not-null="true"/>
    <many-to-one name="timeZone" class="com.twocoast.tcsc.model.TimeZone" column="TIME_ZONE_ID" not-null="true"/>
    <one-to-one name="agent" class="com.twocoast.tcsc.model.Agent" property-ref="server"/>
  </class>
</hibernate-mapping>


Sorry for the confusion.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.