Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Foriegn Key Not Updating(ElipseLink is not modifying the foreign key)
Foriegn Key Not Updating [message #1645365] Mon, 02 March 2015 13:22 Go to next message
Ben Mazyopa is currently offline Ben MazyopaFriend
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 #1649829 is a reply to message #1645365] Wed, 04 March 2015 14:30 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Can you show with a code example what you are doing and how you are merging your changes? Is the institutionType reference pointing to an entirely different InstitutionType instance, or is it that changes to the referenced instance are not being picked up?

If it is the later, you should know what merge will only cascade through the object graph over references marked with the 'cascade=CascadeType.MERGE' . Adding this to your mapping might help give you the behavior you are looking for.

Re: Foriegn Key Not Updating [message #1651208 is a reply to message #1649829] Thu, 05 March 2015 07:05 Go to previous messageGo to next message
Ben Mazyopa is currently offline Ben MazyopaFriend
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 messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
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

icon14.gif  Re: Foriegn Key Not Updating [message #1651816 is a reply to message #1651788] Thu, 05 March 2015 14:29 Go to previous message
Ben Mazyopa is currently offline Ben MazyopaFriend
Messages: 3
Registered: July 2014
Junior Member
Thanks Chris for your precise solution. It has now worked.

Certainly, I had all be belief that the change of the Id field for an InstitutionType would effect the change. Following you suggestion, I retrieved the Institution object, stored the InstitutionTypeId to a temporary variable, then created a new InstitutionType and set its Id using the old Id. Thereafter the institution object is bound to the user interface for modifications.

See the code below

public Institution getInstitution() {
		if(institution==null){
			institution=new Institution();
			institution.setInstitutionType(new InstitutionType());
			institution.setAddress(new Address());
		}
		
		if(institution.getInstitutionType().getId()!=0){
			int oldId=institution.getInstitutionType().getId();
			institution.setInstitutionType(new InstitutionType());
			institution.getInstitutionType().setId(oldId);
		}
		return institution;
	}


Thanks once more! If there is a better way of archiving this, be free to advise. THANNNNNNNNNNNNNNKS!
Previous Topic: Eclipse Luna SR2 (4.4.2) Release for Windows
Next Topic:Seeding the 'version' column starting number?
Goto Forum:
  


Current Time: Tue Feb 27 11:40:25 GMT 2024

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

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

Back to the top