You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by "rzo1 (via GitHub)" <gi...@apache.org> on 2026/08/29 17:53:27 UTC

[PR] [OPENJPA-2967] Make VERSION() work for surrogate versions [openjpa]

rzo1 opened a new pull request, #168:
URL: https://github.com/apache/openjpa/pull/168

   Note that the issue description is out of date: `getColumns()` no longer calls `getVersionFieldMapping()` and `initialize()` already validates the class mapping, both since e94a8515b, and both are already covered by tests. What remains is the surrogate case.
   
   `VersionVal.getType()` returned null for a type whose version is mapped by the version strategy rather than by a `@Version` field. `initialize()` admits such a type on purpose, since its version columns exist, so the null escaped into `JPQLExpressionBuilder` (`type.isPrimitive()` for `SELECT VERSION(e)`) and into `Filters.wrap` (for `WHERE VERSION(e) = :v`), both failing with a NullPointerException.
   
   `getType()` now falls back to `Object.class` — the same answer the in-memory `VersionVal` already gives, so the two query paths agree — and `toDataStoreValue()` converts through the version column's java type. Types with a real `@Version` field are unaffected.
   
   `initialize()` also reported an invalid `VERSION()` argument with the `bad-getobjectid` message, which describes an object id failure and whose argument is null for a bare identification variable. It now uses a dedicated message naming the path.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@openjpa.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [OPENJPA-2967] Make VERSION() work for surrogate versions [openjpa]

Posted by "solomax (via GitHub)" <gi...@apache.org>.
solomax commented on code in PR #168:
URL: https://github.com/apache/openjpa/pull/168#discussion_r3891558097


##########
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/VersionVal.java:
##########
@@ -78,11 +79,19 @@ public void setMetaData(ClassMetaData meta) {
 
     @Override
     public Class getType() {
-    	FieldMetaData versionField = _path.getMetaData().getVersionField();
-    	if (versionField != null) {
-    		return versionField.getType();
-    	}
-        return null;
+        // note: getColumns()/initialize() resolve the target type from the
+        // ExpState; getType() has none, so it uses the path's class.
+        ClassMetaData meta = _path.getMetaData();
+        FieldMetaData versionField = (meta == null) ? null : meta.getVersionField();
+        if (versionField != null)

Review Comment:
   Could you please add `{}`? :)



##########
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/VersionVal.java:
##########
@@ -98,23 +107,43 @@ public ExpState initialize(Select sel, ExpContext ctx, int flags) {
         // non-pc fields at all
         ClassMapping cls = _path.getClassMapping(state);
         if (cls == null || cls.getEmbeddingMapping() != null)
-            throw new UserException(_loc.get("bad-getobjectid", _path.getFieldMapping(state)));
+            throw new UserException(_loc.get("bad-version-path", pathDescription()));
 
         // types that are not versioned have no version columns to select,
         // group, order or compare by; fail with a meaningful message rather
         // than a NullPointerException further down the line
-        if (cls.getVersion() == null || cls.getVersion().getColumns().length == 0)
+        if (cls.getVersion().getColumns().length == 0)

Review Comment:
   `cls.getVersion()` can't be `null` here?



##########
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/VersionVal.java:
##########
@@ -78,11 +79,19 @@ public void setMetaData(ClassMetaData meta) {
 
     @Override
     public Class getType() {
-    	FieldMetaData versionField = _path.getMetaData().getVersionField();
-    	if (versionField != null) {
-    		return versionField.getType();
-    	}
-        return null;
+        // note: getColumns()/initialize() resolve the target type from the
+        // ExpState; getType() has none, so it uses the path's class.
+        ClassMetaData meta = _path.getMetaData();
+        FieldMetaData versionField = (meta == null) ? null : meta.getVersionField();
+        if (versionField != null)

Review Comment:
   In all changes :)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@openjpa.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [OPENJPA-2967] Make VERSION() work for surrogate versions [openjpa]

Posted by "rzo1 (via GitHub)" <gi...@apache.org>.
rzo1 merged PR #168:
URL: https://github.com/apache/openjpa/pull/168


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@openjpa.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [OPENJPA-2967] Make VERSION() work for surrogate versions [openjpa]

Posted by "rzo1 (via GitHub)" <gi...@apache.org>.
rzo1 commented on code in PR #168:
URL: https://github.com/apache/openjpa/pull/168#discussion_r3897687876


##########
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/VersionVal.java:
##########
@@ -78,11 +79,19 @@ public void setMetaData(ClassMetaData meta) {
 
     @Override
     public Class getType() {
-    	FieldMetaData versionField = _path.getMetaData().getVersionField();
-    	if (versionField != null) {
-    		return versionField.getType();
-    	}
-        return null;
+        // note: getColumns()/initialize() resolve the target type from the
+        // ExpState; getType() has none, so it uses the path's class.
+        ClassMetaData meta = _path.getMetaData();
+        FieldMetaData versionField = (meta == null) ? null : meta.getVersionField();
+        if (versionField != null)

Review Comment:
   Done, every block this PR touches.



##########
openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/VersionVal.java:
##########
@@ -98,23 +107,43 @@ public ExpState initialize(Select sel, ExpContext ctx, int flags) {
         // non-pc fields at all
         ClassMapping cls = _path.getClassMapping(state);
         if (cls == null || cls.getEmbeddingMapping() != null)
-            throw new UserException(_loc.get("bad-getobjectid", _path.getFieldMapping(state)));
+            throw new UserException(_loc.get("bad-version-path", pathDescription()));
 
         // types that are not versioned have no version columns to select,
         // group, order or compare by; fail with a meaningful message rather
         // than a NullPointerException further down the line
-        if (cls.getVersion() == null || cls.getVersion().getColumns().length == 0)
+        if (cls.getVersion().getColumns().length == 0)

Review Comment:
   Correct, it cannot. `ClassMapping._version` is `private final`, assigned unconditionally in both constructors (`ClassMapping.java:102` and `:112`, via `MappingRepository.newVersion()` which always returns `new Version(cls)`), there is no setter, and there's no subclass of `ClassMapping`/`MappingRepository` or override of `newVersion()`/`getVersion()` in the tree. An unresolved mapping still has one — `_version.resolve(...)` runs later and never clears it — and the embeddable case is rejected one line earlier by the `getEmbeddingMapping() != null` check. What an unversioned type gives you is a `Version` with no columns, and `getColumns()` is null-safe in itself (`_cols` starts as `EMPTY_COLUMNS`, `setColumns()` coerces null back), so the remaining `getColumns().length == 0` is the only check that means anything.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@openjpa.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org