users@glassfish.java.net

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

From: Markus Fuchs <Markus.Fuchs_at_Sun.COM>
Date: Fri, 20 Apr 2007 10:33:37 -0700

Hi buraluit,

I'm afraid you can't map TEST.VPD_ID to two different relationships. Can
you change the TEST table as in:

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

Then map the relationship to the two non-overlapping column pairs. You
can use your application logic to ensure that VPD_ID1 and VPD_ID2 always
have the same value, e.g.:

     public void setSpecSetupType1(SetupTypeEntity data) {

         if (specSetupType2 != null && data != null) {
            if (specSetupType2.getVpdId() == data.getVpdId()) {
                specSetupType1 = data;
            } else {
                // throw Exception
            }
         }
     }

... similar for setSpecSetupType2.

HTH,

-- markus.

glassfish_at_javadesktop.org wrote:
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>