Spring Data Jpa之外键注解

一:@OneToOne的mappedBy不指定时。 

Customer表中不去指定@OneToOne的mappedBy条件

@Entity     // 作为hibernate 实体类
@Table(name = "tb_customer")       // 映射的表明
@Data
@EntityListeners(AuditingEntityListener.class)
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long custId; //客户的主键

    @Column(name = "cust_name")
    private String custName;//客户名称

    @Column(name="cust_address")
    private String custAddress;//客户地址


    // 单向关联  一对一
    /*
    * cascade 设置关联操作:
                    ALL,       所有持久化操作
                    PERSIST     只有插入才会执行关联操作
                    MERGE,      只有修改才会执行关联操作
                    REMOVE,     只有删除才会执行关联操作
      fetch 设置是否懒加载
                  EAGER 立即加载(默认)
                  LAZY 懒加载( 直到用到对象才会进行查询,因为不是所有的关联对象 都需要用到)
      orphanRemoval  关联移除(通常在修改的时候会用到)
          一旦把关联的数据设置null ,或者修改为其他的关联数据, 如果想删除关联数据, 就可以设置true
      optional  限制关联的对象不能为null
            true 可以为null(默认 ) false 不能为null
      mappedBy  将外键约束执行另一方维护(通常在双向关联关系中,会放弃一方的外键约束)
        值= 另一方关联属性名
    **/
    @OneToOne(
            cascade = CascadeType.ALL,fetch = FetchType.LAZY,orphanRemoval=true/*,optional=false*/)
    // 设置外键的字段名
    @JoinColumn(name="account_id")
    private Account account;
}

发现cascade=CascadeType.All,进行对Customer 进行持久化时候,会更新Accout表,即:级联

 Account表

@Entity
@Table(name="tb_account")
@Data
/*@Getter         //  生成所有属性的get方法
@Setter        //  生成所有属性的set方法
@RequiredArgsConstructor  //  生成final属性的构造函数, 如果没有final就是无参构造函数
@EqualsAndHashCode*/
public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String password;

}

测试

   // 插入
    @Test
    public void testC(){
        // 初始化数据
        Account account = new Account();
        account.setUsername("xushu");

        Customer customer = new Customer();
        customer.setCustName("徐庶");
        customer.setAccount(account);

        repository.save(customer);
    }

 此时更新 Customer表,发现Account表也被修改了。

执行后, Customer表为从表,Account表为主表

执行顺序:

 

Hibernate: create table tb_account (id bigint not null auto_increment, password varchar(255), username varchar(255), primary key (id)) engine=InnoDB

Hibernate: create table tb_customer (id bigint not null auto_increment, cust_address varchar(255), cust_name varchar(255), account_id bigint, primary key (id)) engine=InnoDB

Hibernate: alter table tb_customer add constraint FKebtvem8sp3670o0s0mtpuqpp5 foreign key (account_id) references tb_account (id)


Hibernate: insert into tb_account (password, username) values (?, ?)

Hibernate: insert into tb_customer (account_id, cust_address, cust_name) values (?, ?, ?)


二:@OneToOne的mappedBy指定时。

如果代码变为:

  Customer表,在@OneToOne中添加mappedBy = "customer"时

@Entity     // 作为hibernate 实体类
@Table(name = "tb_customer")       // 映射的表明
@Data
@EntityListeners(AuditingEntityListener.class)
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long custId; //客户的主键

    @Column(name = "cust_name")
    private String custName;//客户名称

    @Column(name="cust_address")
    private String custAddress;//客户地址


    // 单向关联  一对一
    /*
    * cascade 设置关联操作:
                    ALL,       所有持久化操作
                    PERSIST     只有插入才会执行关联操作
                    MERGE,      只有修改才会执行关联操作
                    REMOVE,     只有删除才会执行关联操作
      fetch 设置是否懒加载
                  EAGER 立即加载(默认)
                  LAZY 懒加载( 直到用到对象才会进行查询,因为不是所有的关联对象 都需要用到)
      orphanRemoval  关联移除(通常在修改的时候会用到)
          一旦把关联的数据设置null ,或者修改为其他的关联数据, 如果想删除关联数据, 就可以设置true
      optional  限制关联的对象不能为null
            true 可以为null(默认 ) false 不能为null
      mappedBy  将外键约束执行另一方维护(通常在双向关联关系中,会放弃一方的外键约束)
        值= 另一方关联属性名
    **/
    @OneToOne(mappedBy = "customer",
            cascade = CascadeType.ALL,fetch = FetchType.LAZY,orphanRemoval=true/*,optional=false*/)
    // 设置外键的字段名
    @JoinColumn(name="account_id")
    private Account account;
}

  

Account表添加:

    @OneToOne
    @JoinColumn(name="customer_id")
    private Customer customer;
时。

@Entity
@Table(name="tb_account")
@Data
/*@Getter         //  生成所有属性的get方法
@Setter        //  生成所有属性的set方法
@RequiredArgsConstructor  //  生成final属性的构造函数, 如果没有final就是无参构造函数
@EqualsAndHashCode*/
public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String password;

    @OneToOne
    @JoinColumn(name="customer_id")
    private Customer customer;

}

此时 Customer表为主表,Account表为从表,


执行顺序:

Hibernate: create table tb_account (id bigint not null auto_increment, password varchar(255), username varchar(255), primary key (id)) engine=InnoDB

Hibernate: create table tb_customer (id bigint not null auto_increment, cust_address varchar(255), cust_name varchar(255), account_id bigint, primary key (id)) engine=InnoDB

Hibernate: alter table tb_customer add constraint FKebtvem8sp3670o0s0mtpuqpp5 foreign key (account_id) references tb_account (id)

Hibernate: insert into tb_account (password, username) values (?, ?)

Hibernate: insert into tb_customer (account_id, cust_address, cust_name) values (?, ?, ?)

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值