I'm using spring CrudRepository
throughout my application. Now I want to also create one for an @Entity
that does not have an @Id
. Is that possible at all?
//probably ID is always required? public interface Repository<T, ID extends Serializable>
If your object does not have an id, but its' table does, this is fine. Make the object an Embeddable object, embeddable objects do not have ids. You will need a Entity that contains this Embeddable to persist and query it.
JPA requires that every entity has an ID. So no, entity w/o an ID is not allowed. Every JPA entity must have a primary key.
Id is required by JPA, but it is not required that the Id specified in your mapping match the Id in your database. For instance you can map a table with no id to a jpa entity. To do it just specify that the "Jpa Id" is the combination of all columns.
It is indeed not necessary to put the @Repository annotation on interfaces that extend JpaRepository ; Spring recognises the repositories by the fact that they extend one of the predefined Repository interfaces.
JPA requires that every entity has an ID. So no, entity w/o an ID is not allowed.
Every JPA entity must have a primary key.
from JPA spec
You may want to read more about how JPA handles a case when there's no id on the DB side from here (see 'No Primary Key').
Alternatively you can extend AbstractPersistable<Long>
for your all POJO entities.
Follow example: - https://github.com/spring-projects/spring-data-examples/blob/master/jpa/example/src/main/java/example/springdata/jpa/simple/User.java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With