JPA查询一个实体中有多个实体的许多人?

发布于 2025-02-12 15:00:16 字数 2731 浏览 1 评论 0原文

我有某种情况。假设我正在制作有关游戏的数据库。因此,首先,我有三个实体:CompanyPlatformgame

Company是这样的:

@Data
@Entity
@Table(name = "company")
public class Company implements Serializable {

    @Id
    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "MA_COMPANY_ID_S")
    @SequenceGenerator(name = "MA_COMPANY_ID_S", allocationSize = 1, sequenceName = "MA_COMPANY_ID_S")
    @Column(name = "ID")
    private Long id;

    @Column(name = "COMPANY_NAME")
    private String companyName;
}

平台是这样的

@Data
@Entity
@Table(name = "platform")
public class Platform {
    @Id
    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "MA_PLATFORM_ID_S")
    @SequenceGenerator(name = "MA_PLATFORM_ID_S", allocationSize = 1, sequenceName = "MA_PLATFORM_ID_S")
    @Column(name = "ID")
    private Long id;

    @Column(name = "PLATFORM_NAME")
    private String platformName;
}

,最后,game

@Data
@Entity
@Table(name = "game")
public class Game {
    @Id
    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "MA_GAME_ID_S")
    @SequenceGenerator(name = "MA_GAME_ID_S", allocationSize = 1, sequenceName = "MA_GAME_ID_S")
    @Column(name = "ID")
    private Long id;

    @Column(name = "GAME_NAME")
    private String gameName;

    @Column(name="TEST")
    private String testName;

    @ManyToOne 
    @JoinColumn(name="COMPANY_ID",
            foreignKey=@ForeignKey(name="FK_COMPANY_ID"))
    private Company company;

    @ManyToOne
    @JoinColumn(name="PLATFORM_ID",
            foreignKey=@ForeignKey(name="FK_PLATFORM_ID"))
    private Platform platform;
}

如您所见,游戏必须具有两个外键链接到公司和平台(如一个公司)由一家公司生产,然后放在平台上(稍后放置多平台的概念),

我使用存储库文件将其与数据库联系起来。我将只列出gamerepository示例,因为这是问题出现的地方

@Repository
public interface GameRepository extends JpaRepository<Game, Long> {
    @Query("SELECT id, gameName, company, platform FROM Game g WHERE g.id = :id")
    public Game findGameById(@Param("id") String id);
}

,我会得到此错误:

org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode 
 \-[IDENT] IdentNode: 'platform' {originalText=platform}

但是,如果我只坚持一个@manytoone < /code>查询中的变量,例如我删除companyplatform>(例如:)从g g g g w with g g gamename,gamename,公司.id = :id“,查询有效。

我在哪里做错了,可以拥有多个@manytoone关系,该关系指的是一个实体内的多个实体并运行它?或者我应该我是否坚持@manytomany

I have some sort of scenario. Let's say that I'm making a database about games. So first, I have Three entities: Company, Platform and Game.

Company goes like this:

@Data
@Entity
@Table(name = "company")
public class Company implements Serializable {

    @Id
    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "MA_COMPANY_ID_S")
    @SequenceGenerator(name = "MA_COMPANY_ID_S", allocationSize = 1, sequenceName = "MA_COMPANY_ID_S")
    @Column(name = "ID")
    private Long id;

    @Column(name = "COMPANY_NAME")
    private String companyName;
}

Platform goes like this

@Data
@Entity
@Table(name = "platform")
public class Platform {
    @Id
    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "MA_PLATFORM_ID_S")
    @SequenceGenerator(name = "MA_PLATFORM_ID_S", allocationSize = 1, sequenceName = "MA_PLATFORM_ID_S")
    @Column(name = "ID")
    private Long id;

    @Column(name = "PLATFORM_NAME")
    private String platformName;
}

And finally, Game goes like this

@Data
@Entity
@Table(name = "game")
public class Game {
    @Id
    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "MA_GAME_ID_S")
    @SequenceGenerator(name = "MA_GAME_ID_S", allocationSize = 1, sequenceName = "MA_GAME_ID_S")
    @Column(name = "ID")
    private Long id;

    @Column(name = "GAME_NAME")
    private String gameName;

    @Column(name="TEST")
    private String testName;

    @ManyToOne 
    @JoinColumn(name="COMPANY_ID",
            foreignKey=@ForeignKey(name="FK_COMPANY_ID"))
    private Company company;

    @ManyToOne
    @JoinColumn(name="PLATFORM_ID",
            foreignKey=@ForeignKey(name="FK_PLATFORM_ID"))
    private Platform platform;
}

As you can see, a game has to have two foreign keys linking to a company and a platform, as in, a game is produced by one company and then put on a platform (put aside the notions of multiplatform later)

I use a repository file to connect this with my database. I'll just list the GameRepository example because this is where the problem comes in

@Repository
public interface GameRepository extends JpaRepository<Game, Long> {
    @Query("SELECT id, gameName, company, platform FROM Game g WHERE g.id = :id")
    public Game findGameById(@Param("id") String id);
}

If I run my program, I get this error instead:

org.hibernate.QueryException: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode 
 \-[IDENT] IdentNode: 'platform' {originalText=platform}

However, if I only stick with only ONE @ManyToOne variable in the query, like if I remove either the company or platform (for example: "SELECT id, gameName, company FROM Game g WHERE g.id = :id", the query WORKS.

Where did I go wrong, is it possible to have multiple @ManyToOne relationship that refer to multiple entities within ONE entity and make it run? Or should I have stuck with @ManyToMany? If so, how?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

客…行舟 2025-02-19 15:00:17

正如布鲁诺·阿尔维斯(Bruno Alves)所评论的那样,有两个解决方案用于此问题,

  1. 请尝试将方法命名为 getByid(long ID),因此Spring Data为您生成了所需的查询。此解决方案不需要查询注释,
  2. 也许您的HQL请求错了? 选择g。

尝试从游戏g中

As Bruno Alves commented, there are 2 solutions for this problem

  1. Try naming the method to getById(long id), so Spring Data generates the required query for you. Query annotation is not needed for this solution
  2. Maybe your HQL request is wrong? Try select g from Game g where g.id = :id

ManyToOne/ManyToMany do not matter for your request

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文