W
W
Wan-Derer2021-11-26 10:18:38
Java
Wan-Derer, 2021-11-26 10:18:38

spring data. Can an object not be an Entity?

I have a table that does not have a Primary Key. It is not required to contain unique data in any of the fields, or in any combination of them, i.e. I can't even make a Composite Primary Key. I need to subtract lines from it one by one, preferably in the order they were added (but not necessarily, you can also in any order).

All the Spring Data JPA examples I've seen are based on Entity, i.e. must have a unique ID, which is not applicable in this case.

Question: is it possible to work with such a table using Spring Data (except for writing a direct SQL query)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BorLaze, 2021-11-26
@BorLaze

https://en.wikibooks.org/wiki/Java_Persistence/Ide...

If your table truly has no unique columns, then use all of the columns as the id. Typically when this occurs the data is read-only, so even if the table allows duplicate rows with the same values, the objects will be the same anyway, so it does not matter that JPA thinks they are the same object. The issue with allowing updates and deletes is that there is no way to uniquely identify the object's row, so all of the matching rows will be updated or deleted.

That is, it will be possible to work, with one limitation - all identical records will "collapse" into one. But considering
Rows with the same data are the same type of events, each subsequent one is guaranteed to "overlap" the previous one.
just what you need.

J
Jacen11, 2021-11-26
@Jacen11

what kind of nonsense is this. Each entry in the database must have an id. And that's all in general. Here is the unique value. How is this not applicable?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question