Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Complex Key: Multiple writable mappings exist;(Cannot figure out how to correct this error... Please help. :))
Complex Key: Multiple writable mappings exist; [message #1052880] Tue, 30 April 2013 23:26 Go to next message
Michael Gardner is currently offline Michael GardnerFriend
Messages: 4
Registered: April 2013
Junior Member
Hello,

I am new to JPA, but I have reviewed the documentation and still cannot figure out how to resolve the following error. I would really appreciate your help to figure out this error.

Thanks in advance for your time and comments,

Mike

-------------------------------------------
Multiple writable mappings exist for the field [Licenses.CustomerId]. Only one may be defined as writable, all others must be specified read-only. Mapping: org.eclipse.persistence.mappings.ManyToOneMapping[customerVendorProduct] Descriptor: RelationalDescriptor(entities.License --> [DatabaseTable(Licenses)]) Runtime Exceptions:
-------------------------------------------


Here are the relationships that are involved:
* is the primary key.

Customer (1) ---- (n) License (n) ---- (1) CustomerVendorProduct
  *CustomerId           *LicenseId           *CustomerId+*VendorId+*ProductId
                         CustomerId (fk)


Here is the relavant code for each entity:

----- CUSTOMER ENTITY ------------------------------
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "CustomerId")
    private Long customerId;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
private Collection<CustomerVendorProduct> customerVendorProductCollection;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
private Collection<License> licenseCollection;
---------------------------------------------------


----- CUSTOMERVENDORPRODUCT ENTITY ----------------
    @EmbeddedId
    protected CustomerVendorProductPK customerVendorProductPK;

@JoinColumn(name = "VendorId", referencedColumnName = "VendorId", 
    insertable = false, updatable = false)
@ManyToOne(optional = false)
private Vendor vendor;

@JoinColumn(name = "ProductId", referencedColumnName = "ProductId", 
    insertable = false, updatable = false)
@ManyToOne(optional = false)
private Product product;

@JoinColumn(name = "CustomerId", referencedColumnName = "CustomerId", 
    insertable = false, updatable = false)
@ManyToOne(optional = false)
private Customer customer;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "customerVendorProduct")
private Collection<License> licenseCollection;
---------------------------------------------------


---- LICENSE ENTITY -------------------------------
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "LicenseId")
    private Long licenseId;

@JoinColumns({
  @JoinColumn(name = "CustomerId", referencedColumnName = "CustomerId"),
  @JoinColumn(name = "VendorId", referencedColumnName = "VendorId"),
  @JoinColumn(name = "ProductId", referencedColumnName = "ProductId")})
  
@ManyToOne(optional = false)
private CustomerVendorProduct customerVendorProduct;

@JoinColumn(name = "CustomerId", referencedColumnName = "CustomerId")
@ManyToOne(optional = false)
private Customer customer;
----------------------------------------------------
Re: Complex Key: Multiple writable mappings exist; [message #1052946 is a reply to message #1052880] Wed, 01 May 2013 14:15 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
The License entity is using the CustomerId column in two mappings; the ManyToOne to Customer and CustomerVendorProduct, but only one mapping should be able write to the field. So you will need to mark one of the relationships as read-only, by marking the join-column as insertable=false, updateable=false just as you have done in the CustomerVendorProduct entity for the JoinColums mapped in both relations and EmbeddedId.

Also note that in JPA 2.0, relationships are allowed to be the primary key. You can mark the relationships within CustomerVendorProduct with @MapsId and point them to the fields within the EmbeddedId, and have JPA set the values in the EmbeddedId automatically from the values in the relationships. Or you can do away with having an EmbeddedId at all, and mark the ManyToOnes with @Id, using a primaryKeyclass instead of EmbeddedId. A simple example of the later is here http://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/DerivedIdentifiers
while http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/mappedbyid has more descriptive @MapsId examples.

Best Regards,
Chris

Re: Complex Key: Multiple writable mappings exist; [message #1052995 is a reply to message #1052946] Wed, 01 May 2013 19:15 Go to previous message
Michael Gardner is currently offline Michael GardnerFriend
Messages: 4
Registered: April 2013
Junior Member
Hi Chris,

Thank you very much for taking the time to send such a complete and helpful response! I really appreciate it. I am going to make the changes that you suggested and things should work properly. The next step will be for me to take a look at the alternatives that you've suggested in the second paragraph.

Thank you again for your help!

Mike
Previous Topic:Exception in thread "main" java.lang.ExceptionInInitializerError
Next Topic:How to refresh the entities with out re starting the server
Goto Forum:
  


Current Time: Sun Dec 21 00:30:16 GMT 2014

Powered by FUDForum. Page generated in 0.02292 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software