简体   繁体   English

保存后刷新并获取实体(JPA/Spring Data/Hibernate)

[英]Refresh and fetch an entity after save (JPA/Spring Data/Hibernate)

I've these two simple entities Something and Property .我有这两个简单的实体SomethingProperty The Something entity has a many-to-one relationship to Property , so when I create a new Something row, I assign an existing Property . Something实体与Property具有多对一的关系,因此当我创建新的Something行时,我分配了一个现有的Property

Something:某物:

@Entity
@Table(name = "something")
public class Something implements Serializable {

    private static final long serialVersionUID = 1L;

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

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

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

    @ManyToOne
    private Property property;

    // getters and setters

    @Override
    public String toString() {
        return "Something{" +
            "id=" + getId() +
            ", name='" + getName() + "'" +
            ", owner='" + getOwner() + "'" +
            ", property=" + getProperty() +
            "}";
    }

Property:财产:

@Entity
@Table(name = "property")
public class Property implements Serializable {

    private static final long serialVersionUID = 1L;

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

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

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

    @Column(name = "dimension")
    private Integer dimension;

    // getters and setters

    @Override
    public String toString() {
        return "Property{" +
            "id=" + getId() +
            ", shape='" + getShape() + "'" +
            ", color='" + getColor() + "'" +
            ", dimension='" + getDimension() + "'" +
            "}";
    }
}

This is the SomethingRepository (Spring):这是SomethingRepository (Spring):

@SuppressWarnings("unused")
@Repository
public interface SomethingRepository extends JpaRepository<Something,Long> {
    
}

Through a REST controller and a JSON, I want to create a new Something :通过 REST controller 和 JSON,我想创建一个新的Something

@RestController
@RequestMapping("/api")
public class SomethingResource {

    private final SomethingRepository somethingRepository;

    public SomethingResource(SomethingRepository somethingRepository) {
        this.somethingRepository = somethingRepository;
    }

    @PostMapping("/somethings")
    public Something createSomething(@RequestBody Something something) throws URISyntaxException {
        Something result = somethingRepository.save(something);
        return result;
    }
}

This is the JSON in input (the property id 1 is an existing row in the database):这是输入中的 JSON( property id 1 是数据库中的现有行):

{
  "name": "MyName",
  "owner": "MySelf",
  "property": {
    "id": 1
  }

} }

The problem is: after the method .save(something) , the variable result contains the persisted entity, but without the fields of field property , validated (they are null ):问题是:在方法.save(something)之后,变量result包含持久实体,但没有 field property的字段,经过验证(它们是null ):

Output JSON: Output JSON:

{
  "id": 1,
  "name": "MyName",
  "owner": "MySelf",
  "property": {
    "id": 1,
    "shape": null,
    "color": null,
    "dimension": null
  }
}

I expect that they are validated/returned after the save operation.我希望它们在保存操作后得到验证/返回。

To workaround this, I have to inject/declare the EntityManager in the REST controller, and call the method EntityManager.refresh(something) (or I have to call a .findOne(something.getId()) method to have the complete persisted entity):为了解决这个问题,我必须在 REST controller 中注入/声明EntityManager ,并调用EntityManager.refresh(something)方法(或者我必须调用.findOne(something.getId())方法来获得完整的持久化实体):

@RestController
@RequestMapping("/api")
@Transactional
public class SomethingResource {

    private final SomethingRepository somethingRepository;
    
    private final EntityManager em;

    public SomethingResource(SomethingRepository somethingRepository, EntityManager em) {
        this.somethingRepository = somethingRepository;
        this.em = em;
    }

    @PostMapping("/somethings")
    public Something createSomething(@RequestBody Something something) throws URISyntaxException {
        Something result = somethingRepository.save(something);
        em.refresh(result);
        return result;
    }
}

With this workaround, I've the expected saved entith (with a correct JSON):使用此解决方法,我已经保存了预期的 entith(使用正确的 JSON):

{
  "id": 4,
  "name": "MyName",
  "owner": "MySelf",
  "property": {
    "id": 1,
    "shape": "Rectangle",
    "color": "Red",
    "dimension": 50
  }
}

Is there an automatic method/annotation, with JPA or Spring or Hibernate, in order to have the "complete" persisted entity?是否有使用 JPA 或 Spring 或 Hibernate 的自动方法/注释,以便拥有“完整”的持久实体?

I would like to avoid to declare the EntityManager in every REST or Service class, or I want avoid to call the .findOne(Long) method everytime I want the new refreshed persisted entity.我想避免在每个 REST 或服务 class 中声明EntityManager ,或者我想避免每次我想要新的刷新的持久实体时调用.findOne(Long)方法。

Instead of defining EntityManager in each of your resource , you can define it once by creating a Custom JpaRepository.您可以通过创建自定义JpaRepository 来定义一次,而不是在每个资源中定义EntityManager Reference 参考

Then use the refresh of your EntityManager in each of your repository directly.然后直接在每个存储库中使用EntityManagerrefresh

Refer the below example:请参考以下示例:

CustomRepository Interface自定义存储库接口

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;

import java.io.Serializable;

@NoRepositoryBean
public interface CustomRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
  void refresh(T t);
}

CustomRepository Implementation自定义存储库实现

import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.io.Serializable;

public class CustomRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
    implements CustomRepository<T, ID> {

  private final EntityManager entityManager;

  public CustomRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) {
    super(entityInformation, entityManager);
    this.entityManager = entityManager;
  }

  @Override
  @Transactional
  public void refresh(T t) {
    entityManager.refresh(t);
  }
}

Enable Custom JPARepository in Spring Boot Application Class在 Spring Boot 应用程序类中启用自定义 JPARepository

@SpringBootApplication
@EnableJpaRepositories (repositoryBaseClass = CustomRepositoryImpl.class)
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

Your Something Repository你的东西存储库

public interface SomethingRepository extends CustomRepository<Something, Long> {

}

Use Refresh directly in SomethingResource (Assuming Something is an Entity)在SomethingResource中直接使用Refresh (假设某物是一个实体)

@RestController
@RequestMapping("/api")
@Transactional
public class SomethingResource {

    private final SomethingRepository somethingRepository;

    public SomethingResource(SomethingRepository somethingRepository) {
        this.somethingRepository = somethingRepository;
    }

    @PostMapping("/somethings")
    public Something createSomething(@RequestBody Something something) throws URISyntaxException {
        Something result = somethingRepository.save(something);
        somethingRepository.refresh(result);
        return result;
    }
}

That's not enough:这还不够:

Something result = somethingRepository.save(something);

You need to manually merge the incoming entity:您需要手动合并传入的实体:

Something dbSomething = somethingRepository.findOne(
    Something.class, something.getId()
);
dbSomething.setName(something.getName());
dbSomething.setOwner(something.getOwner());

somethingRepository.save(dbSomething);

Since the property attribute is using the default FetchType.EAGER , the entity should have the property attribute initialized.由于property属性使用默认的FetchType.EAGER ,因此实体应该初始化property属性。

But, that's strange to call the Repository twice from the REST controller.但是,从 REST 控制器调用存储库两次很奇怪。 You should have a Service layer that does all that in a @Transactional service method.你应该有一个服务层,在@Transactional服务方法中完成所有这些。 That way, you don't need to resave the entity since it's already managed.这样,您不需要重新保存实体,因为它已经被管理。

@Transactional
public Something mergeSomething(Something something) {
    Something dbSomething = somethingRepository.findOne(
        Something.class, something.getId()
    );
    dbSomething.setName(something.getName());
    dbSomething.setOwner(something.getOwner());

    return dbSomething;
}

Now, you need to carefully merge every property you sent.现在,您需要仔细合并您发送的每个属性。 In your case, if you send null for property you should decide whether you should nullify the @ManyToOne reference or not.在您的情况下,如果您为property发送null ,您应该决定是否应取消@ManyToOne引用。 So, it depends on your current application business logic requirements.因此,这取决于您当前的应用程序业务逻辑需求。

Update更新

If you make sure you always send back the same entity you previously fetched, you could just use merge .如果您确保始终发回之前获取的同一实体,则可以使用merge

em.merge(result);

But your property attribute is just an id, and not an actual child entity, so you have to resolve that yourself in the Service layer.但是您的property属性只是一个 id,而不是实际的子实体,因此您必须自己在 Service 层解决这个问题。

In Spring Boot JpaRepository:在 Spring Boot JpaRepository 中:

If our modifying query changes entities contained in the persistence context, then this context becomes outdated.如果我们的修改查询更改了持久化上下文中包含的实体,那么这个上下文就会过时。

In order to fetch the entities from the database with latest record.为了从具有最新记录的数据库中获取实体。

Use @Modifying(clearAutomatically = true)使用@Modifying(clearAutomatically = true)

@Modifying annotation has clearAutomatically attribute which defines whether it should clear the underlying persistence context after executing the modifying query. @Modifying 注释具有 clearAutomatically 属性,该属性定义在执行修改查询后是否应清除底层持久性上下文。

Example:示例:

@Modifying(clearAutomatically = true)
@Query("UPDATE NetworkEntity n SET n.network_status = :network_status WHERE n.network_id = :network_id")
        int expireNetwork(@Param("network_id") Integer network_id,  @Param("network_status") String network_status);

I would replace the property with a proxy generated using session.load我会用使用session.load生成的代理替换该属性

        something.setProperty(session.load(Property.class, something.getProperty().getId()))
        Something result = somethingRepository.save(something);
        return result;

Now the result will have the entire property object loaded from the db现在结果将从数据库加载整个属性 object

By the time you persist the entity it will be in managed state so if you just call something.getProperty();当您持久化实体时,它将处于托管状态,因此如果您只调用something.getProperty(); it loads from the database and fills the property value of the something entity它从数据库加载并填充something实体的property

public Something save(Something something) {
    em.persist(something);
    something.getProperty();
    return something;
}

so normally when you have many-to-one relationship that should be fetched automatically.所以通常当你有应该自动获取的多对一关系时。 If not calling the getters of the objects in entity will fill them too by firing a new DB Find request.如果不调用实体中对象的 getter 也会通过触发新的 DB Find 请求来填充它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM