Foriegn Key Not Updating [message #1645365] |
Mon, 02 March 2015 13:22 ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Ben Mazyopa![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=benjmaz%40gmail.com) 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 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Ben Mazyopa![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=benjmaz%40gmail.com) 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 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Chris Delahunt![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=christopher.delahunt%40oracle.com) 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.02939 seconds