Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using abstract Base-Entity: ResultSet contains ID multiple times [DATAJDBC-579] #799

Closed
spring-projects-issues opened this issue Jul 16, 2020 · 3 comments
Assignees
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@spring-projects-issues
Copy link

Clemens Hahn opened DATAJDBC-579 and commented

I'm working with an abstract base entity model

abstract class BaseEntity(@Id open val id: Long?)

My concrete model extends this base entity like

data class Ticket(
        override val id: Long? = null,
        val title: String,
        val comments: MutableSet<Comment>
) : BaseEntity(id)

When reading from the TicketRepository I got the warning WARN 4594 — [ main] o.s.d.j.core.convert.ResultSetAccessor : ResultSet contains ID multiple times.

I also noticed that the ID field is two times present in the query: DEBUG 4594 — [ main] o.s.jdbc.core.JdbcTemplate : Executing prepared SQL statement [SELECT "TICKET"."ID" AS "ID", "TICKET"."ID" AS "ID", "TICKET"."TITLE" AS "TITLE" FROM "TICKET" WHERE "TICKET"."ID" = ?]

Is it possible to use base entities? I did not noticed this warning in spring-data-jdbc 1.1.5.RELEASE (but also the duplicate ID in the SELECT statement).

Please feel free to use my showcase in order to reproduce this scenario at GitHub (ticket/DATAJDBC-579 branch)


Affects: 2.1 M1 (2020.0.0)

@spring-projects-issues
Copy link
Author

Jens Schauder commented

I haven't looked into the example project to determine what is going on.

Spring Data JDBC doesn't care about inheritance. It just analyses the class at hand and works with whatever it finds.
Why it seems to find two id properties is currently not clear to me.
I'll have to investigate that.

The warning really just tells you that the query result is weird, because it doesn't have a single column per field, but multiple columns. As long as both columns contain the same value and the JDBC driver doesn't complain everything should be fine

@schauder
Copy link
Contributor

This is ... interesting.

It looks like the ID property gets found twice, probably once in the base class and once in the inherited class.

In the select this leads to selecting it twice. On insert it leads to including it in the insert, which shouldn't happen, since the id is null and therefore should not be written to the database and instead be generated and returned by the database.

This behaviour should have been fixed by spring-projects/spring-data-commons#390

@schauder
Copy link
Contributor

I was misinterpreting the mentioned issue.

It removes overridden properties only when the type is not assignable.
This is not the case her.
What we have here is another variant for which the mentioned issue actually adds documentation:

Overriding actually creates two fields, which are both visible to Spring Data JDBC and therefore both get persisted, leading to the problems you are seeing.

To resolve this, mark the property in the base class with @Transient and move the @Id annotation and any other annotations to the sub class, like this:

    abstract class BaseEntity(@Transient open val id: Long?)

    data class Ticket(
        @Id override val id: Long? = null,
        val title: String
    ) : BaseEntity(id)

@schauder schauder closed this as not planned Won't fix, can't repro, duplicate, stale Jul 19, 2024
@schauder schauder added status: declined A suggestion or change that we don't feel we should currently apply and removed type: bug A general bug labels Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

2 participants