简体   繁体   中英

What is the difference between @EmbeddedId and @Id in Spring JPA?

I have a composite key class annotated with @Embeddable . Then I have my entity class using this composite key class as its primary key. I tried to annotate it with either @Id and @EmbeddedId , both seems to work? Is there any functional difference except for readability?

eg

@Embeddable public class CompositeKey { ... }
@Entity public class MyEntity {
    @EmbeddedId private CompositeKey id
}

@Embeddable public class CompositeKey { ... }
@Entity public class MyEntity {
    @Id private CompositeKey id
}

I've always used @EmbeddedId in case of Composite primary keys and @Id in case of simple primary key.

I'm surprised how @Id is also working.

Other than readability, I believe you can skip using @Embeddable annotation with @EmbeddedId but won't be able to do that with @Id annotation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM