休眠将2个Java文件链接到同一数据库表

时间:2018-11-05 13:06:39

标签: java hibernate

作为对这个问题的继续

fetch oneToMany and manyToOne using hibernate and @JsonIgnore

我的JsonIgnore有问题 我正在考虑构建一个具有JsonIgnore批注的对象 还有一个没有它的物体 当我尝试使用新对象时,它仍在使用旧对象-我缺少什么?

这是新代码(CategoryIgnoreJson是我创建的新类)

    @GET
@Produces(MediaType.APPLICATION_JSON)
@Path("getCategoriesQandA")
public List<CategoryIgnoreJson> getCategoriesQandA() {
    ediUtils = new EDIUtils(SYSTEM_NAME, USER_NAME);
    Init(ediUtils);
    ediUtils.writeToLog("get Categories , questions and answers ");
    List<CategoryIgnoreJson> categoriesArray;

    categoriesArray = categoryIgnoreJsonRepository.getEffective();

    return categoriesArray;

}

我还创建了新的存储库

public interface CategoryIgnoreJsonRepository extends JpaRepository<CategoryIgnoreJson, Long>{
@Transactional 
@Modifying
@Query("update  Category set expiration_date = current_date() where category_id = ?1 ")
void expireCategory(Long id );  


@Query("from Category where function ('coalesce' ,effectiveDate ,current_date() ) <= current_date() "
        + "and function('coalesce' ,expirationDate , to_date('50001231','yyyymmdd')) > current_date() ")
List<CategoryIgnoreJson> getEffective( );

}

我在日志文件中看到旧类别仍然被称为 我还将旧类别中的表名从Categories更改为Categories1(只是为了验证该代码是否被调用),并得到了预期的错误

edi_ms.categories1" does not exist

我怎么叫新班?我想念什么?

1 个答案:

答案 0 :(得分:0)

发现了问题 我还需要将选择更改为

    @Query("from CategoryIgnoreJson ..."

还更改映射的值,以引用父对象中映射到的对象的名称(在CatagoryIgnoreJson.java中)

@OneToMany(fetch = FetchType.EAGER, mappedBy = "categoryIgnoreJson")
@Fetch(FetchMode.SUBSELECT)
@NotFound(action = NotFoundAction.IGNORE)
public List<QuestionIgnoreJson> getQuestions() {
    return this.questions;enter code here