tomas
tomas

Reputation: 840

Spring JPA - Different entities with same id

I have two entities

@Entity
@Table(name = "view_a")
public class A extends BaseStringIdTableClass

@Entity
@Table(name = "view_b")
public class B extends BaseStringIdTableClass

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseStringIdTableClass implements Serializable {
  @Id
  private String id;

And in the database i have two views

select * from view_a
|ID|ColumnA|....
|34222|some Value|....

select * from view_b
|ID|ColumnB|...
|34222|lla lla|...

I have therefore in the database different views. But the rows in this different views have the same ID.

Now i try to read the entities with the standard CRUD Repository.

A a = aRepository.findById("34222").get();
B b = bRepository.findById("34222").get();

In this case i can not find entity b. If i swop the two lines of code i can not find entity a.

I think the persistence context can at one time containt for a specific ID only one entity? Is this right. What can i do?

Repository definitions

public interface ARepository extends JpaRepository<A, String>, QuerydslPredicateExecutor<A> {
public interface BRepository extends JpaRepository<B, String>, QuerydslPredicateExecutor<B> {

Upvotes: 0

Views: 2948

Answers (1)

tomas
tomas

Reputation: 840

Sleeping over one night always help....First sorry for my initial incomplete question.

The problem/error was the both entities extended the same abstract class. And in this abstract class the ID was definied.

Fix after this recogniton was easy. One of the entities does not extend my abstract class, but definies his own id. And now it works......

Upvotes: 2

Related Questions