users@glassfish.java.net

_at_many-to-one with composite key for multiple columns challenge..

From: <glassfish_at_javadesktop.org>
Date: Thu, 19 Apr 2007 15:08:15 PDT

Hi,

I am new the JPA and wondering how to solve this problem:

Thanks in advance.

buraluit

Here is the scenario:

I have following tables:

1. Table SETUP_TYPE
       VPD_ID NUMBER DEFAULT 0 NOT NULL,
       SETUP_TYPE VARCHAR2(25) NOT NULL,
       DESCRIPTION VARCHAR2(40) NOT NULL,

     (Primary Key is SETUP_TYPE + VPD_ID)

2. Table TEST (PK is ID)
    Columns:
        1) ID
        2) VPD_ID
        3) SPEC_SETUP_TYPE1 VARCHAR2(25) (FK to SETUP_TYPE on SPEC_SETUP_TYPE1 + VPD_ID)
        4) SPEC_SETUP_TYPE2 VARCHAR2(25) (FK to SETUP_TYPE on SPEC_SETUP_TYPE2 + VPD_ID)


Here is the Entity:

package test.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.famis.ejb3.entity.core.SetupTypeEntity;

@SuppressWarnings("serial")
@Entity(name = "Test")
@Table(name = "TEST")
public class TestEntity implements Serializable {

    private Long id;
    private Long vpdId;
    private SetupTypeEntity specSetupType1;
    private SetupTypeEntity specSetupType2;

    public TestEntity() {
    }

    @Id
    @Column(name = "ID")
    public Long getId() {
        return id;
    }

    public void setId(Long value) {
        this.id = value;
    }

    @Column(name = "VPD_ID", insertable=false, updatable=false)
    public Long getVpdId() {
        return vpdId;
    }

    public void setVpdId(Long value) {
        this.vpdId = value;
    }

    /*
     * Following are the two columns in the database table which has a FK to another table name SETUP_TYPE
     * whose primary key is a copmosite key as SETUP_TYPE + VPD_ID
     */
    
    @ManyToOne
    @JoinColumns( {
            @JoinColumn(name="SPEC_SETUP_TYPE01", referencedColumnName="SETUP_TYPE"),
            @JoinColumn(name="VPD_ID", referencedColumnName="VPD_ID", insertable=false, updatable=false)
            })
     public SetupTypeEntity getSpecSetupType1() {
         return specSetupType1;
     }
     
     public void setSpecSetupType1(SetupTypeEntity data) {
         specSetupType1 = data;
     }

     @ManyToOne
     @JoinColumns( {
             @JoinColumn(name="SPEC_SETUP_TYPE02", referencedColumnName="SETUP_TYPE"),
             @JoinColumn(name="VPD_ID", referencedColumnName="VPD_ID", insertable=false, updatable=false)
             })
     public SetupTypeEntity getSpecSetupType2() {
         return specSetupType2;
     }
     
     public void setSpecSetupType2(SetupTypeEntity data) {
         this.specSetupType2 = data;
     }
}

Problems:

When I load up the data and try to create it:

1. This entity doesn’t insert any of SPEC_SETUP_TYPEs because of the insertable=false.
2. And if I take off the insertable=false then Toplink complains:
[TEST.VPD_ID]. Only one may be defined as writable, all others must be specified read-only.

This seems to me something very simple but I am not able to figure this out as a beginner.
[Message sent by forum member 'buraluit' (buraluit)]

http://forums.java.net/jive/thread.jspa?messageID=213276