Hibernate-3

Cont..Hibernate-2 

(5.2)By-Directional Relationship

In a bi-directional relation both sides know about the other side.So both sides can access another side.
Owner end data can fetch using inverse end & inverse end data can fetch using owner end

Criteria:
    1. Selection of one entity to exchange pk to fk or DBMS  rules
    2.Using @JoinTable
    3.Decomposing
  
(1)1-1 Relationship
1-1(Total participation)
1. Selection of one entity to exchange pk to fk or DBMS  rules


Girl.java
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@Entity

public class Girl {
    @Id
    private int id;
    private String name;

    @OneToOne
//If do not add unique=true it will be not a 1-1 relationship,It will be 1-m
    @JoinColumn(name="boy_id", referencedColumnName = "id",unique = true)
    private Boy boy;
}

Boy.java
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString(exclude = "girl")
@Entity
public class Boy {
    @Id
    private int id;
    private String name;
    @OneToOne(mappedBy = "boy")
//    @Setter(AccessLevel.NONE)//disable to create Setter in inverse end
    private Girl girl;

    public Boy(int id, String name) {
        this.id = id;
        this.name = name;
    }

//Create utility method & enable Setter methods in inverse end
    public void setGirl(Girl girl){
        girl.setBoy(this);
        this.girl = girl;
    }
}

1-1(Full partial participation)
 2.Using @JoinTable

Girl2.java
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString(exclude = "boy")
@Entity
public class Girl2 {
    @Id
    private int id;
    private String name;
    @OneToOne(mappedBy = "girl")
    private Boy2 boy;

    public Girl2(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public void setBoy(Boy2 boy) {
        boy.setGirl(this);
        this.boy = boy;
    }
}

Boy2.java
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@ToString
@Entity
public class Boy2 {
    @Id
    private int id;
    private String name;
    @OneToOne
    @JoinTable(name = "BoyGirl",
            joinColumns = @JoinColumn(name = "boy_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "girl_id", referencedColumnName = "id", unique = true))
    private Girl2 girl;

    public Boy2(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

(1)1-M Relationship
1. Selection of one entity to exchange pk to fk or DBMS  rules
1-M(Total participation)

Customer.java
@AllArgsConstructor
@NoArgsConstructor
@ToString(exclude = "orderList")
@Getter
@Setter
@Entity
public class Customer {
    @Id
    private int id;
    private String name;
    @OneToMany(mappedBy = "customer", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Order> orderList;

    public Customer(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public void setOrderList(List<Order> orderList) {
        for (Order order : orderList) {
            order.setCustomer(this);
        }
        this.orderList = orderList;
    }
}

Order.java
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Getter
@Setter
@Entity
@Table(name="`Order`")
public class Order {
    @Id
    private int id;
    private Date date;
    @ManyToOne(cascade = {CascadeType.PERSIST}, fetch = FetchType.LAZY)
    @JoinColumn(name="customer_id", referencedColumnName = "id", nullable = false)
    private Customer customer;
}

1-M(Full partial participation)

Customer2.java
//@AllArgsConstructor
@NoArgsConstructor
@ToString(exclude = "orderList")
@Getter
@Setter
@Entity
public class Customer2 {
    @Id
    private int id;
    private String name;
    @OneToMany(mappedBy = "customer", cascade = CascadeType.PERSIST)
    @Setter(AccessLevel.NONE)
    private List<Order2> orderList;

    public Customer2(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Customer2(int id, String name, List<Order2> orderList) {
        this.id = id;
        this.name = name;
        if (orderList != null){
            throw new UnsupportedOperationException("Order list can't be set due to bi-directional conflicts.");
        }
    }

//Can full accessibility of setter method of inverse end ,of use the methods must remove  @Setter(AccessLevel.NONE) 

    //    public void setOrderList(List<Order2> orderList) {
//        for (Order2 order : orderList) {
//            order.setCustomer(this);
//        }
//        this.orderList = orderList;
//    }

//    public void addOrder(Order2 order){
//        order.setCustomer(this);
//        this.orderList.add(order);
//    }
//
//    public void removeOrder(Order2 order){
//        if (order.getCustomer() == this) {
//            order.setCustomer(null);
//        }else{
//            throw new RuntimeException("Invalid Order");
//        }
//    }
}

}

Order2.java
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Getter
@Setter
@Entity
public class Order2 {
    @Id
    private int id;
    private Date date;
    @ManyToOne(cascade = {CascadeType.PERSIST})
    @JoinTable(name = "CustomerOrder",
            joinColumns = @JoinColumn(name = "order_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "customer_id", referencedColumnName = "id"))
    private Customer2 customer;
}

Cascading
Simply we can if perform action to parent entity it will affect to child entity or vise versa,either or else.We can set that to the relationship. in the entity
Eg;-
@ManyToOne(cascade = {CascadeType.PERSIST})
@OneToOne(mappedBy = "girl", cascade = {CascadeType.PERSIST,CascadeType.REMOVE})


Cascading is about persistence actions involving one object propagating to other objects via an association. 
If set (cascade = {CascadeType.All})->Will execute all methods in NH.But not the good practice must narrow down these context,regrading to entity requirements.

Fetching(more...)








Comments

Popular posts from this blog

Hibernate-1

Map Interface using Java

GIT & Version Controlling