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
As Bruno Alves commented, there are 2 solutions for this problem
ManyToOne/ManyToMany do not matter for your request
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.