欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Springboot JPA级联操作

程序员文章站 2022-03-02 19:03:25
...

自从接触了JPA,就对立面的级联操作很费解,特此做一下学习。JPA提供的Entity级联可以建立一对一、一对多、多对多的关系,覆盖查询、保存、刷新、删除等操作。
(1)一对一,@OneToOne
TUser与TGender成一对一关系

@Entity
@Data
@Table(name = "user",schema = "hr")
@JsonIgnoreProperties(value = {"handler","hibernateLazyInitializer","fieldHandler"})
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class TUser {
    @Id
    @GeneratedValue(generator="jpa-uuid")
    @Column(name = "user_id")
    private String id;

    @Column(name = "user_name")
    private String name;

    @Column(name = "user_describe")
    private String desc;

   //@JoinColumn的name指的是当前表user的字段gender_id,referencedColumnName指关联表gender的字段id
    @OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.ALL})
    @JoinColumn(name = "gender_id",referencedColumnName = "id")
    private TGender genderId;
}

而TGender为:

@Entity
@Data
@Table(name = "gender",schema = "hr")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonIgnoreProperties(value = {"handler","hibernateLazyInitializer","fieldHandler"})
public class TGender {
    @Id
    @GenericGenerator(name = "jpa-uuid", strategy = "uuid")
    private String id;

    private String sex;
	
	//可维护可不维护,mappedBy就是被维护端,被TUser类中的属性“genderId”维护
    @OneToOne(mappedBy = "genderId", cascade = {CascadeType.MERGE, CascadeType.REFRESH}, optional = false)
    private TUser tUser;

(2)一对多,@OneToMany 和 @ManyToOne
两个是一起用的,以Company和User为例子,一个公司有多个职工

@Entity
@Data
@Table(name = "user",schema = "hr")
@JsonIgnoreProperties(value = {"handler","hibernateLazyInitializer","fieldHandler"})
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class TUser {
    @Id
    @GeneratedValue(generator="jpa-uuid")
    @Column(name = "user_id")
    private String id;

    @Column(name = "user_name")
    private String name;

    @Column(name = "user_describe")
    private String desc;

    @OneToOne(cascade = {CascadeType.MERGE, CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.ALL})
    @JoinColumn(name = "gender_id",referencedColumnName = "id")
    private TGender genderId;
   
    //@JoinColumn的name指的是当前表user的字段company_id,referencedColumnName指关联表company的字段id
    @ManyToOne(cascade = {CascadeType.ALL})
    @JoinColumn(name = "company_id",referencedColumnName = "id")
    private TCompany companyId;
}

TCompany 为:

@Entity
@Data
@Table(name = "company",schema = "hr")
@JsonIgnoreProperties(value = {"handler","hibernateLazyInitializer","fieldHandler"})
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class TCompany {

    @Id
    @GeneratedValue(generator="jpa-uuid")
    private String id;

    private String name;
    
   //查询时建议把userList忽略
   //可维护可不维护,mappedBy就是被维护端,被TUser类中的属性“companyId”维护
    @OneToMany(mappedBy="companyId",cascade = {CascadeType.ALL})
    private List<TUser> userList;
}

(3)多对多,@ManyToMany
已TUser和TRole为例子

@Entity
@Data
@Table(name = "user",schema = "hr")
@JsonIgnoreProperties(value = {"handler","hibernateLazyInitializer","fieldHandler"})
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class TUser {
    @Id
    @GeneratedValue(generator="jpa-uuid")
    @Column(name = "user_id")
    private String id;

    @Column(name = "user_name")
    private String name;

    @Column(name = "user_describe")
    private String desc;

   //使用JoinTable属性把中间表关联起来,中间表主键为自增,joinColumns指的当前表user,name 属性指向中间表的user_id,referencedColumnName 指向user中的id,inverseJoinColumns是role表,name 属性指向中间表的role_id,referencedColumnName 指向role中的id
    @ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST,CascadeType.REMOVE})
    @JoinTable(name = "user_role", schema = "hr",
            joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "user_id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
    private List<TRole> roleSet = new ArrayList<>();
}

TRole为:

@Entity
@Data
@Table(name = "role",schema = "hr")
@GenericGenerator(name = "jpa-uuid", strategy = "uuid")
public class TRole {

    @Id
    @GeneratedValue(generator="jpa-uuid")
    private String id;

    private String name;

    @Column(name = "role_desc")
    private String desc;
    
    //mappedBy就是被维护端,被TUser类中的属性“roleSet”维护
    @ManyToMany(mappedBy = "roleSet",cascade = {CascadeType.PERSIST,CascadeType.REMOVE})
    private List<TUser> userSet = new ArrayList<>();
}

依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <dependencies>
    <!-- Compile -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- config -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.9.2</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.2</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.47</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- Test -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
  </dependencies>
相关标签: 后端开发 jpa