You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2026/07/04 21:25:07 UTC
(cayenne) branch master updated (20d3d83e5 -> f0eec8562)
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git
from 20d3d83e5 CAY-2973 Exception trying to copy/paste a callback
new e208d54e6 minor cleanup
new f0eec8562 CAY-2974 CayenneSqlException with a reference to translated query
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
RELEASE-NOTES.txt | 1 +
.../apache/cayenne/CayenneRuntimeException.java | 2 +-
.../org/apache/cayenne/CayenneSqlException.java | 69 ++++++++++++
.../org/apache/cayenne/access/DataContext.java | 5 +-
.../cayenne/access/DataDomainQueryAction.java | 19 +++-
.../apache/cayenne/access/flush/FlushObserver.java | 22 +++-
.../main/java/org/apache/cayenne/util/Util.java | 47 +++++---
.../apache/cayenne/CayenneSqlExceptionTest.java | 79 ++++++++++++++
.../cayenne/access/CayenneSqlExceptionIT.java | 119 +++++++++++++++++++++
.../java/org/apache/cayenne/util/UtilTest.java | 38 +++++++
.../cayenne/modeler/ui/action/PasteAction.java | 89 +++++----------
.../cayenne/modeler/undo/PasteUndoableEdit.java | 24 ++---
12 files changed, 421 insertions(+), 93 deletions(-)
create mode 100644 cayenne/src/main/java/org/apache/cayenne/CayenneSqlException.java
create mode 100644 cayenne/src/test/java/org/apache/cayenne/CayenneSqlExceptionTest.java
create mode 100644 cayenne/src/test/java/org/apache/cayenne/access/CayenneSqlExceptionIT.java
(cayenne) 01/02: minor cleanup
Posted by aa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git
commit e208d54e6b2ec0b4503734b0dcc190653cbd8e28
Author: Andrus Adamchik <an...@objectstyle.com>
AuthorDate: Sat Jul 4 16:08:15 2026 -0400
minor cleanup
---
.../apache/cayenne/CayenneRuntimeException.java | 2 +-
.../cayenne/modeler/ui/action/PasteAction.java | 89 +++++++---------------
.../cayenne/modeler/undo/PasteUndoableEdit.java | 24 +++---
3 files changed, 39 insertions(+), 76 deletions(-)
diff --git a/cayenne/src/main/java/org/apache/cayenne/CayenneRuntimeException.java b/cayenne/src/main/java/org/apache/cayenne/CayenneRuntimeException.java
index 083dd124d..0a40d37c7 100644
--- a/cayenne/src/main/java/org/apache/cayenne/CayenneRuntimeException.java
+++ b/cayenne/src/main/java/org/apache/cayenne/CayenneRuntimeException.java
@@ -27,7 +27,7 @@ import org.apache.cayenne.util.LocalizedStringsHandler;
*/
public class CayenneRuntimeException extends RuntimeException {
- private static String exceptionLabel;
+ private static final String exceptionLabel;
static {
String version = LocalizedStringsHandler.getString("cayenne.version");
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/PasteAction.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/PasteAction.java
index 3cc9821b8..26949af8c 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/PasteAction.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/action/PasteAction.java
@@ -136,9 +136,7 @@ public class PasteAction extends AppAction implements FlavorListener {
}
private void paste(Object where, Object content) {
- paste(where, content, (DataChannelDescriptor) getProjectSession()
- .project()
- .getRootNode(), getProjectSession().getSelectedDataMap());
+ paste(where, content, (DataChannelDescriptor) getProjectSession().project().getRootNode());
}
/**
@@ -147,23 +145,17 @@ public class PasteAction extends AppAction implements FlavorListener {
public void paste(
Object where,
Object content,
- DataChannelDescriptor dataChannelDescriptor,
- DataMap map) {
+ DataChannelDescriptor dataChannelDescriptor) {
ProjectSession session = getProjectSession();
- /**
- * Add a little intelligence - if a tree leaf is selected, we can paste to a
- * parent datamap
- */
+ // Add a little intelligence - if a tree leaf is selected, we can paste to a parent datamap
if (isTreeLeaf(where) && isTreeLeaf(content)) {
where = session.getSelectedDataMap();
}
- if ((where instanceof DataChannelDescriptor || where instanceof DataNodeDescriptor)
- && content instanceof DataMap) {
+ if ((where instanceof DataChannelDescriptor || where instanceof DataNodeDescriptor) && content instanceof DataMap dataMap) {
// paste DataMap to DataDomain or DataNode
- DataMap dataMap = ((DataMap) content);
dataMap.setName(NameBuilder
.builder(dataMap, dataChannelDescriptor)
@@ -171,16 +163,12 @@ public class PasteAction extends AppAction implements FlavorListener {
.dupesPattern(COPY_PATTERN)
.name());
- /**
- * Update all names in the new DataMap, so that they would not conflict with
- * names from other datamaps of this domain
- */
-
- // add some intelligence - if we rename an entity, we should rename all links
+ // Update all names in the new DataMap, so that they would not conflict with
+ // names from other datamaps of this domain.
+ // Add some intelligence - if we rename an entity, we should rename all links
// to it as well
Map<String, String> renamedDbEntities = new HashMap<>();
Map<String, String> renamedObjEntities = new HashMap<>();
- Map<String, String> renamedEmbeddables = new HashMap<>();
for (DbEntity dbEntity : dataMap.getDbEntities()) {
String oldName = dbEntity.getName();
@@ -209,16 +197,11 @@ public class PasteAction extends AppAction implements FlavorListener {
}
for (Embeddable embeddable : dataMap.getEmbeddables()) {
- String oldName = embeddable.getClassName();
embeddable.setClassName(NameBuilder
.builder(embeddable, dataMap)
.baseName(embeddable.getClassName())
.dupesPattern(COPY_PATTERN)
.name());
-
- if (!oldName.equals(embeddable.getClassName())) {
- renamedEmbeddables.put(oldName, embeddable.getClassName());
- }
}
for (Procedure procedure : dataMap.getProcedures()) {
@@ -265,15 +248,13 @@ public class PasteAction extends AppAction implements FlavorListener {
}
CreateDataMapAction.onMapCreated(this, getProjectSession(), dataMap);
- } else if (where instanceof DataMap) {
+ } else if (where instanceof DataMap dataMap) {
// paste DbEntity to DataMap
- final DataMap dataMap = ((DataMap) where);
// clear data map parent cache
clearDataMapCache(dataMap);
- if (content instanceof DbEntity) {
- DbEntity dbEntity = (DbEntity) content;
+ if (content instanceof DbEntity dbEntity) {
dbEntity.setName(NameBuilder
.builder(dbEntity, dataMap)
.baseName(dbEntity.getName())
@@ -282,9 +263,8 @@ public class PasteAction extends AppAction implements FlavorListener {
dataMap.addDbEntity(dbEntity);
CreateDbEntityAction.onDbEntityCreated(this, session, dbEntity);
- } else if (content instanceof ObjEntity) {
+ } else if (content instanceof ObjEntity objEntity) {
// paste ObjEntity to DataMap
- ObjEntity objEntity = (ObjEntity) content;
objEntity.setName(NameBuilder.builder(objEntity, dataMap)
.baseName(objEntity.getName())
.dupesPattern(COPY_PATTERN)
@@ -296,9 +276,8 @@ public class PasteAction extends AppAction implements FlavorListener {
session,
dataMap,
objEntity);
- } else if (content instanceof Embeddable) {
+ } else if (content instanceof Embeddable embeddable) {
// paste Embeddable to DataMap
- Embeddable embeddable = (Embeddable) content;
embeddable.setClassName(NameBuilder
.builder(embeddable, dataMap)
.baseName(embeddable.getClassName())
@@ -311,8 +290,7 @@ public class PasteAction extends AppAction implements FlavorListener {
session,
dataMap,
embeddable);
- } else if (content instanceof QueryDescriptor) {
- QueryDescriptor query = (QueryDescriptor) content;
+ } else if (content instanceof QueryDescriptor query) {
query.setName(NameBuilder
.builder(query, dataMap)
@@ -323,9 +301,8 @@ public class PasteAction extends AppAction implements FlavorListener {
dataMap.addQueryDescriptor(query);
QueryTypeDialog.fireQueryEvent(this, session, dataMap, query);
- } else if (content instanceof Procedure) {
+ } else if (content instanceof Procedure procedure) {
// paste Procedure to DataMap
- Procedure procedure = (Procedure) content;
procedure.setName(NameBuilder
.builder(procedure, dataMap)
.dupesPattern(COPY_PATTERN)
@@ -339,11 +316,9 @@ public class PasteAction extends AppAction implements FlavorListener {
dataMap,
procedure);
}
- } else if (where instanceof DbEntity) {
- final DbEntity dbEntity = (DbEntity) where;
+ } else if (where instanceof DbEntity dbEntity) {
- if (content instanceof DbAttribute) {
- DbAttribute attr = (DbAttribute) content;
+ if (content instanceof DbAttribute attr) {
attr.setName(NameBuilder
.builder(attr, dbEntity)
.dupesPattern(COPY_PATTERN)
@@ -353,8 +328,7 @@ public class PasteAction extends AppAction implements FlavorListener {
dbEntity.addAttribute(attr);
CreateAttributeAction.fireDbAttributeEvent(this, session, session
.getSelectedDataMap(), dbEntity, attr);
- } else if (content instanceof DbRelationship) {
- DbRelationship rel = (DbRelationship) content;
+ } else if (content instanceof DbRelationship rel) {
rel.setName(NameBuilder
.builder(rel, dbEntity)
.baseName(rel.getName())
@@ -368,11 +342,9 @@ public class PasteAction extends AppAction implements FlavorListener {
dbEntity,
rel);
}
- } else if (where instanceof ObjEntity) {
- ObjEntity objEntity = (ObjEntity) where;
+ } else if (where instanceof ObjEntity objEntity) {
- if (content instanceof ObjAttribute) {
- ObjAttribute attr = (ObjAttribute) content;
+ if (content instanceof ObjAttribute attr) {
attr.setName(NameBuilder
.builder(attr, objEntity)
.baseName(attr.getName())
@@ -382,8 +354,7 @@ public class PasteAction extends AppAction implements FlavorListener {
objEntity.addAttribute(attr);
CreateAttributeAction.fireObjAttributeEvent(this, session, session
.getSelectedDataMap(), objEntity, attr);
- } else if (content instanceof ObjRelationship) {
- ObjRelationship rel = (ObjRelationship) content;
+ } else if (content instanceof ObjRelationship rel) {
rel.setName(NameBuilder
.builder(rel, objEntity)
.baseName(rel.getName())
@@ -396,8 +367,7 @@ public class PasteAction extends AppAction implements FlavorListener {
session,
objEntity,
rel);
- } else if (content instanceof ObjCallbackMethod) {
- ObjCallbackMethod method = (ObjCallbackMethod) content;
+ } else if (content instanceof ObjCallbackMethod method) {
method.setName(NameBuilder
.builderForCallbackMethod(objEntity)
@@ -413,11 +383,9 @@ public class PasteAction extends AppAction implements FlavorListener {
getProjectSession().fireCallbackMethodEvent(ce);
}
- } else if (where instanceof Embeddable) {
- final Embeddable embeddable = (Embeddable) where;
+ } else if (where instanceof Embeddable embeddable) {
- if (content instanceof EmbeddableAttribute) {
- EmbeddableAttribute attr = (EmbeddableAttribute) content;
+ if (content instanceof EmbeddableAttribute attr) {
attr.setName(NameBuilder
.builder(attr, embeddable)
.baseName(attr.getName())
@@ -432,12 +400,10 @@ public class PasteAction extends AppAction implements FlavorListener {
attr);
}
- } else if (where instanceof Procedure) {
+ } else if (where instanceof Procedure procedure) {
// paste param to procedure
- final Procedure procedure = (Procedure) where;
- if (content instanceof ProcedureParameter) {
- ProcedureParameter param = (ProcedureParameter) content;
+ if (content instanceof ProcedureParameter param) {
param.setName(NameBuilder
.builder(param, procedure)
@@ -491,7 +457,7 @@ public class PasteAction extends AppAction implements FlavorListener {
CMTransferable.CAYENNE_FLAVOR);
if (content instanceof List) {
- content = ((List) content).get(0);
+ content = ((List) content).getFirst();
}
Object currentObject = getProjectSession().getSelectedObject();
@@ -500,10 +466,7 @@ public class PasteAction extends AppAction implements FlavorListener {
return false;
}
- /**
- * Checking all available pairs source-pasting object
- */
-
+ // Checking all available pairs source-pasting object
return ((currentObject instanceof DataChannelDescriptor || currentObject instanceof DataNodeDescriptor) && content instanceof DataMap)
||
diff --git a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/PasteUndoableEdit.java b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/PasteUndoableEdit.java
index da3766fda..395be9e86 100644
--- a/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/PasteUndoableEdit.java
+++ b/modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/undo/PasteUndoableEdit.java
@@ -18,9 +18,6 @@
****************************************************************/
package org.apache.cayenne.modeler.undo;
-import javax.swing.undo.CannotRedoException;
-import javax.swing.undo.CannotUndoException;
-
import org.apache.cayenne.configuration.DataChannelDescriptor;
import org.apache.cayenne.configuration.DataNodeDescriptor;
import org.apache.cayenne.map.DataMap;
@@ -34,25 +31,28 @@ import org.apache.cayenne.map.ObjEntity;
import org.apache.cayenne.map.ObjRelationship;
import org.apache.cayenne.map.Procedure;
import org.apache.cayenne.map.ProcedureParameter;
+import org.apache.cayenne.map.QueryDescriptor;
+import org.apache.cayenne.modeler.project.ProjectSession;
import org.apache.cayenne.modeler.ui.action.PasteAction;
import org.apache.cayenne.modeler.ui.action.RemoveAction;
import org.apache.cayenne.modeler.ui.action.RemoveAttributeAction;
import org.apache.cayenne.modeler.ui.action.RemoveCallbackMethodAction;
import org.apache.cayenne.modeler.ui.action.RemoveProcedureParameterAction;
import org.apache.cayenne.modeler.ui.action.RemoveRelationshipAction;
-import org.apache.cayenne.modeler.project.ProjectSession;
import org.apache.cayenne.modeler.ui.project.editor.objentity.callbacks.ObjCallbackMethod;
-import org.apache.cayenne.map.QueryDescriptor;
+
+import javax.swing.undo.CannotRedoException;
+import javax.swing.undo.CannotUndoException;
public class PasteUndoableEdit extends CayenneUndoableEdit {
- private DataChannelDescriptor domain;
- private DataMap map;
- private Object where;
- private Object content;
+ private final DataChannelDescriptor domain;
+ private final DataMap map;
+ private final Object where;
+ private final Object content;
- public PasteUndoableEdit(ProjectSession session, DataChannelDescriptor domain, DataMap map, Object where,
- Object content) {
+ public PasteUndoableEdit(
+ ProjectSession session, DataChannelDescriptor domain, DataMap map, Object where, Object content) {
super(session);
this.domain = domain;
this.map = map;
@@ -74,7 +74,7 @@ public class PasteUndoableEdit extends CayenneUndoableEdit {
public void redo() throws CannotRedoException {
PasteAction action = globalActions.getAction(PasteAction.class);
- action.paste(where, content, domain, map);
+ action.paste(where, content, domain);
}
@Override
(cayenne) 02/02: CAY-2974 CayenneSqlException with a reference to translated query
Posted by aa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git
commit f0eec8562621faa1623f0b8beefc8c67f561296c
Author: Andrus Adamchik <an...@objectstyle.com>
AuthorDate: Sat Jul 4 16:23:28 2026 -0400
CAY-2974 CayenneSqlException with a reference to translated query
---
RELEASE-NOTES.txt | 1 +
.../org/apache/cayenne/CayenneSqlException.java | 69 ++++++++++++
.../org/apache/cayenne/access/DataContext.java | 5 +-
.../cayenne/access/DataDomainQueryAction.java | 19 +++-
.../apache/cayenne/access/flush/FlushObserver.java | 22 +++-
.../main/java/org/apache/cayenne/util/Util.java | 47 +++++---
.../apache/cayenne/CayenneSqlExceptionTest.java | 79 ++++++++++++++
.../cayenne/access/CayenneSqlExceptionIT.java | 119 +++++++++++++++++++++
.../java/org/apache/cayenne/util/UtilTest.java | 38 +++++++
9 files changed, 382 insertions(+), 17 deletions(-)
diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index acd6dcc9d..df2a21fff 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -23,6 +23,7 @@ CAY-2969 Extender API for "soft" delete
CAY-2970 Tighten deferred value resolution contract on commit
CAY-2971 Remove extra spaces within SQL parenthesis
CAY-2972 Fewer parentheses in generated SQL
+CAY-2974 CayenneSqlException with a reference to translated query
Bug Fixes:
diff --git a/cayenne/src/main/java/org/apache/cayenne/CayenneSqlException.java b/cayenne/src/main/java/org/apache/cayenne/CayenneSqlException.java
new file mode 100644
index 000000000..1f0b69e7a
--- /dev/null
+++ b/cayenne/src/main/java/org/apache/cayenne/CayenneSqlException.java
@@ -0,0 +1,69 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+
+package org.apache.cayenne;
+
+import org.apache.cayenne.access.translator.TranslatedStatement;
+import org.apache.cayenne.query.Query;
+
+/**
+ * An exception thrown when a failure occurs while executing a query statement or reading its results. It carries the
+ * {@link Query} being executed and the {@link TranslatedStatement} that was being executed at the time of the failure,
+ * and includes the statement's SQL in the exception message, so that a failure can be correlated with a specific query.
+ *
+ * @since 5.0
+ */
+public class CayenneSqlException extends CayenneRuntimeException {
+
+ private final transient Query query;
+ private final transient TranslatedStatement statement;
+
+ /**
+ * Creates an exception for a failure that happened while executing the provided query and statement. The statement
+ * may be null if the failure occurred before a statement was translated.
+ */
+ public CayenneSqlException(String message, Query query, TranslatedStatement statement, Throwable cause) {
+ // pass the pre-built message as a "%s" argument so that any "%" characters in the SQL (e.g. LIKE patterns)
+ // are not interpreted as format specifiers by the superclass
+ super("%s", cause, buildMessage(message, statement));
+ this.query = query;
+ this.statement = statement;
+ }
+
+ /**
+ * Returns the query that was being executed when the failure occurred, or null if it is not known.
+ */
+ public Query getQuery() {
+ return query;
+ }
+
+ /**
+ * Returns the statement that was being executed when the failure occurred, or null if it is not known.
+ */
+ public TranslatedStatement getStatement() {
+ return statement;
+ }
+
+ private static String buildMessage(String message, TranslatedStatement statement) {
+ String base = message != null ? message : "SQL failure";
+ return statement != null
+ ? base + " SQL: [" + statement.sql() + "]"
+ : base;
+ }
+}
diff --git a/cayenne/src/main/java/org/apache/cayenne/access/DataContext.java b/cayenne/src/main/java/org/apache/cayenne/access/DataContext.java
index bc479d2fa..27a26c2a4 100644
--- a/cayenne/src/main/java/org/apache/cayenne/access/DataContext.java
+++ b/cayenne/src/main/java/org/apache/cayenne/access/DataContext.java
@@ -991,9 +991,10 @@ public class DataContext implements ObjectContext {
// this event is caught by peer nested DataContexts to synchronize the state
fireDataChannelCommitted(this, changes);
}
- // "catch" is needed to unwrap OptimisticLockExceptions
+ // "catch" is needed to unwrap meaningful CayenneRuntimeExceptions (e.g. OptimisticLockException, or a
+ // CayenneSqlException carrying the failing SQL) without unwinding past them into their lower-level cause
catch (CayenneRuntimeException ex) {
- Throwable unwound = Util.unwindException(ex);
+ Throwable unwound = Util.unwindException(ex, CayenneRuntimeException.class);
if (unwound instanceof CayenneRuntimeException cayenneRuntimeException) {
throw cayenneRuntimeException;
diff --git a/cayenne/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java b/cayenne/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java
index 87e3709a1..e6e80d8f2 100644
--- a/cayenne/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java
+++ b/cayenne/src/main/java/org/apache/cayenne/access/DataDomainQueryAction.java
@@ -20,6 +20,7 @@
package org.apache.cayenne.access;
import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.CayenneSqlException;
import org.apache.cayenne.DataRow;
import org.apache.cayenne.EmbeddableObject;
import org.apache.cayenne.ObjectContext;
@@ -27,6 +28,7 @@ import org.apache.cayenne.ObjectId;
import org.apache.cayenne.Persistent;
import org.apache.cayenne.QueryResponse;
import org.apache.cayenne.ResultIterator;
+import org.apache.cayenne.access.translator.TranslatedStatement;
import org.apache.cayenne.cache.QueryCache;
import org.apache.cayenne.cache.QueryCacheEntryFactory;
import org.apache.cayenne.di.AdhocObjectFactory;
@@ -100,6 +102,9 @@ class DataDomainQueryAction implements QueryRouter, OperationObserver {
private Map<DataNode, Collection<Query>> queriesByNode;
private boolean noObjectConversion;
+ // the latest statement reported via nextStatement, used to correlate a failure with a specific SQL statement
+ private TranslatedStatement statement;
+
// True when using a caching strategy (shared or local cache), indicating lists are immutable and need copying
private boolean cachedResult;
// True when results were found in cache (cache hit), false when fetched from database (cache miss or explicit refresh)
@@ -685,9 +690,21 @@ class DataDomainQueryAction implements QueryRouter, OperationObserver {
}
}
+ @Override
+ public void nextStatement(Query query, TranslatedStatement statement) {
+ this.statement = statement;
+ }
+
@Override
public void nextQueryException(Query query, Exception ex) {
- throw new CayenneRuntimeException("Query exception.", Util.unwindException(ex));
+ // preserve meaningful Cayenne exceptions, even when they wrap a lower-level cause; only wrap raw lower-level
+ // failures in a CayenneSqlException so they can be correlated with the failing statement
+ Throwable unwound = Util.unwindException(ex, CayenneRuntimeException.class);
+
+ if (unwound instanceof CayenneRuntimeException cayenneException) {
+ throw cayenneException;
+ }
+ throw new CayenneSqlException("Query exception.", query, statement, unwound);
}
@Override
diff --git a/cayenne/src/main/java/org/apache/cayenne/access/flush/FlushObserver.java b/cayenne/src/main/java/org/apache/cayenne/access/flush/FlushObserver.java
index 09808bdee..9834dc30b 100644
--- a/cayenne/src/main/java/org/apache/cayenne/access/flush/FlushObserver.java
+++ b/cayenne/src/main/java/org/apache/cayenne/access/flush/FlushObserver.java
@@ -20,10 +20,12 @@
package org.apache.cayenne.access.flush;
import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.CayenneSqlException;
import org.apache.cayenne.DataRow;
import org.apache.cayenne.ObjectId;
import org.apache.cayenne.ResultIterator;
import org.apache.cayenne.access.OperationObserver;
+import org.apache.cayenne.access.translator.TranslatedStatement;
import org.apache.cayenne.map.DbAttribute;
import org.apache.cayenne.query.InsertBatchQuery;
import org.apache.cayenne.query.Query;
@@ -36,14 +38,30 @@ import java.util.List;
*/
class FlushObserver implements OperationObserver {
+ // the latest statement reported via nextStatement, used to correlate a failure with a specific SQL statement
+ private TranslatedStatement statement;
+
+ @Override
+ public void nextStatement(Query query, TranslatedStatement statement) {
+ this.statement = statement;
+ }
+
@Override
public void nextQueryException(Query query, Exception ex) {
- throw new CayenneRuntimeException("Raising from query exception.", Util.unwindException(ex));
+ // preserve meaningful Cayenne exceptions (e.g. OptimisticLockException), even when they wrap a lower-level
+ // cause; only wrap raw lower-level failures in a CayenneSqlException so they can be correlated with the
+ // failing statement
+ Throwable unwound = Util.unwindException(ex, CayenneRuntimeException.class);
+
+ if (unwound instanceof CayenneRuntimeException cayenneException) {
+ throw cayenneException;
+ }
+ throw new CayenneSqlException("Flush exception.", query, statement, unwound);
}
@Override
public void nextGlobalException(Exception ex) {
- throw new CayenneRuntimeException("Raising from underlyingQueryEngine exception.", Util.unwindException(ex));
+ throw new CayenneRuntimeException("Flush exception.", Util.unwindException(ex));
}
/**
diff --git a/cayenne/src/main/java/org/apache/cayenne/util/Util.java b/cayenne/src/main/java/org/apache/cayenne/util/Util.java
index ccce61697..01e9ff22e 100644
--- a/cayenne/src/main/java/org/apache/cayenne/util/Util.java
+++ b/cayenne/src/main/java/org/apache/cayenne/util/Util.java
@@ -173,20 +173,43 @@ public class Util {
* recursively "unwraps" it, and returns the result to the user.
*/
public static Throwable unwindException(Throwable th) {
- if (th instanceof SAXException sax) {
- if (sax.getException() != null) {
- return unwindException(sax.getException());
- }
- } else if (th instanceof SQLException) {
- SQLException sql = (SQLException) th;
- if (sql.getNextException() != null) {
- return unwindException(sql.getNextException());
- }
- } else if (th.getCause() != null) {
- return unwindException(th.getCause());
+ Throwable next = nextInChain(th);
+ return next != null ? unwindException(next) : th;
+ }
+
+ /**
+ * Unwinds an exception chain like {@link #unwindException(Throwable)}, but never unwinds past the innermost
+ * exception that is an instance of {@code upTo}. If the chain contains such an exception, the innermost matching one
+ * is returned; otherwise the fully unwound root cause is returned. This lets callers strip generic wrappers while
+ * preserving a meaningful exception type that itself wraps a lower-level cause.
+ *
+ * @since 5.0
+ */
+ public static Throwable unwindException(Throwable th, Class<? extends Throwable> upTo) {
+ Throwable next = nextInChain(th);
+ if (next == null) {
+ return th;
}
- return th;
+ Throwable deeper = unwindException(next, upTo);
+ if (upTo.isInstance(deeper)) {
+ return deeper;
+ }
+ return upTo.isInstance(th) ? th : deeper;
+ }
+
+ /**
+ * Returns the next exception to unwind to (a nested SAX exception, a chained SQL exception, or the cause), or null
+ * if this is the end of the chain.
+ */
+ private static Throwable nextInChain(Throwable th) {
+ if (th instanceof SAXException sax) {
+ return sax.getException();
+ } else if (th instanceof SQLException sql) {
+ return sql.getNextException();
+ } else {
+ return th.getCause();
+ }
}
/**
diff --git a/cayenne/src/test/java/org/apache/cayenne/CayenneSqlExceptionTest.java b/cayenne/src/test/java/org/apache/cayenne/CayenneSqlExceptionTest.java
new file mode 100644
index 000000000..29edd36a6
--- /dev/null
+++ b/cayenne/src/test/java/org/apache/cayenne/CayenneSqlExceptionTest.java
@@ -0,0 +1,79 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+
+package org.apache.cayenne;
+
+import org.apache.cayenne.access.translator.TranslatedSQL;
+import org.apache.cayenne.access.translator.TranslatedStatement;
+import org.apache.cayenne.query.Query;
+import org.apache.cayenne.query.SQLTemplate;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class CayenneSqlExceptionTest {
+
+ @Test
+ public void messageIncludesSql() {
+ Query query = new SQLTemplate();
+ TranslatedStatement statement = new TranslatedSQL("SELECT * FROM ARTIST", null, null);
+ Throwable cause = new RuntimeException("boom");
+
+ CayenneSqlException e = new CayenneSqlException("Query exception.", query, statement, cause);
+
+ assertSame(query, e.getQuery());
+ assertSame(statement, e.getStatement());
+ assertSame(cause, e.getCause());
+ assertTrue(e.getMessage().contains("Query exception."), e.getMessage());
+ assertTrue(e.getMessage().contains("SELECT * FROM ARTIST"), e.getMessage());
+ }
+
+ @Test
+ public void queryNotIncludedInMessage() {
+ Query query = new SQLTemplate();
+ TranslatedStatement statement = new TranslatedSQL("SELECT * FROM ARTIST", null, null);
+
+ CayenneSqlException e = new CayenneSqlException("Query exception.", query, statement, new RuntimeException());
+
+ assertSame(query, e.getQuery());
+ assertFalse(e.getMessage().contains(query.toString()), e.getMessage());
+ }
+
+ @Test
+ public void sqlWithPercentDoesNotBreakFormatting() {
+ // SQL may contain '%' (e.g. LIKE patterns); it must not be interpreted as a format specifier
+ TranslatedStatement statement = new TranslatedSQL("SELECT * FROM ARTIST WHERE NAME LIKE '%foo%'", null, null);
+
+ CayenneSqlException e = new CayenneSqlException("Query exception.", null, statement, new RuntimeException());
+
+ assertTrue(e.getMessage().contains("LIKE '%foo%'"), e.getMessage());
+ }
+
+ @Test
+ public void nullQueryAndStatementAreAllowed() {
+ CayenneSqlException e = new CayenneSqlException("Query exception.", null, null, new RuntimeException());
+
+ assertNull(e.getQuery());
+ assertNull(e.getStatement());
+ assertTrue(e.getMessage().contains("Query exception."), e.getMessage());
+ }
+}
diff --git a/cayenne/src/test/java/org/apache/cayenne/access/CayenneSqlExceptionIT.java b/cayenne/src/test/java/org/apache/cayenne/access/CayenneSqlExceptionIT.java
new file mode 100644
index 000000000..34c464846
--- /dev/null
+++ b/cayenne/src/test/java/org/apache/cayenne/access/CayenneSqlExceptionIT.java
@@ -0,0 +1,119 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ****************************************************************/
+
+package org.apache.cayenne.access;
+
+import org.apache.cayenne.CayenneSqlException;
+import org.apache.cayenne.exp.ExpressionFactory;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.query.ObjectSelect;
+import org.apache.cayenne.query.SQLSelect;
+import org.apache.cayenne.testdo.testmap.Artist;
+import org.apache.cayenne.unit.CayenneProjects;
+import org.apache.cayenne.unit.CayenneTestsEnv;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Verifies that failures happening while a statement is executed against the database surface as a
+ * {@link CayenneSqlException} carrying the failing query and its translated SQL.
+ */
+public class CayenneSqlExceptionIT {
+
+ @RegisterExtension
+ static final CayenneTestsEnv env = CayenneTestsEnv.forProject(CayenneProjects.TESTMAP_PROJECT);
+
+ private DataContext context;
+
+ @BeforeEach
+ public void setUp() {
+ context = env.context();
+ }
+
+ @Test
+ public void selectWithInvalidSql() {
+ // deliberately malformed SQL
+ String badSql = "SELECT FROM NOT ARTIST WHERE";
+ SQLSelect<Artist> query = SQLSelect.query(Artist.class, badSql);
+
+ CayenneSqlException e = assertThrows(CayenneSqlException.class, () -> query.select(context));
+// e.printStackTrace();
+
+ assertNotNull(e.getStatement(), "statement should have been captured");
+ // SQLSelect is executed as an internally-routed SQLTemplate, so getQuery() reports that executed query
+ assertNotNull(e.getQuery());
+ assertTrue(e.getMessage().contains(badSql),
+ () -> "Expected message to include the failing SQL, but was: " + e.getMessage());
+ }
+
+ @Test
+ public void objectSelectWithBadExpressionParameter() {
+ // comparing the numeric PK column to a non-numeric value produces valid SQL that the DB rejects when the
+ // bogus parameter is bound and the statement is executed
+ ObjectSelect<Artist> query = ObjectSelect.query(Artist.class)
+ .where(ExpressionFactory.matchDbExp(Artist.ARTIST_ID_PK_COLUMN, "not-a-number"));
+
+ CayenneSqlException e = assertThrows(CayenneSqlException.class, () -> query.select(context));
+// e.printStackTrace();
+
+ assertNotNull(e.getStatement(), "statement should have been captured");
+ assertSame(query, e.getQuery());
+ assertTrue(e.getStatement().sql().contains("ARTIST"),
+ () -> "Expected SELECT SQL against ARTIST, but was: " + e.getStatement().sql());
+ }
+
+ @Test
+ public void commitWithNonExistingColumn() {
+ // point the "artistName" attribute at a column that does not exist in the ARTIST table, so the generated
+ // INSERT references an unknown column and the DB rejects it on commit
+ DbEntity artistDb = context.getEntityResolver().getDbEntity("ARTIST");
+ DbAttribute nameColumn = artistDb.getAttribute("ARTIST_NAME");
+ artistDb.removeAttribute("ARTIST_NAME");
+ nameColumn.setName("BOGUS_COLUMN");
+ artistDb.addAttribute(nameColumn);
+
+ ObjEntity artistObj = context.getEntityResolver().getObjEntity("Artist");
+ ObjAttribute nameAttribute = artistObj.getAttribute("artistName");
+ nameAttribute.setDbAttributePath("BOGUS_COLUMN");
+
+ context.getEntityResolver().refreshMappingCache();
+
+ Artist artist = context.newObject(Artist.class);
+ artist.setArtistName("aaa");
+
+ CayenneSqlException e = assertThrows(CayenneSqlException.class, () -> context.commitChanges());
+// e.printStackTrace();
+
+ assertNotNull(e.getStatement(), "statement should have been captured");
+ assertNotNull(e.getQuery());
+ assertTrue(e.getStatement().sql().contains("BOGUS_COLUMN"),
+ () -> "Expected INSERT SQL to reference BOGUS_COLUMN, but was: " + e.getStatement().sql());
+ assertTrue(e.getMessage().contains("BOGUS_COLUMN"),
+ () -> "Expected message to include the failing SQL, but was: " + e.getMessage());
+ }
+}
diff --git a/cayenne/src/test/java/org/apache/cayenne/util/UtilTest.java b/cayenne/src/test/java/org/apache/cayenne/util/UtilTest.java
index 7a114681e..99010cb4e 100644
--- a/cayenne/src/test/java/org/apache/cayenne/util/UtilTest.java
+++ b/cayenne/src/test/java/org/apache/cayenne/util/UtilTest.java
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileWriter;
+import java.sql.SQLException;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -327,4 +328,41 @@ public class UtilTest {
public void underscoredToJavaSpecialChars() throws Exception {
assertEquals("ABCpoundXyz", Util.underscoredToJava("ABC#_XYZ", true));
}
+
+ @Test
+ public void unwindException() {
+ SQLException sql = new SQLException("bad sql");
+ CayenneRuntimeException wrapper = new CayenneRuntimeException(sql);
+
+ // the plain form unwinds all the way to the non-Cayenne root
+ assertSame(sql, Util.unwindException(wrapper));
+ }
+
+ @Test
+ public void unwindExceptionUpTo_stopsAtWrapper() {
+ SQLException sql = new SQLException("bad sql");
+ CayenneRuntimeException wrapper = new CayenneRuntimeException(sql);
+
+ // does not unwind past the CayenneRuntimeException into its lower-level SQLException cause
+ assertSame(wrapper, Util.unwindException(wrapper, CayenneRuntimeException.class));
+ }
+
+ @Test
+ public void unwindExceptionUpTo_returnsInnermostMatch() {
+ SQLException sql = new SQLException("bad sql");
+ CayenneRuntimeException inner = new CayenneRuntimeException("inner", sql);
+ CayenneRuntimeException outer = new CayenneRuntimeException("outer", inner);
+
+ // generic outer wrapper is stripped, but unwinding stops at the innermost matching exception
+ assertSame(inner, Util.unwindException(outer, CayenneRuntimeException.class));
+ }
+
+ @Test
+ public void unwindExceptionUpTo_noMatchUnwindsToRoot() {
+ IllegalArgumentException root = new IllegalArgumentException("root");
+ RuntimeException wrapper = new RuntimeException(root);
+
+ // no exception of the requested type in the chain, so behaves like the plain unwind
+ assertSame(root, Util.unwindException(wrapper, SQLException.class));
+ }
}