Is the JPA @Embedded annotation mandatory?

后端 未结 2 1092
既然无缘
既然无缘 2021-01-08 01:07

I have tried omitting the @Embedded annotation and still the fields have been embedded in the table. I cannot find anything which would say tha

相关标签:
2条回答
  • 2021-01-08 01:52

    Embedded-Embeddable is not mandatory, but it gives you nice OOP perspective of your entities' relationship. Another way to do such a thing - is to use OneToOne mapping. But in such a case entity WILL be written to separate table (while in case of embedded it CAN be written to the separate table in your DB).

    0 讨论(0)
  • 2021-01-08 02:11

    In case of using Hibernate it does not matter if you annotate the field itself (as @Embedded) or if you annotate the referenced class (as @Embeddable). At least one of both is needed to let Hibernate determine the type.

    And there is a (implicit) statement about this inside the Hibernate documentation, take a look here: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#mapping-declaration-component

    It says:

    The Person entity has two component properties, homeAddress and bornIn. homeAddress property has not been annotated, but Hibernate will guess that it is a persistent component by looking for the @Embeddable annotation in the Address class.

    0 讨论(0)
提交回复
热议问题