...
Code Block |
---|
title | User.java |
---|
borderStyle | solid |
---|
|
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@ElementCollection
@CollectionTable(name="user_address")
@OrderColumn
private Set<Address> addresses = new HashSet<Address>();
public User(){
}
//...
}
|
Code Block |
---|
title | JPQL.java |
---|
borderStyle | solid |
---|
|
// Select a collection of embeddables
Query q = em.createQuery("SELECT u FROM User u , in (u.addresses) a WHERE a.state='xx'");
// TODO -- add more!
|
Relationships from Embeddables
...
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;
}
//...
}
|
Code Block |
---|
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!
|
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(){
}
//...
}
|
Code Block |
---|
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!
|