Java 设置方法出现 SpringBootTest EntityNotFoundException
Java SpringBootTest EntityNotFoundException on setup method
有一个class用于测试保存一些约束,有方法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
方法
如果classEntityFeatureConfig
里面有这样的字段,可能问题出在
@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;
这个错误可能与什么有关,
数据不在存储库中?
尝试使用从保存方法返回的对象而不是创建新对象
Feature f = featureRepository.save(new Feature(1);
Entity e = entityRepository.save(new Entity(1));
有一个class用于测试保存一些约束,有方法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
方法
如果classEntityFeatureConfig
里面有这样的字段,可能问题出在
@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;
这个错误可能与什么有关, 数据不在存储库中?
尝试使用从保存方法返回的对象而不是创建新对象
Feature f = featureRepository.save(new Feature(1);
Entity e = entityRepository.save(new Entity(1));