spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
3.02k stars 1.42k forks source link

two entities with the same table name - merged table #3556

Closed cloner1998 closed 3 months ago

cloner1998 commented 3 months ago

Hello, I accidentally had two entities with the same table name (actually one without explicit Table annotation) and jpa without warning created a table that emerged from merging all columns of both entities (and unusable as it's firing wrong insert queries with fewer arguments than required - so it's not a merge)

I don't know if this is expected behavior or not but I was expecting a fail-fast behavior

here is my example:

@Entity
@Table(name = "a")
class A(
    @Id
    val a: Int,
    val c: Int,
    val aa: Int
)

@Entity
@Table(name = "a")
class B(
    @Id
    val b: Int,
    val c: Int,
    val bb: Int
)

which result in

create table public.a (
  a integer not null,
  aa integer not null,
  b integer primary key not null,
  bb integer not null,
  c integer not null
);

thanks in advance

quaff commented 3 months ago

I think it's reasonable, it's a valid use case to map the same table to different entities (full and brief). BTW, It's not handled by Spring Data JPA but JPA implementation such as Hibernate and EclipseLink.

schauder commented 3 months ago

As @quaff correctly noted this issue is in the realm of the JPA implementation, and not of Spring Data.

cloner1998 commented 3 months ago

thank you for correcting me have a good day