如何解决使用H2的DataJpaTest无法访问子选择实体中的数据
我正在运行一个设置为带有H2数据库的DataJpaTest的单元测试。效果很好,直到我使用Subselect引入了新的不可变实体集。它不会中断,但不会找到应有的数据。我在sql中看到它正确地加入了Subselect查询。
在像应用程序一样正常运行时,@ Subselect实体可以正常工作,因此以某种方式存在单元测试设置问题。
这是我的主要实体:
@Entity
@DynamicUpdate
@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = true,callSuper = false)
@Table(name = "apps",schema = "public")
public class App extends BaseEntity
{
@Id
@Column(name = "app_id",nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "ent_generator")
@SequenceGenerator(name = "ent_generator",sequenceName = "entity_seq",allocationSize = 1)
private long appId;
@EqualsAndHashCode.Include
@Basic
@Column(name = "app_name",length = 200)
private String appCode;
@EqualsAndHashCode.Include
@Basic
@Column(name = "app_ver_name",length = 200)
private String appVersion;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parent_app_id")
private App parent;
// ...
@OnetoMany(
mappedBy = "app",cascade = CascadeType.ALL,orphanRemoval = true
)
private Set<TrainingRequirement> trainingRequirements = new HashSet<>();
@OnetoMany(mappedBy = "app")
private Set<EffectiveTrainingRequirement> effectiveTrainingRequirements = new HashSet<>();
// ...
}
这是我的@Subselect实体:
@Entity
@Immutable
@Subselect(
"SELECT\n" +
" tr.trn_req_id AS trn_req_id\n" +
",tr.app_id AS app_id\n" +
" FROM apps a\n" +
" JOIN training_reqs tr\n" +
"-- ...")
@Synchronize({"apps","training_reqs"})
public class EffectiveTrainingRequirement
{
@Id
@Column(name = "trn_req_id",nullable = false)
private long trainingRequirementId;
@EqualsAndHashCode.Include
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "app_id")
private App app;
}
@RunWith(springrunner.class)
@DataJpaTest
public class TrainingRequirementRepositoryTest
{
@Autowired
private TestEntityManager entityManager;
@Autowired
private AppRepository appRepository;
@Before
public void setup()
{
// ...
app4 = new App(organization,"a4");
app4.addTrainingRequirement(new TrainingRequirement("treq6",c6));
app4.addTrainingRequirement(new TrainingRequirement("treq7",c7,tr1));
app4.addTrainingRequirement(new TrainingRequirement("treq8",c8,tr2));
entityManager.persist(app4);
app4v2 = new App(organization,"a4",app4,"v2");
app4v2.setParent(app4);
app4v2.addTrainingRequirement(treq7chg = new TrainingRequirement("treq7",tr2));
treq7chg.setChangeType(InheritOverride.CHANGE);
app4v2.addTrainingRequirement(treq6rmv = new TrainingRequirement("treq6"));
treq6rmv.setChangeType(InheritOverride.REMOVE);
app4v2.addTrainingRequirement(treq9add = new TrainingRequirement("treq9",c9,tr4));
treq9add.setChangeType(InheritOverride.ADDITION);
entityManager.persist(app4v2);
}
@Test
public void test_AppWithEffectiveTR()
{
App app = appRepository.getAppWithParent(organization,"v2").get();
logger.debug("# tr: "+app.getTrainingRequirements().size());
logger.debug("# etr: "+app.getEffectiveTrainingRequirements().size());
for (EffectiveTrainingRequirement tr : app.getEffectiveTrainingRequirements())
logger.debug("tr: "+tr.toString());
}
}
存储库:
@Repository
public interface AppRepository extends CrudRepository<App,Long>
{
String APP_FETCH_QUERY = "SELECT a FROM App a " +
"LEFT JOIN FETCH a.parent p " +
"LEFT JOIN FETCH a.trainingRequirements atr " +
"LEFT JOIN FETCH a.effectiveTrainingRequirements ";
@Query(value = APP_FETCH_QUERY +
"WHERE a.organization = :org " +
" AND a.appCode = :appCode " +
" AND a.appVersion = :appVersion" )
Optional<App> getAppWithParent(@Param("org") Organization org,@Param("appCode") String appCode,@Param("appVersion") String appVersion);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。