一对多注解
@OneToMany
作用
-
建立一对多的关系映射
属性
-
targetEntityClass:指定多的方的类的字节码
-
mappedBy:指定从表实体类中引用主表对象的名称,在哪方出现,哪方不维护外键
-
cascade:指定要使用的级联操作
-
fetch:指定是否采用延迟加载
-
orphanRemoval:是否使用孤儿删除
@ManyToOne
作用
-
建立多对一的关系
属性
-
targetEntityClass:指定一的一方实体类字节码
-
cascade:指定要使用的级联操作
-
fetch:指定是否采用延迟加载
-
optional:关联是否可选。如果设置为false,则必须始终存在非空关系
@JoinColumn
作用
-
定义主键字段和外键字段的对应关系,维护外键
属性
-
name:指定表中的外键字段的名称
-
referencedColumnName:指定引用主表的主键字段名称
-
unique:是否唯一,默认值不唯一
-
nullable:是否允许为空,默认值允许
-
insertable:是否允许插入,默认值允许
-
updatable:是否允许更新,默认值允许
-
columnDefinition:列的定义信息
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* @ClassName: Customer
* @Description:jpa的所有的注解都在一个叫javax.persistence包下
* @author jsz
* @date 2018年8月20日
*/
@Entity // 指定当前类是实体类,写上此注解用于在创建SessionFactory/EntityManager时,加载映射配置
@Table(name = "cst_customer") // 指定实体类和表之间的对应关系
public class Customer {
@Id
@Column(name = "cust_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long cust_id;
@Column(name = "cust_name")
private String cust_name;
@Column(name = "cust_source")
private String cust_source;
@Column(name = "cust_industry")
private String cust_industry;
@Column(name = "cust_level")
private String cust_level;
@Column(name = "cust_address")
private String cust_address;
@Column(name = "cust_phone")
private String cust_phone;
/*
* targetEntity:
* mappedBy:当前java类在对方的属性名,在哪里出现,那方不维护外键
*/
@OneToMany(targetEntity=LinkMan.class,mappedBy="customer",cascade=CascadeType.REMOVE)
private Set<LinkMan> linkMans = new HashSet<LinkMan>();
public Customer() {
}
public Customer(Long cust_id, String cust_name) {
this.cust_id = cust_id;
this.cust_name = cust_name;
}
public Set<LinkMan> getLinkMans() {
return linkMans;
}
public void setLinkMans(Set<LinkMan> linkMans) {
this.linkMans = linkMans;
}
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_address() {
return cust_address;
}
public void setCust_address(String cust_address) {
this.cust_address = cust_address;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_address=" + cust_address
+ ", cust_phone=" + cust_phone + "]";
}
}
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "cst_linkman")
public class LinkMan {
@Id
@Column(name = "lkm_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long lkm_id;// '联系人编号(主键)',
@Column(name = "lkm_name")
private String lkm_name;// '联系人姓名',
@Column(name = "lkm_gender")
private String lkm_gender;// '联系人性别',
@Column(name = "lkm_phone")
private String lkm_phone;// '联系人办公电话',
@Column(name = "lkm_mobile")
private String lkm_mobile;// '联系人手机',
@Column(name = "lkm_email")
private String lkm_email;// '联系人邮箱',
@Column(name = "lkm_qq")
private String lkm_qq;// '联系人qq',
@Column(name = "lkm_position")
private String lkm_position;// '联系人职位',
@Column(name = "lkm_memo")
private String lkm_memo;// '联系人备注',
@ManyToOne(targetEntity=Customer.class,cascade=CascadeType.PERSIST)
@JoinColumn(name="lkm_cust_id",referencedColumnName="cust_id")
private Customer customer;
public Long getLkm_id() {
return lkm_id;
}
public void setLkm_id(Long lkm_id) {
this.lkm_id = lkm_id;
}
public String getLkm_name() {
return lkm_name;
}
public void setLkm_name(String lkm_name) {
this.lkm_name = lkm_name;
}
public String getLkm_gender() {
return lkm_gender;
}
public void setLkm_gender(String lkm_gender) {
this.lkm_gender = lkm_gender;
}
public String getLkm_phone() {
return lkm_phone;
}
public void setLkm_phone(String lkm_phone) {
this.lkm_phone = lkm_phone;
}
public String getLkm_mobile() {
return lkm_mobile;
}
public void setLkm_mobile(String lkm_mobile) {
this.lkm_mobile = lkm_mobile;
}
public String getLkm_email() {
return lkm_email;
}
public void setLkm_email(String lkm_email) {
this.lkm_email = lkm_email;
}
public String getLkm_qq() {
return lkm_qq;
}
public void setLkm_qq(String lkm_qq) {
this.lkm_qq = lkm_qq;
}
public String getLkm_position() {
return lkm_position;
}
public void setLkm_position(String lkm_position) {
this.lkm_position = lkm_position;
}
public String getLkm_memo() {
return lkm_memo;
}
public void setLkm_memo(String lkm_memo) {
this.lkm_memo = lkm_memo;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
import static org.junit.Assert.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.junit.Test;
import com.itheima.domain.Customer;
import com.itheima.domain.LinkMan;
import com.itheima.utils.JPAUtils;
public class Demo02 {
/**
* @MethodName:test01
* @Description:级联保存,保存联系人的时候,保存客户
* @throws Exception
*/
@Test
public void test01() throws Exception {
EntityManager manger = JPAUtils.getEntityManager();
EntityTransaction tx = manger.getTransaction();
tx.begin();
Customer c1 = new Customer();
c1.setCust_name("张三");
LinkMan l1 = new LinkMan();
l1.setLkm_name("tom01");
LinkMan l2 = new LinkMan();
l2.setLkm_name("tom02");
l1.setCustomer(c1);
l2.setCustomer(c1);
manger.persist(l1);
manger.persist(l2);
tx.commit();
manger.close();
}
/**
* @MethodName:test02
* @Description:级联删除,删除用户时,删除关联的联系人
* @throws Exception
*/
@Test
public void test02() throws Exception {
EntityManager manger = JPAUtils.getEntityManager();
EntityTransaction tx = manger.getTransaction();
tx.begin();
Customer customer = manger.find(Customer.class, 2l);
manger.remove(customer);
tx.commit();
manger.close();
}
}