栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Spring Data JPA中联接两个表实体

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Spring Data JPA中联接两个表实体

有关员工拥有一个或多个电话的典型示例,请参阅此Wikibook部分。

对于你的特定示例,如果你想建立

one-to-one
关系,则应更改ReleaseDateType模型中的下一个代码:

@Column(nullable = true) private Integer media_Id;

对于:

@oneToOne(fetch = FetchType.LAZY)@JoinColumn(name="CACHE_MEDIA_ID", nullable=true)private CacheMedia cacheMedia ;

在CacheMedia模型中,你需要添加:

@oneToOne(cascade=ALL, mappedBy="ReleaseDateType")private ReleaseDateType releaseDateType;

然后应在你的存储库中替换:

@Query("Select * from A a  left join B b on a.id=b.id")public List<ReleaseDateType> FindAllWithDescriptionQuery();

通过:

//In this case a query annotation is not need since spring constructs the query from the method namepublic List<ReleaseDateType> findByCacheMedia_Id(Integer id); 

或通过:

@Query("FROM ReleaseDateType AS rdt WHERe cm.rdt.cacheMedia.id = ?1")    //This is using a named query methodpublic List<ReleaseDateType> FindAllWithDescriptionQuery(Integer id);

或者,如果你喜欢使用

@oneToManyand @ManyToOne
关系,则应在
ReleaseDateType
模型中更改下一个代码:

@Column(nullable = true) private Integer media_Id;

对于:

@oneToMany(cascade=ALL, mappedBy="ReleaseDateType")private List<CacheMedia> cacheMedias ;

在CacheMedia模型中,你需要添加:

@ManyToOne(fetch = FetchType.LAZY)@JoinColumn(name="RELEASE_DATE_TYPE_ID", nullable=true)private ReleaseDateType releaseDateType;

然后应在你的存储库中替换:

@Query("Select * from A a  left join B b on a.id=b.id")public List<ReleaseDateType> FindAllWithDescriptionQuery();

通过:

//In this case a query annotation is not need since spring constructs the query from the method namepublic List<ReleaseDateType> findByCacheMedias_Id(Integer id); 

或通过:

@Query("FROM ReleaseDateType AS rdt LEFT JOIN rdt.cacheMedias AS cm WHERe cm.id = ?1")    //This is using a named query methodpublic List<ReleaseDateType> FindAllWithDescriptionQuery(Integer id);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392725.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号