onetoone在spring数据jpa中的级联类型

ui7jx7zq  于 2021-07-13  发布在  Java
关注(0)|答案(3)|浏览(438)

我用 OneToOnespring data JPA 我想从数据库中删除一条记录 Address 不接触用户的table。但我不能。
如果我移除 User ,在这种情况下 Address 被移除了,很好。
但是你怎么能删除一个 Address 不碰地板 User ?
https://github.com/mytestpercon/testcascade
用户.java

@Entity
    @Table(name = "user", schema = "testCascade")
    public class User implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;

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

        @OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
        private Address address;

        // Getter and Setter ...

    }

地址.java

@Entity
    @Table(name = "address", schema = "testCascade")
    public class Address implements Serializable {

        @Id
        private Long id;

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

        @OneToOne
        @MapsId
        @JoinColumn(name = "id")
        private User user;

        // Getter and Setter ...

    }

删除控制器.java

@Controller
    public class DeleteController {

        @Autowired
        ServiceJpa serviceJpa;

        @GetMapping(value = "/deleteAddressById")
        public String deleteAddressById () {

            serviceJpa.deleteAddressById(4L);
            return "redirect:/home";

        }

    }
ha5z0ras

ha5z0ras1#

public class Address {

   @Id
    private Long id;      

    @MapsId
    @JoinColumn(name = "id")
    private User user;
  }

重命名 @JoinColumn(name = "id")@JoinColumn(name = "user_id") 不能说指向user的列就是地址的id

bogh5gae

bogh5gae2#

你把Map弄错了这就是问题所在。
试试下面的,看看
用户.java

@Entity
@Table(name = "user", schema = "testCascade")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

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

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="foriegn key column in user table for address example.. address_id")
    private Address address;

    // Getter and Setter ...

}

地址.java

@Entity
@Table(name = "address", schema = "testCascade")
public class Address implements Serializable {

    @Id
    private Long id;

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

    //name of the address variable in your user class
    @OneToOne(mappedBy="address", 
        cascade={CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
                    CascadeType.REFRESH})
    private User user;

    // Getter and Setter ...

}
aurhwmvo

aurhwmvo3#

为了解决这个问题,您需要阅读hibernate文档hibernate Example 162 , Example 163 , Example 164 .
另外,我建议您在springdatajpa中使用@primarykeyjoincolumn注解
这有助于我解决这个问题。
而且还需要指定参数 orphanRemoval = true 用户.java

@Entity(name = "User")
    @Table(name = "user", schema = "testother")
    public class User implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private Long id;

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

        @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
        private Address address;

        public void addAddress(Address address) {
            address.setUser( this );
            this.address = address;
        }

        public void removeAddress() {
            if ( address != null ) {
                address.setUser( null );
                this.address = null;
            }
        }

        // Getter and Setter
    }

地址.java

@Entity(name = "Address")
    @Table(name = "address", schema = "testother")
    public class Address implements Serializable {

        @Id
        private Long id;

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

        @OneToOne
        @MapsId
        @JoinColumn(name = "id")
        private User user;

        // Getter and Setter
    }

deletecontroller.java文件

@Controller
    public class DeleteController {

        @Autowired
        ServiceJpa serviceJpa;

        @GetMapping(value = "/deleteUser")
        public String deleteUser () {

            User user = serviceJpa.findUserById(2L).get();

            user.removeAddress();
            serviceJpa.saveUser(user);

            return "/deleteUser";
        }

    }

或进行自定义sql查询。

@Repository
    public interface DeleteAddress extends JpaRepository<Address, Long> {

        @Modifying
        @Query("delete from Address b where b.id=:id")
        void deleteBooks(@Param("id") Long id);

    }

相关问题