This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

(JPA) questions about embeddable class

 
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
 
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.
 
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
 
please buy this thing and then I get a fat cut of the action:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic