一、编写两个实体类
1、一对一关系实现:a:使用外键关联 b:使用主键关联,两个表的主键相同
2、外键方案:配置关联关系:两个实体类互相关联,并且在关联的属性上添加一个@OneToOne代表一个对一个的关系;
在属性上添加@JoinColumn nme 存储外键的字段名称 referencedColumnNmae 对方表的字段名称 (可以省略)
3、级联操作: 需要再@OneToOne 注解中添加一个属性 casecade
CascadeType.PERSIST : 级联添加’ CascadeType.MEGGE : 级联更新’ CascadeType.REMOVE : 级联删除’ CascadeType.ALL : 增删改都用级联操作’
4、主键关联方案:不在使用@JoinColumn注解;使用@PrimaryKeyJoinColumn 注解不需要配置属性;两个实体类关联属性上都要添加
实体类一、
@Entity @Table(name = "cst_customer") public class Customer { // 配置主键自增的策略 @GeneratedValue(strategy = GenerationType.IDENTITY) @Id @Column(name="cust_id") private long custId; @Column(name="cust_name") private String custName; @Column(name="cust_source") private String custSource; @Column(name="cust_indutry") private String custIndutry; @Column(name="cust_level") private String custLevel; @Column(name="cust_address") private String custAddress; @Column(name="cust_phone") private String custPhone; //表一对一关系 @OneToOne //使用外键关联表 @JoinColumn(name = "extid",referencedColumnName = "ext_id") //使用主键关联表 // @PrimaryKeyJoinColumn private CustomerExt customerExt; public CustomerExt getCustomerExt() { return customerExt; } public void setCustomerExt(CustomerExt customerExt) { this.customerExt = customerExt; } }
实体类二、
@Entity
@Table(name = "cst_customer_ext")
public class CustomerExt {
@Id
@Column(name="ext_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long extId;
private String memo;
private String info;
@OneToOne
@JoinColumn(name="custid",referencedColumnName = "cust_id")
// 主键关联
// @PrimaryKeyJoinColumn
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
二、编写dao:两个dao都要继承JpaRepository<CustomerExt,Long>
dao一、 public interface CustomerExtDao extends JpaRepository<CustomerExt,Long> { } dao二、 public interface CustomerDao extends JpaRepository<Customer,Long> { }
三、测试
package cn.zrf.jpa; import cn.zrf.jpa.dao.CustomerDao; import cn.zrf.jpa.dao.CustomerExtDao; import cn.zrf.jpa.entity.Customer; import cn.zrf.jpa.entity.CustomerExt; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Commit; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class OneToOneTest { @Autowired private CustomerDao customerDao; @Autowired private CustomerExtDao customerExtDao; @Test @Transactional @Commit public void addCustomer(){ // 1创建Customer 对象 Customer customer = new Customer(); customer.setCustName("张无忌"); customer.setCustAddress("光明顶"); customer.setCustLevel("来了老弟"); // 2创建CustomerExt 对象 CustomerExt customerExt = new CustomerExt(); customerExt.setMemo("我是你哥哥"); customerExt.setInfo("371826"); //3配置对象之间的关联关系 customer.setCustomerExt(customerExt); customerExt.setCustomer(customer); // 4 把对象写入数据路 customerDao.save(customer); customerExtDao.save(customerExt); } }