Hi,

I want to create a very simple one-to-one relationship between two
classes - 'Employee' and 'MedicalRecord' -  i.e one employee has one
medical record. I have created two database tables - 'medicalrecord' and
'employee'. The 'employee' table has a primary key called 'employeeId'.
Medical records are linked to employees via a foreign key in the
'medicalrecord' table, also called 'employeeId'.

HERE ARE MY CLASS DEFINITIONS:-

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public class MedicalRecord
{
    private int medicalRecordId;
    private int employeeId;

   + other fields

    public int getMedicalRecordId() {
        return medicalRecordId;
    }

     public void setMedicalRecordId(int theId) {
         medicalRecordId = theId;
    }

    public int getEmployeeId()
    {
         return employeeId;
    }

    public void setEmployeeId(int theId) {
        employeeId = theId;
    }

    + other public accessor methods

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

public class Employee
{
    private int employeeId;
    private MedicalRecord medicalRecord;

    + other fields

    public void setEmployeeId(int CustId) {
        employeeId=CustId;
    }

    public int getEmployeeId() {
        return employeeId;
    }

     public MedicalRecord getMedicalRecord() {
        return medicalRecord;
    }

    public void setMedicalRecord(MedicalRecord mr)  {
        medicalRecord = mr;
    }

    + other public accessor methods

}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

HERE ARE MY MAPPINGS:-

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

  <!--  Mapping for MedicalRecord  -->
  <class name="MedicalRecord" identity="medicalRecordId"
key-generator="IDENTITY">
    <description MedicalRecord definition</description>
    <map-to table="medicalrecord" xml="medicalrecord" />

    <field name="medicalRecordId" type="integer">
      <sql name="medicalRecordId" type="integer" />
      <xml name="medicalRecordId" node="attribute"/>
    </field>

   <field name="employeeId" type="integer">
      <sql name="employeeId" type="integer" />
      <xml name="employeeId" node="attribute"/>
   </field>

  + other field definitions

</class>

   <!--  Mapping for Employee  -->
  <class name="Employee" identity="employeeId" key-generator="IDENTITY">

    <description>Employee definition</description>
    <map-to table="employee" xml="project" />

    <field name="employeeId" type="integer">
      <sql name="employeeId" type="integer" />
      <xml name="employeeId" node="attribute"/>
    </field>

    <field name="medicalRecord" type="MedicalRecord">
      <sql name="employeeId" />
      <xml name="medicalRecord" node="element" />
    </field>

    + other field definitions

</class>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Here is a code snippet that first retrieves a specific Employee, creates
a new MedicalRecord, links it to the Employee, and then persists it:-

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Employee      employee;
    MedicalRecord   medicalRecord;
    OQLQuery      employeeOql;
    QueryResults  employees;

     employeeOql = db.getOQLQuery("SELECT e from Employee e WHERE
employeeId=$1");
     employeeOql.bind(53);
     employees =  employeeOql.execute();

     while(employees.hasMore()) {
         employee = (Employee)employees.next();

         // Create a new MedicalRecord object associated with the
Employee and persist it
         medicalRecord = new MedicalRecord();
         medicalRecord.setEmployeeId(employee.getEmployeeId());

         +  set other medicalRecord properties

         db.create(medicalRecord);
     }

     db.commit(); // Line of code that throws exception.
     db.close();

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Execution of this code results in the following stack trace:-

java.sql.SQLException: [SQLDEV]Column name 'employeeId' appears more
than once in the result column list. at com.inet.tds.a.a(Unknown Source)
at com.inet.tds.b.do(Unknown Source) at
com.inet.tds.b.executeUpdate(Unknown Source) at
org.exolab.castor.jdo.engine.SQLEngine.store(SQLEngine.java:849) at
org.exolab.castor.persist.ClassMolder.store(ClassMolder.java:1544) at
org.exolab.castor.persist.LockEngine.store(LockEngine.java:745) at
org.exolab.castor.persist.TransactionContext.prepare(TransactionContext.java:1162)
at
org.exolab.castor.jdo.engine.DatabaseImpl.commit(DatabaseImpl.java:498)
at
com.fc.fortunecookie.servlets.EmployeeServlet.service(EmployeeServlet.java:104)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at
org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:503)
at
org.apache.tomcat.core.ContextManager.service(ContextManager.java:559)
at
org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:160)
at
org.apache.tomcat.service.TcpConnectionThread.run(SimpleTcpEndpoint.java:338)
at java.lang.Thread.run(Thread.java:484)

The line in the trace
"com.fc.fortunecookie.servlets.EmployeeServlet.service(EmployeeServlet.java:104)"
corresponds to the "db.commit()" line of code above, which is causing
the exception.

Can anyone tell me if I am going about this relation in the right way in
terms of my class mapping  ? I am doing something fundamentally wrong or
missing something here ?

Am I right to use:

<field name="medicalRecord" type="MedicalRecord">
      <sql name="employeeId" />
      <xml name="medicalRecord" node="element" />
</field>

in my mapping file ?

I have other 1:1 relations set up in the same way inside my Employee
mapping that also use "employeeId" primary/foreign key relationship to
link together the tables (in this case 'employee' with 'bankaccount'.
For example:

<field name="bankAccount" type="com.fc.fortunecookie.beans.BankAccount">

      <sql name="employeeId" />
      <xml name="bankAccount" node="element" />
</field>

I can get one-to-many relationships working no problem but can't seem to
get any of my 1:1 mappings to work (i.e. to commit). Can I only use the
"employeeId" foreign key name once in another (i.e.  not 'employee')
table ?

Any help would be greatly appreciated.

Regards,

Graeme

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to