Emf 写入事务

Emf Write transaction

如果我尝试通过事务编辑域修改资源文件,我在资源管理器中有资源文件I am getting exception as

java.lang.IllegalStateException: Cannot modify resource set without a write transaction at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.assertWriting(TransactionChangeRecorder.java:348) at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.appendNotification(TransactionChangeRecorder.java:302) at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.processObjectNotification(TransactionChangeRecorder.java:284) at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.notifyChanged(TransactionChangeRecorder.java:240) at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:374) at org.eclipse.emf.common.notify.impl.NotificationImpl.dispatch(NotificationImpl.java:1027) at org.eclipse.emf.common.notify.impl.NotifyingListImpl.addUnique(NotifyingListImpl.java:299) at org.eclipse.emf.common.util.AbstractEList.add(AbstractEList.java:303)

我认为问题在于,您正试图在另一个写入事务中执行一个写入事务。命令应该可以解决问题。这可以使用模型的 EditingDomain 来完成:(确保 org.eclipse.emf.transaction 在您的依赖项中)

import org.eclipse.emf.transaction.TransactionalEditingDomain; 
import org.eclipse.emf.transaction.util.TransactionUtil;

public void doEditing(EObject element) {
    // Make sure your element is attached to a resource, otherwise this will return null
    TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(element);
    domain.getCommandStack().execute(new RecordingCommand(domain) {

        @Override
        protected void doExecute() {
            // Implement your write operations here,
            // for example: set a new name
            element.eSet(element.eClass().getEStructuralFeature("name"), "aNewName");
        }
    });
}