有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

来自其他表JPA的java映射字段(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”中,包含来自其他实体(favorite)的负载。我该怎么做

我不想在公司内部合并收藏夹,因为我只需要一个字段

非常感谢


共 (0) 个答案