« Return to Thread: Primary key foreign key entity relationship issue

Primary key foreign key entity relationship issue

by mvenn :: Rate this Message:

Reply to Author | View in Thread

Hello,

I have an entity mapping scenario I'm not sure how to handle.  

Tables:
CP_LOB
lob_id - PK - Identity
...

CP_LOB_PROPERTY
lob_id - PK & FK (from cp_lob.lob_id)
...

Entities:
@Entity
@Table(name = "CP_LOB")
public class CpLob extends BaseDomain {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "LOB_ID", unique = true, nullable = false, precision = 11, scale = 0)
    private Long lobId;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "cpLob")
    @PrivateOwned
    private CpLobProperty cpLobProperty;
        ...
}


@Entity
@Table(name = "CP_LOB_PROPERTY")
public class CpLobProperty extends BaseDomain {
    @Id
    @Column(name = "LOB_ID", unique = true, nullable = false, precision = 11, scale = 0)
    private Long lobId;
   
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @PrivateOwned
    @JoinColumn(name = "LOB_ID", unique = true, nullable = false, insertable = false, updatable = false)
    private CpLob cpLob;
        ...
}

When I try to persist a CpLob object containing a CpLobProperty I get the following error:
        2009-07-08 11:50:00,166 ERROR com.gmrc.cpp.struts.actions.CppBaseAction (processException:1089) {Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 1.1.1.v20090430-r4097): org.eclipse.persistence.exceptions.DatabaseException
        Internal Exception: java.sql.SQLException: [SQL0407] Null values not allowed in column or variable LOB_ID.
        Error Code: -407
        Call: INSERT INTO CP_LOB_PROPERTY (LOB_ID, WIND_HAIL_DEDUCT, CO_INSURANCE, TARGET_MARKET, INFLATION_GUARD, DEDUCTIBLE, VALUE_RPT_FORM, EQUIPMENT_BREAKDOWN, VALUATION, THEFT, CAUSE_OF_LOSS) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        bind => [null, null, null, , null, null, , Y, null, null, null]

If I take off the readonly attribute for CpLobProperty.cpLob I this error:
        Exception [EclipseLink-48] (Eclipse Persistence Services - 1.1.1.v20090430-r4097): org.eclipse.persistence.exceptions.DescriptorException
        Exception Description: Multiple writable mappings exist for the field [CP_LOB_PROPERTY.LOB_ID].  Only one may be defined as writable, all others must be specified read-only.
        Mapping: org.eclipse.persistence.mappings.OneToOneMapping[cpLob]
        Descriptor: RelationalDescriptor(com.gmrc.jpa.domain.CpLobProperty --> [DatabaseTable(CP_LOB_PROPERTY)])
                at org.eclipse.persistence.exceptions.DescriptorException.multipleWriteMappingsForField(DescriptorException.java:976)
                at org.eclipse.persistence.internal.descriptors.ObjectBuilder.initialize(ObjectBuilder.java:2310)
                ...
               

What is the property way to annotate CpLobProperty to get around these errors?

Thanks for your help,
Matt

 « Return to Thread: Primary key foreign key entity relationship issue