簡體   English   中英

JpaRepository 不保存實體

[英]JpaRepository not saving entity

我有一台使用 spring 引導和 spring 數據 jpa 的服務器。

我的服務器中有兩個用@RestController注釋的類。 其中一個可能會更改實體,而另一個不會。

@RestController
@Slf4j
public class ControllerA {
    private EntityRepository entityRepository;

    public ControllerA(EntityRepository entityRepository) {
        this.entityRepository = entityRepository;
    }

    @PostMapping("/pathStr")
    public void changeEntity(@RequestParam("entityId") Long entityId) {
        // init
        Entity entity = entityRepository.findById(entityId);
        // make changes to entity
        entity.getOneToOneLinkedEntity().setIntProperty(0);
        // save entity
        entityRepository.save(entity);
    }
}

@RestController
@Slf4j
public class ControllerB {

    private Entity cachedEntity;
    private EntityRepository entityRepository;

    public ControllerB(EntityRepository entityRepository) {
        this.entityRepository = entityRepository;
    }

    @MessageMapping("/otherPath")
    public void getEntity(ArgumentType argument) {
        if (cachedEntity == null) {
            cachedEntity = entityRepository.save(new Entity());
        }
        Entity entity = entityRepository.findById(cachedEntity.getId()).orElse(null);
        int properyValue = entity.getOneToOneLinkedEntity().getIntProperty(); // this is not zero
    }
}

這是兩個實體和存儲庫:

@Entity
public class Entity implements Serializable {

    @Id
    @GeneratedValue private Long id;

    @NotNull
    @OneToOne(cascade=CascadeType.ALL)
    private OneToOneLinkedEntity linkedEntity;
}

@Entity
public class OneToOneLinkedEntity implements Serializable {

    @Id
    @GeneratedValue private Long id;

    @NotNull
    private int intProperty = 0;
}

public interface EntityRepository extends JpaRepository<Entity, Long> {
}

我從客戶端調用 ControllerA.changeEntity,一旦返回,我再調用 ControllerB.getEntity。 我在第一次調用中所做的更改未顯示在第二次調用中,(如果我直接使用 sql 查詢,它們也不在數據庫中) int 屬性具有舊值。 即使我只在實體上而不是在linkedEntity上進行保存, CascadeType.ALL也應該使鏈接實體更新,對吧?

我嘗試在 ControllerA 中保存后添加entityRepository.flush() ,但問題仍然存在。 我能做些什么? 如何讓 ControllerB 獲得正確的 intProperty 值?

這就是我在 application.properties 中的內容:

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_name
spring.datasource.username=user
spring.datasource.password=pass
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultComponentSafeNamingStrategy

您應該在您的方法中添加@Transactional注釋,因此 spring 將處理事務並提交您的更改。
這通常出現在@Service class 上,但我在你的例子中看到你沒有,所以把它放在 controller 上(或添加服務層,我認為它更好)

您忽略了依賴注入,這是 spring 框架的美妙之處。

  1. 創建一個實現 JPARepository 的存儲庫 class 並使用 @Repository 對其進行注釋。
  2. 創建一個服務 class 並使用 @Service 和 @Transactional 對其進行注釋。
  3. 在服務 class 中自動裝配存儲庫並進行相應的方法調用 like.save().find() 等。
  4. 在 Controller Class 中自動裝配服務 class 並調用將調用存儲庫方法的服務方法。

這就是你所要做的。 For making your application flow fast, better you create model for the entity class and pass the model between classes instead of entity as it contains a lot more information and thus much more heavy than normal model object.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM