我有一个小样本应用程序,我想在其中实现hibernate级联效果,其中我有一个人模型和一个信用卡模型。一个人可以拥有一张以上的信用卡。
以下是人物模型:
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* Represents a person entity
*
* @author
*
*/
@Entity
@Table(name = "PERSON")
public class Person implements Serializable {
private static final long serialVersionUID = -5527566248002296042L;
public Person() {
}
public Person(String furstBame) {
this.firstName = furstBame;
}
@Id
@Column(name = "ID")
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "MONEY")
private Double money;
@OneToMany (mappedBy="person")
private List<CreditCard> creditCards;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public List<CreditCard> getCreditCards() {
return creditCards;
}
public void setCreditCards(List<CreditCard> creditCards) {
this.creditCards = creditCards;
}
}
这是信用卡型号:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* Represents a credit card entity
*
*
*/
@Entity
@Table(name = "CREDIT_CARD")
public class CreditCard implements Serializable {
private static final long serialVersionUID = 5924361831551833717L;
public CreditCard() {
System.out.println("CreditCard");
}
@Id
@Column(name = "ID")
@GeneratedValue
private Integer id;
@Column(name = "TYPE")
private String type;
@Column(name = "NUMBER")
private String number;
@ManyToOne
@JoinColumn (name="person_ID")
private Person person;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
请编辑代码和任何人请解释在这些情况下添加级联的实际语法。我使用的是注释,而不是XML的东西。
答案 0 :(得分:0)
您需要使用:
@OneToMany (mappedBy="person" cascade=CascadeType.ALL)
如果您希望在删除此人时删除个人信用卡,则还需要指定:
@OneToMany (mappedBy="person" cascade=CascadeType.ALL, orphanRemoval=true)