jpa 抽象架构类型“User_Book”未知

gopyfrb3  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(115)

我有一个包含多个实体的数据库,特别是BookUser,它们之间存在如下ManyToMany关系:
书:

@Entity
@Table(name = "Books")
public class Book implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.TABLE)
  @Column(name = "bookId", nullable = false, unique = true)
  private Long id;

  @Column(name = "title", nullable = false)
  private String title;

  @Column(name = "price", nullable = false)
  private int price;

  @ManyToMany(fetch = FetchType.EAGER)
  @JoinTable(name = "User_Book",
             joinColumns = @JoinColumn(name = "bookId"),
             inverseJoinColumns = @JoinColumn(name = "userId"))
  private Set<UserAccount> users;

用户:

@Entity
@Table(name = "UserAccounts")
public class UserAccount implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue(strategy = GenerationType.TABLE)
  @Column(name = "userId", nullable = false, unique = true)
  private Long id;

  @Column(name = "username", nullable = false, unique = true)
  private String username;

  @Column(name = "password", nullable = false)
  private String password;

  @ManyToMany(fetch = FetchType.EAGER)
  @JoinTable(name = "User_Book",
             joinColumns = @JoinColumn(name = "userId"),
             inverseJoinColumns = @JoinColumn(name = "bookId"))
  Set<Book> purchasedBooks;

一切正常,数据库中确实创建了表User_Book,问题似乎与该表的访问有关。
例如,

Query query = entityManager.createQuery("SELECT u FROM User_Book u");

一直在告诉我
The abstract schema type 'User_Book' is unknown
那么,我应该从头开始创建User_Book实体吗?它会像现在一样自动填充吗?也就是说,每当用户购买一本书时,该购买会记录在表中吗?

pieyvz9o

pieyvz9o1#

User_Book不是实体。因此,您不能使用createQuery,但可以使用createNativeQuery执行SQL查询:

Query query = entityManager.createNativeQuery("SELECT * FROM User_Book");

结果将为List<Object[]>

相关问题