Mapping Relationships

Type Relationships

Type Relationships

  • One-to-One

    • Unidirectional One-to-One

    • Bidirectional One-to-One

Type Relationships

  • Many-to-One

    • Unidirectional Many-to-One

    • Unidirectional One-to-Many

    • Bidirectional Many-to-One

Type Relationships

  • Many-to-Many

    • Unidirectional Many-to-Many

    • Bidirectional Many-to-Many

One-to-One

Unidirectional One-to-One

@Entity
public class Customer {
    @OneToOne
    @JoinColumn(name = "fk_shippingaddress")
    private ShippingAddress shippingAddress;

    // code
}

Unidirectional One-to-One

public class Example {
    public static void main(String[] args){
        // code
        Customer c = em.find(Customer.class, 1L);
        ShippingAddress sa = c.getShippingAddress();
    }
}

Bidirectional One-to-One

@Entity
public class Customer {
    @OneToOne
    @JoinColumn(name = "fk_shippingaddress")
    private ShippingAddress shippingAddress;

    // code
}

Bidirectional One-to-One

@Entity
public class ShippingAddress {
    @OneToOne(mappedBy = "shippingAddress")
    private Customer customer;

    // code
}

One-to-Many

Unidirectional Many-to-One

@Entity
public class OrderItem {
    @ManyToOne
    private Order order;

    // code
}

Unidirectional Many-to-One

@Entity
public class OrderItem {
    @ManyToOne
    @JoinColumn(name = "fk_order")
    private Order order;

    // code
}

Unidirectional Many-to-One

public class Example {
    public static void main(String[] args){
        // code
        Order o = em.find(Order.class, 1L);

        OrderItem i = new OrderItem();
        i.setOrder(o);

        em.persist(i);
    }
}

Unidirectional One-to-Many

@Entity
public class Order {
    @OneToMany
    private List<OrderItem> items = new ArrayList<OrderItem>();

    // code
}

Unidirectional One-to-Many

@Entity
public class Order {
    @OneToMany
    @JoinColumn(name = "fk_order")
    private List<OrderItem> items = new ArrayList<OrderItem>();

    // code
}

Unidirectional One-to-Many

public class Example {
    public static void main(String[] args){
        // code
        Order o = em.find(Order.class, 1L);

        OrderItem i = new OrderItem();

        o.getItems().add(i);

        em.persist(i);
    }
}

Bidirectional Many-to-One

@Entity
public class OrderItem {
    @ManyToOne
    @JoinColumn(name = "fk_order")
    private Order order;

    // code
}

Bidirectional Many-to-One

@Entity
public class Order {
    @OneToMany(mappedBy = "order")
    private List<OrderItem> items = new ArrayList<OrderItem>();

    // code
}

Bidirectional Many-to-One

public class Example {
    public static void main(String[] args){
        // code
        Order o = em.find(Order.class, 1L);

        OrderItem i = new OrderItem();
        i.setOrder(o);

        o.getItems().add(i);

        em.persist(i);
    }
}

Many-to-Many

Unidirectional Many-to-Many

@Entity
public class Store {
    @ManyToMany
    private Set<Product> products = new HashSet<Product>();

    // code
}

Unidirectional Many-to-Many

@Entity
public class Store {
    @ManyToMany
    @JoinTable(name = "store_product",
           joinColumns = { @JoinColumn(name = "fk_store") },
           inverseJoinColumns = { @JoinColumn(name = "fk_product") })
    private Set<Product> products = new HashSet<Product>();

    // code
}

Unidirectional Many-to-Many

public class Example {
    public static void main(String[] args){
        // code
        Store s = em.find(Store.class, 1L);

        Product p = new Product();

        s.getProducts().add(p);

        em.persist(p);
    }
}

Bidirectional Many-to-Many

@Entity
public class Store {
    @ManyToMany
    @JoinTable(name = "store_product",
           joinColumns = { @JoinColumn(name = "fk_store") },
           inverseJoinColumns = { @JoinColumn(name = "fk_product") })
    private Set<Product> products = new HashSet<Product>();

    // code
}

Bidirectional Many-to-Many

@Entity
public class Product {
    @ManyToMany(mappedBy="products")
    private Set<Store> stores = new HashSet<Store>();

    // code
}

Fetch strategies

Fetch strategies

  • LAZY - FetchType.LAZY

  • EAGER - FetchType.EAGER

Fetch strategies

  • @OneToMany

    • LAZY

  • @ManyToOne

    • EAGER

  • @ManyToMany

    • LAZY

  • @OneToOne

    • EAGER

Каскадирование

Каскадные типы

  • CascadeType.PERSIST

  • CascadeType.REMOVE

  • CascadeType.DETACH

  • CascadeType.MERGE

  • CascadeType.REFRESH

  • CascadeType.ALL