有一個用于測驗的類來保存一些約束,有一個setup帶有@Before注解的方法,其中測驗資料保存在資料庫中:
..并且在這種方法中運行測驗時,保存Entity到存盤庫時會出現EntityNotFoundException錯誤:
. . .
@Autowired
EntityFeatureConfigRepository entityFeatureConfigRepository;
@Autowired
FeatureRepository featureRepository;
@Autowired
EntityRepository entityRepository;
@Before
public void setup() {
entityRepository.deleteAll();
featureRepository.deleteAll();
entityFeatureConfigRepository.deleteAll();
featureRepository.save(new Feature(1); <==== 1L - id
entityRepository.save(new Entity(1));
entityFeatureConfigRepository.save(new EntityFeatureConfig( <=EXCEPTION
new Feature(1),
new Entity(1),
true));
}
. . .
Unable to find Entity with id 1; nested exception is javax.persistence.EntityNotFoundException: Unable to find Entity with id 1
錯誤是Entity不存在,雖然保存在setup方法中
如果類中有這樣的欄位,EntityFeatureConfig則問題可能出在它們身上。
@Id
@ManyToOne
@ToString.Exclude
@JsonIgnore
@JoinColumn(name = "feature_id", referencedColumnName = "id")
private Feature feature;
@Id
@ManyToOne
@ToString.Exclude
@JoinColumn(name = "entity_id", referencedColumnName = "id")
private Entity entity;
和Entity
@OneToMany(fetch = FetchType.EAGER, mappedBy = "entity")
@EqualsAndHashCode.Exclude
@JsonIgnore
private Set<UserManagingEntity> user;
這個錯誤與什么有關,資料不在存盤庫中?
uj5u.com熱心網友回復:
嘗試使用從保存方法回傳的物件而不是創建新物件
Feature f = featureRepository.save(new Feature(1);
Entity e = entityRepository.save(new Entity(1));
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/461383.html
