Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why initialize id when is inherited from super class in JPA Hibernate

I have this super-class

open class Base(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long = 0,
    @Column var userId: Long? = 0,
    @Column var created: Date = Date(),
    @Column var updated: Date = Date(),
    @Column var currency: String = ""
)

which I then inherit in the following class

@Entity
@Table(name = "foo")
class Foo(
    @Column var name: String = "",
) : Base()

Now the compiler complains

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Persistent entity ‘Foo’ should have primary key

But the id is defined in Base. Why is it complaining?

>Solution :

But the id is defined in Base. Why is it complaining?

Possibly because Foo does not appear to inherit from Base. But even if it did inherit from Base, annotations on the superclass and its members are of interest to JPA only if the superclass is a @MappedSuperclass or an @Entity itself.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading