...
Table of Contents |
---|
minLevel | 2 |
---|
indent | 20px |
---|
style | disc |
---|
|
Collections Embeddables
In the code snippet below, there is a User2 Entity which has a collection of ordered Embedded address.
Code Block |
---|
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 Block |
---|
title | User2.java |
---|
borderStyle | solid |
---|
|
@Entity
public class User2 {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@ElementCollection
@CollectionTable(name="user_addresses")
@OrderColumn
private Set<Address> addresses = new HashSet<Address>();
public User2(){
}
//...
}
|
Relationships from Embeddables
In the code snippet below, there is an Address embeddable with a ManyToOne relationship to a Coordinates Entity.
Code Block |
---|
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 Block |
---|
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;
}
//...
}
|
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 Block |
---|
title | User.java |
---|
borderStyle | solid |
---|
|
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Embedded
ContactInfo contactInfo;
public User(){
}
//...
}
|