Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 184470

Summary: Updating model does not commit changes to CompilationUnit - provide "headless" API
Product: [WebTools] Dali JPA Tools Reporter: Tom Mutdosch <mutdosch>
Component: GeneralAssignee: Karen Butzke <karenfbutzke>
Status: RESOLVED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: karenfbutzke, neil.hauge
Version: 1.0   
Target Milestone: 2.0.5   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Bug Depends on: 271847    
Bug Blocks:    

Description Tom Mutdosch CLA 2007-04-27 12:19:26 EDT
We have a wizard with an operation that sets the primary key on a selected Entity. 
We are setting the primary key by setting the mapping key on the IPersistentAttribute:

IPersistentAttribute attribute = myAttribute;				attribute.setMappingKey(IMappingKeys.ID_ATTRIBUTE_MAPPING_KEY), false);

This does not seem to actually commit the working copy.  If I then open up the modified entity in the Java editor, it will appear dirty.  It looks like in the jdtUtility.Member edit_ method, it does:
if ( ! textEditorPresent) {
	compilationUnit.getBuffer().setContents(doc.get());
	compilationUnit.commitWorkingCopy(true, null);
	compilationUnit.discardWorkingCopy();
}


This does not commit the change to the compilation unit.  Perhaps if this code could instead do something along the lines of:

compilationUnit.reconcile(ICompilationUnit.NO_AST, false, null, null);
if ( compilationUnit.isWorkingCopy()) {
	compilationUnit.commitWorkingCopy(true, null);
} else {
	compilationUnit.save(null,true);
}


Or an alternative would be an API mechanism that could be called to commit the JPA model changes.
Comment 1 Neil Hauge CLA 2007-05-17 16:54:32 EDT
Our general design is to work in tandem with the Java and XML editors, making edits to the source for the user, but never assuming that a user wants to commit.  We always want to leave this decision up to the user, since we don't want to assume that the code is in a "committable" state.

Looking at the comments of this method, there are some limitations that we are dealing with that seem to be very touchy.  I'm copying Karen to see if she has any comments on your suggestion.
Comment 2 Tom Mutdosch CLA 2007-05-18 08:33:57 EDT
Makes sense.  In our case when our wizard operation runs, the target entity is likely not even open in the Java/XML editor.  We are sort of using the model as a headless API.  In this scenario, it's a bit cumbersome as any change we make to the model we have to add a block of code to handle committing ourselves -- such as in the below example where we add a new NamedQuery to the Entity.  I was just thinking that it might be cleaner/nicer if there was an API mechanism on the model, such as IEntity.save() or JPAModel.saveModel() or something like that.

// add named query
List<INamedQuery> namedQueryList = ((IEntity)mapping).getNamedQueries();
INamedQuery q = ((IEntity)mapping).createNamedQuery(namedQueryList.size());
q.setName( "getMovies");
q.setQuery("select m FROM Movie m");
namedQueryList.add(q);

// save model change
ICompilationUnit compUnit = entityType.findJdtType().getCompilationUnit();		
if (compUnit.isWorkingCopy()) {
	compUnit.commitWorkingCopy(true, null);				
} else {		
	compUnit.save(null, true);
}
Comment 3 Neil Hauge CLA 2007-11-13 16:12:00 EST
Updating Severity to reflect enhancement request nature of this issue.  We need to evaluate this request for 2.0.
Comment 4 Neil Hauge CLA 2008-04-18 16:21:59 EDT
Ran out of time for this one.
Comment 5 Karen Butzke CLA 2009-04-10 11:07:27 EDT
This problem is solved with the solution to bug 271847, setting it to depend on that bug.
Comment 6 Karen Butzke CLA 2009-04-10 12:47:07 EDT
271847 was fixed