JPA查询一个实体中有多个实体的许多人?
我有某种情况。假设我正在制作有关游戏的数据库。因此,首先,我有三个实体:Company
,Platform
和game
。
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>查询中的变量,例如我删除
company
或platform
>(例如:)从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如布鲁诺·阿尔维斯(Bruno Alves)所评论的那样,有两个解决方案用于此问题,
尝试从游戏g中
As Bruno Alves commented, there are 2 solutions for this problem
ManyToOne/ManyToMany do not matter for your request