• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

(JPA) questions about embeddable class

 
Cainiao Zou
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Can I embed an embeddable class with @embedded in an embeddable class? (nest) I got a Attribute "xxx" has invalid mapping type in this context error.
2. Must the embeddable implement the Serializable interface?

thanks
 
Ran Pleasant
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cainiao Zou wrote:1. Can I embed an embeddable class with @embedded in an embeddable class? (nest) I got a Attribute "xxx" has invalid mapping type in this context error.
2. Must the embeddable implement the Serializable interface?


As you know the "@embedded" annotation indicates that an instance of a class can be imbedded into an instance of another class. In the class that will contain the embedded class you must use annotations to define the mapping between the properties of the enbedded object and columns of the database table to which the containing class is mapped.

You can actually do the mapping in the imbedded class but this will limit you to using the embedded class only in situations where the database columns match up. For example, a table might have fields for two addresses, such as a person table with home and work addresses or an invoice header table with shipTo and billTo addresses. By putting all column mappings in the containing class you can use instances of a single embeddable Address class to encapsulates all of the addresses.
 
Cainiao Zou
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ran Pleasant wrote:
Cainiao Zou wrote:1. Can I embed an embeddable class with @embedded in an embeddable class? (nest) I got a Attribute "xxx" has invalid mapping type in this context error.
2. Must the embeddable implement the Serializable interface?


As you know the "@embedded" annotation indicates that an instance of a class can be imbedded into an instance of another class. In the class that will contain the embedded class you must use annotations to define the mapping between the properties of the enbedded object and columns of the database table to which the containing class is mapped.

You can actually do the mapping in the imbedded class but this will limit you to using the embedded class only in situations where the database columns match up. For example, a table might have fields for two addresses, such as a person table with home and work addresses or an invoice header table with shipTo and billTo addresses. By putting all column mappings in the containing class you can use instances of a single embeddable Address class to encapsulates all of the addresses.


I think you mean, a class can contain more embedded classes, just like your example person Entity contain two Addresses fields
But what about the situation like this?
Entity A contains the instance of embedded class B, and at the same time B contains an embedded class C, for example Person Entity contains Address, and the Address contains a Phone class, which is also an embedded class.
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JPA 1.0 did not require support for nested Embeddables, but JPA 2.0 does support it.

See,

http://en.wikibooks.org/wiki/Java_Persistence/Embeddables#Nesting

So depends on your JPA provider, and what version it supports.

An Embeddable does not need to be serializable, unless you intend to serialize it.
 
Cainiao Zou
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sutherland wrote:JPA 1.0 did not require support for nested Embeddables, but JPA 2.0 does support it.

See,

http://en.wikibooks.org/wiki/Java_Persistence/Embeddables#Nesting

So depends on your JPA provider, and what version it supports.

An Embeddable does not need to be serializable, unless you intend to serialize it.


thank you very much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic