【问题标题】:Updating and deleting from DB using JpaRepository使用 JpaRepository 从数据库中更新和删除
【发布时间】:2019-12-07 14:41:18
【问题描述】:

我有两个与多对多关系相关的实体:

User.java

@Id
@Column(name = "user_id", updatable = false, nullable = false, unique = true)
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;

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

@Column(name = "product")
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
@JoinTable(name = "products_users",
        joinColumns = {@JoinColumn(name = "user_id")},
        inverseJoinColumns = {@JoinColumn(name = "product_id")})
private Set<Product> products;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "created_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date createdOn;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "modified_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date modifiedOn;

// construuctors, getter, setter
}

产品.java

@Id
@Column(name = "product_id", updatable = false, nullable = false, unique = true)
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;

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

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "created_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date createdOn;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="yyyy-MM-dd@HH:mm:ss")
@Column(name = "modified_on")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private Date modifiedOn;

//getters, setters, contructors
}

我想按用户 UUID 从数据库中删除数据。我如何编写这样的查询

@Transactional
    @Modifying
    @Query(value = "delete from users where user_id = ?1", nativeQuery = true)
    void deleteByUUID(UUID uuid);

但它会删除用户表中的唯一行。我希望删除有关此用户及其产品的所有数据。

而且我也不知道如何正确更新用户及其产品。

【问题讨论】:

标签: java spring postgresql jpa spring-data-jpa


【解决方案1】:

您已经对级联进行了所有正确的设置:

问题是您正在触发本机查询。这完全绕过了所有 JPA 配置和级联。即使您使用没有原生的 JPQL 删除,这仍然会省略所有级联和 JPA 配置。

在您的代码中的某处,您需要获取用户,例如使用findOne。之后使用delete 方法。只有这样级联才会起作用。

【讨论】:

  • 它有效,谢谢。你不知道如何更新这个实体吗?
  • 同样的事情......你使用 findXXX 然后简单地更新 java 对象
【解决方案2】:

基于 Maciej Kowalski 的回答

通过 UUID 在存储库中查找:

  Optional<User> findByUUID(UUID uuid);

关于要删除的服务:

Optional<User> optionalUser = repository.findByUUID(uuid);
        if (optionalUser.isPresent()) {
            User user = optionalUser.get();
            repository.delete(user);
        }
        // here on else you can throw an Exception

关于服务更新:

Optional<User> optionalUser = repository.findByUUID(uuid);
        if (optionalUser.isPresent()) {
            User user = optionalUser.get();
            // Make changes here for example user.setName(otherName)
            repository.save(user);
        }
        // here on else you can throw an Exception

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多