{excerpt:hidden=true}Embeddable samples{excerpt} h2. Embeddable classes An entity may use other fine-grained classes to represent entity state. Instances of these classes, unlike entity instances, do not have persistent identity of their own. Instead, they exist only as part of the state of the entity to which they belong. (From 2.0 Proposed Final Draft March 13, 2009 -- do I need to provide footnote or something?) In short, an embeddable is a seperation of data into a Java class that relies on the owning Entity for it's identity. Many(most) times an embeddable resides in the same database row as the owning Entity. Review the [getting started|Getting started] page on how to run the samples. h2. Samples ||Schema||Class diagram|| |!embeddables.jpeg!|!embeddables_entities.jpeg! [#Collections of Embeddables] [#Relationships from Embeddables] [#Nested Embeddables] h2. Collections of Embeddables In the code snippet below, there is a User Entity which has a collection of Embedded addresses. {code:title=Address.java|borderStyle=solid} @Embeddable public class Address { @Basic private String street; @Basic private String city; @Basic private String state; @Basic private Integer zip; public Address(){ } //... } {code} {code:title=User.java|borderStyle=solid} @Entity public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @ElementCollection @CollectionTable(name="user_address") private Set
addresses = new HashSet
(); public User(){ } //... } {code} {code:title=JPQL.java|borderStyle=solid} ... // Select Entity based off a query over a collection of embeddables Query q = em.createQuery("SELECT u FROM User u , in (u.addresses) a WHERE a.state='xx'"); // TODO -- add more! ... {code} h2. Relationships from Embeddables In the code snippet below, there is an Address embeddable with a ManyToOne relationship to a Coordinates Entity. {code:title=Address.java|borderStyle=solid} @Embeddable public class Address { @Basic private String street; @Basic private String city; @Basic private String state; @Basic private Integer zip; @ManyToOne(cascade=CascadeType.ALL) Coordinates coordinates; public Address(){ } //... } {code} {code:title=Coordinates .java|borderStyle=solid} @Entity public class Coordinates { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) int id; @Basic double longitude; @Basic double latitude; public Coordinates(){ } public Coordinates(double lon, double lat){ longitude=lon; latitude=lat; } //... } {code} {code:title=JPQL.java|borderStyle=solid} ... // Embedded -> relationship traversal Query q = em.createQuery("SELECT u FROM User u , in (u.addresses) a WHERE a.coordinates.longitude=xxx"); // TODO -- add more! ... {code} h2. Nested Embeddables In the code snippet below, there is a User Entity which has an embedded ContactInfo. ContactInfo contains two other embeddeded embeddables, Address and Phone. {code:title=Address.java|borderStyle=solid} @Embeddable public class Address { @Basic private String street; @Basic private String city; @Basic private String state; @Basic private Integer zip; public Address(){ } //... } {code} {code:title=Phone.java|borderStyle=solid} @Embeddable public class Phone { @Basic private String phone_number; @Basic private String phone_type; //... } {code} {code:title=ContactInfo.java|borderStyle=solid} @Embeddable public class ContactInfo { public ContactInfo(){ } @Embedded Address homeAddress; @Embedded Phone homePhone; //... } {code} {code:title=User.java|borderStyle=solid} @Entity public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; @Embedded ContactInfo contactInfo; public User(){ } //... } {code} {code:title=JPQL.java|borderStyle=solid} ... // Nested embeddables traversal Query q = em.createQuery("SELECT u FROM User u WHERE u.contactInfo.homePhone.number='507-555-5555' AND u.contactInfo.homePhone.type='cell'"); // TODO -- add more! ... {code}