java - 来自其他表 JPA 的映射字段(Spring boot)
问题描述
实体 1
@Data
@Entity
public class Company implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int companyId;
private String name;
@Transient
private int haveFavoriteUser;
}
实体 2
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
private String userId;
}
实体 3
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Favorite implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int favoriteId;
@ManyToOne
@JoinColumn(name = "userId")
private User user;
@ManyToOne
@JoinColumn(name = "companyId")
private Company company;
}
存储库:
@Repository
public interface CompanyRepository extends JpaRepository<Company, Long> {
@Query(value = "SELECT CE.*, IF(F.companyId = CE.companyId, 1, 0) as haveFavoriteUser FROM Company CE " + //
"LEFT JOIN Favorite F ON CE.companyId = F.companyId " + //
"WHERE F.userId = :user ORDER BY CE.name", //
nativeQuery = true)
List<Company> findAllFavorite(@Param("user") String user);
}
我希望在我的 jpa 存储库映射值“haveFavoriteUser”中,加载来自其他实体(最喜欢的)。我怎样才能做到这一点?
我不想合并公司内部的收藏夹,因为我只需要一个字段。
太感谢了。
解决方案
推荐阅读
- javascript - 如何在按钮单击/悬停时显示/隐藏组件?
- javascript - Dialogflow agent.add 无法正常工作
- java - 比较java中时间字符串的格式
- python - Python Plotly:向具有多个子图的散点图添加水平线
- r - 如何在 ggplot2 中制作一个函数来制作 geom_bar() 制作一个条形图,让我检查向量中的颜色?
- kubernetes - curl 无法访问 kubernetes 服务 - 错误:没有到主机的路由
- r - 有没有一种简单的方法来分析数据列表中的数据集?
- html - CSS网格:每两行不同的对齐方式
- python - 更新 Pandas 列,其中新值是该列中的值和数据框中的另一列的组合
- php - 如何解决 laravel 中删除时找不到 409 页面错误?