Close

JPA - CascadeType Examples

JPA JAVA EE 

@Entity
public class EntityC {
@Id
@GeneratedValue
private int myIdC;
@OneToOne(cascade = CascadeType.PERSIST)
private EntityD entityD;
.............
}

@Entity
public class EntityD {
@Id
@GeneratedValue
private int myIdD;
private String stringD;
.............
}
Original Post




Bidirectional @OneToMany on Map values:

@Entity
public class Employee {
@Id
@GeneratedValue
private long id;
private String name;
@OneToMany(mappedBy = "taskEmployee", cascade = CascadeType.ALL)
@MapKeyColumn(name = "TASK_DATE", nullable = true)
private Map<Date, Task> tasks;
.............
}

@Entity
public class Task {
@Id
@GeneratedValue
private long id;
private String name;
private String description;
@ManyToOne
@JoinColumn(name = "EMP_FK")
private Employee taskEmployee;
.............
}
Original Post




import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
public class Customer {
@Id
@GeneratedValue
private int id;
private String name;
private String address;
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
private List<OrderItem> orderItems = new ArrayList<>();
.............
}

import javax.persistence.*;

@Entity
public class OrderItem {
@Id
@GeneratedValue
private int id;
private String itemName;
private int quantity;
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "CUST_ID")
private Customer customer;
.............
}
Original Post




See Also