Primary key foreign key entity relationship issue

View: New views
2 Messages — Rating Filter:   Alert me  

Primary key foreign key entity relationship issue

by mvenn :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: Primary key foreign key entity relationship issue

by christopher delahunt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I'm not sure there is a problem with your mappings, the problem is that
you are not setting the CpLobProperty.lobId attribute before calling
persist.  Since this controls the CP_LOB_PROPERTY LOB_ID column, it is
still null when it goes to insert.
Unfortunately, JPA 1.0 required that only basic mappings could be Id
fields.  If you are using the latest EclipseLink nightly build, it has a
preview of the JPA 2.0 spec which allows the OneToOne mapping to be the
id field - so that you do not need
the lobId field in CpLobProperty at all.   If this is not an option, the
only solution  is to set the CpLobProperty lobId when you also set the
cpLob attribute.  This requires that the CpLob have its primary key
already set, which may require that you persist it first then call flush
before trying to persist the CpLobProprety if both are new.

Best Regards,
Chris

mvenn wrote:

> 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
>  
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users