vignesh-manel opened a new pull request, #28630:
URL: https://github.com/apache/camel/pull/28630

   # Description
   
   https://issues.apache.org/jira/browse/CAMEL-26147
   
   ## Problem
   
   A JPA consumer whose query selects values that are not entities, such as a 
column (`select x.address from SendEmail x`), delivered nothing with the 
default `consumeLockEntity=true`. `JpaConsumer.lockEntity` calls 
`entityManager.lock` on each result; for a `String` or a `Long` the lock fails, 
and the catch block returned `entity.getClass().isArray()` (written for the 
arrays of native queries), which is false. `processBatch` then skipped every 
row, and the only trace was a DEBUG log. The endpoint's own body-type metadata 
says such a query "gives one row per message: an Object array, or the value of 
a single column".
   
   ## Change
   
   - New `protected boolean isEntity(Object, EntityManager)`: whether the 
result is an instance of an entity type of the metamodel 
(`getMetamodel().getEntities()` and `isInstance`, so a provider's subclass or 
proxy of an entity still counts as an entity).
   - `lockEntity`: when the lock fails, a result that is not an entity is 
processed (there is nothing to lock). For an entity the lock is attempted 
exactly as before, and a failed lock still skips it.
   - The `consumeDelete` handler removes only entities. Fixing the lock alone 
exposed this: `em.remove` on the `String` failed (`Attempt to cast instance 
"[email protected] [java.lang.String]" to PersistenceCapable failed`) and rolled 
back the whole poll. For any other result it now logs the WARN it already 
logged for the arrays of native queries (CAMEL-24131), with the message 
extended to selected columns.
   - Doc: for a List body the doc said "use *entityType=java.util.List* as a 
configuration". It is the entity class name of the endpoint, 
`jpa:java.util.List`; the paragraph now says so, and that a List or array sent 
to `jpa:<entity class>` fails. Both copies of `jpa-component.adoc` are updated.
   - Upgrade guide: such a query now delivers its values, and with the default 
`consumeDelete=true` they are not deleted, so each poll delivers them again; 
set `consumeDelete=false`.
   - New `JpaConsumerScalarResultTest` (JUnit assertions, like the other tests 
of the module).
   
   Not changed: the ticket also says the doc sentence "For sending to the 
endpoint, the _entityClassName_ is optional" is wrong because the path is 
`required = true` in the metadata. I checked it with a temporary test: 
`jpa:?usePersist=true` with an entity body persisted it. Only a bare `jpa:` 
fails, and that is URI syntax. So I left that sentence as it is.
   
   ## Verification (local, JDK 21, Maven 3.9.12)
   
   - Before the fix, the new test failed: `mock://result Received message 
count. Expected: <2> but was: <0>`, although the poll returned 2.
   - With only the `lockEntity` change, it failed with the `PersistenceCapable` 
error above. With both changes it passes: the 2 values are delivered, the 2 
rows stay, the next poll delivers them again, and the WARN is logged.
   - A temporary test (not committed) for the doc: a `List` or a `SendEmail[]` 
sent to `jpa:org.apache.camel.examples.SendEmail` fails with 
`InvalidPayloadRuntimeException`, and a `List` sent to `jpa:java.util.List` is 
persisted.
   - Full camel-jpa test suite: 53 classes, 124 tests, 0 failures, 1 skipped.
   - `mvn install -Psourcecheck` passes in `components/camel-jpa`, with no 
uncommitted changes afterwards.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   
   - [ ] I have run `mvn clean install -DskipTests` locally from root folder 
and I have committed all auto-generated changes.
     _Not from the root: I built `components/camel-jpa` with `-Psourcecheck` 
and copied the doc to the catalog. CI's uncommitted-changes check covers the 
rest._
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
   
   _Claude Code on behalf of vignesh-manel_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to