[
https://issues.apache.org/jira/browse/CAY-2964?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Andrus Adamchik updated CAY-2964:
---------------------------------
Description:
Given this mapping:
{noformat}
<db-entity name="MEANINGFUL_PK_BIGINT_GENERATED">
<db-attribute name="PK" type="INTEGER" isPrimaryKey="true"
isGenerated="true" isMandatory="true"/>
<db-attribute name="TEST_ATTR" type="VARCHAR" length="255"/>
</db-entity>
<obj-entity name="MeaningfulPkBigintGenerated"
className="org.apache.cayenne.testdo.meaningful_pk.MeaningfulPkBigintGenerated"
dbEntityName="MEANINGFUL_PK_BIGINT_GENERATED">
<obj-attribute name="pk" type="java.math.BigInteger" db-attribute-path="PK"/>
<obj-attribute name="testAttr" type="java.lang.String"
db-attribute-path="TEST_ATTR"/>
</obj-entity> {noformat}
On adapters that do not support JDBC-generated keys (Oracle, Firebird,
FrontBase and default JdbcAdapter), committing a new object whose generated
meaningful PK is mapped to an ObjAttribute Java type different from the DB
column type throws:
{noformat}
java.lang.ClassCastException: class java.lang.Integer cannot be cast
to class java.math.BigInteger
at ReplacementIdVisitor.updateId(ReplacementIdVisitor.java:106){noformat}
Root cause: JdbcPkGenerator.generatePk() types the generated value from the DB
column only (Long for BIGINT, Integer otherwise).
ReplacementIdVisitor.updateId() then writes it directly into the meaningful-PK
property without coercion. Adapters using JDBC generated keys are unaffected
because BatchAction.typeForGeneratedPK() already coerces to the ObjAttribute
type.
_Does it also affect every other DbAdapter for tables with meaningful PKs that
are not auto-incremented?_
was:
This was discovered on On adapters that do not support JDBC generated keys
(Oracle, Firebird, FrontBase and default JdbcAdapter), committing a new object
whose generated meaningful PK is mapped to an ObjAttribute Java type different
from the DB column type throws:
{noformat}
java.lang.ClassCastException: class java.lang.Integer cannot be cast
to class java.math.BigInteger
at ReplacementIdVisitor.updateId(ReplacementIdVisitor.java:106){noformat}
Root cause: JdbcPkGenerator.generatePk() types the generated value from the DB
column only (Long for BIGINT, Integer otherwise).
ReplacementIdVisitor.updateId() then writes it directly into the meaningful-PK
property without coercion. Adapters using JDBC generated keys are unaffected
because BatchAction.typeForGeneratedPK() already coerces to the ObjAttribute
type.
> ClassCastException for non-generated meaningful PKs
> ---------------------------------------------------
>
> Key: CAY-2964
> URL: https://issues.apache.org/jira/browse/CAY-2964
> Project: Cayenne
> Issue Type: Task
> Reporter: Andrus Adamchik
> Assignee: Andrus Adamchik
> Priority: Major
> Fix For: 5.0-M3
>
>
> Given this mapping:
> {noformat}
> <db-entity name="MEANINGFUL_PK_BIGINT_GENERATED">
> <db-attribute name="PK" type="INTEGER" isPrimaryKey="true"
> isGenerated="true" isMandatory="true"/>
> <db-attribute name="TEST_ATTR" type="VARCHAR" length="255"/>
> </db-entity>
> <obj-entity name="MeaningfulPkBigintGenerated"
> className="org.apache.cayenne.testdo.meaningful_pk.MeaningfulPkBigintGenerated"
> dbEntityName="MEANINGFUL_PK_BIGINT_GENERATED">
> <obj-attribute name="pk" type="java.math.BigInteger"
> db-attribute-path="PK"/>
> <obj-attribute name="testAttr" type="java.lang.String"
> db-attribute-path="TEST_ATTR"/>
> </obj-entity> {noformat}
> On adapters that do not support JDBC-generated keys (Oracle, Firebird,
> FrontBase and default JdbcAdapter), committing a new object whose generated
> meaningful PK is mapped to an ObjAttribute Java type different from the DB
> column type throws:
> {noformat}
> java.lang.ClassCastException: class java.lang.Integer cannot be cast
> to class java.math.BigInteger
> at ReplacementIdVisitor.updateId(ReplacementIdVisitor.java:106){noformat}
> Root cause: JdbcPkGenerator.generatePk() types the generated value from the
> DB column only (Long for BIGINT, Integer otherwise).
> ReplacementIdVisitor.updateId() then writes it directly into the
> meaningful-PK property without coercion. Adapters using JDBC generated keys
> are unaffected because BatchAction.typeForGeneratedPK() already coerces to
> the ObjAttribute type.
>
> _Does it also affect every other DbAdapter for tables with meaningful PKs
> that are not auto-incremented?_
--
This message was sent by Atlassian Jira
(v8.20.10#820010)