Foriegn Key Not Updating [message #1645365] |
Mon, 02 March 2015 13:22 |
Ben Mazyopa Messages: 3 Registered: July 2014 |
Junior Member |
|
|
I have a problem in update data which involves foreign keys. What I want is to be able to update the foreign key value depending on the choice of the selected matching entity. (Am using glasshfish 4, JPA 2.1 and EclipseLink)
The scenario I have is, I have an Entity called Institution which has a ManyToOne association with InstutionType as shown below.
@Entity
@Table(name="Institution")
public class Institution {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id")
private Integer id;
@Column(name = "Name", length = 150, nullable = false)
private String name;
@ManyToOne
@JoinColumn(name = "InstitutionTypeId",referencedColumnName="Id",updatable=true)
private InstitutionType institutionType;
.
.
.
}
@Entity
@Table(name="InstitutionType")
public class InstitutionType {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="Id")
private int id;
@Column(name="Code",nullable=false, length=10, unique=true, updatable=false)
private String code;
@Column(name="Description",nullable=false, unique=true,length=50)
private String description;
.
.
.
}
Whenever I update the institution, code and name fields are updated, but InstutionTypeId is not updated to match the current InstitutionType. To observer what is happening, I added a logging statement in the dao and observed that the new InstutionType is passed but not persisted.
What am I missing in the code.
|
|
|
|
Re: Foriegn Key Not Updating [message #1651208 is a reply to message #1649829] |
Thu, 05 March 2015 07:05 |
Ben Mazyopa Messages: 3 Registered: July 2014 |
Junior Member |
|
|
Thanks for your response, however when I use cascade=CascadeType.MERGE I get the following exception
javax.persistence.PersistenceException: Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [id] of class [packagename.InstitutionType] is mapped to a primary key column in the database. Updates are not allowed.
Certainly, I have opted to add the log which was created when using FINEST as the logging level. What you will observe, is that the class to be persist has an InstitutionType with Id=7, however before persisting it seems it retrieves an old one with Id=5. What I want is InstitutionType with Id=5 be updated with the one with Id=7. See the log below
Partial log
2015-03-05T08:54:42.126+0200|Finest: Merge clone with references Institution [id=13, name=CCM Theological College, institutionType=InstitutionType [id=7, description=College, active=true]]
2015-03-05T08:54:42.127+0200|Finest: Register the existing object InstitutionType [id=5, description=College, active=true]
Full Log
2015-03-05T08:54:42.125+0200|Finer: client acquired: 2099166034
2015-03-05T08:54:42.126+0200|Finer: TX binding to tx mgr, status=STATUS_ACTIVE
2015-03-05T08:54:42.126+0200|Finer: acquire unit of work: 462441259
2015-03-05T08:54:42.126+0200|Finest: Merge clone with references Institution [id=13, name=CCM Theological College, institutionType=InstitutionType [id=7, description=College, active=true]]
2015-03-05T08:54:42.127+0200|Finest: Register the existing object InstitutionType [id=5, description=College, active=true]
2015-03-05T08:54:42.127+0200|Finer: TX beforeCompletion callback, status=STATUS_ACTIVE
2015-03-05T08:54:42.127+0200|Finer: begin unit of work commit
2015-03-05T08:54:42.127+0200|Finer: TX afterCompletion callback, status=COMMITTED
2015-03-05T08:54:42.127+0200|Finer: end unit of work commit
2015-03-05T08:54:42.128+0200|Finer: release unit of work
2015-03-05T08:54:42.128+0200|Finer: client released
Note that when persisting I use:
entityManager.merge(institution)
You assistance will be appreciated
[Updated on: Thu, 05 March 2015 07:42] Report message to a moderator
|
|
|
Re: Foriegn Key Not Updating [message #1651788 is a reply to message #1651208] |
Thu, 05 March 2015 14:06 |
Chris Delahunt Messages: 1389 Registered: July 2009 |
Senior Member |
|
|
The error states that EclipseLInk thinks you are attempting to change the primary key on an entity, which isn't supported. You need to check the code you are using to read in the Institution and change the institutionType reference, making sure that you are changing the reference and not just changing the associated InstitutionType instance to have a different primary key.
[Updated on: Thu, 05 March 2015 14:06] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.04923 seconds