我有一个包含多个实体的数据库,特别是Book
和User
,它们之间存在如下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
实体吗?它会像现在一样自动填充吗?也就是说,每当用户购买一本书时,该购买会记录在表中吗?
1条答案
按热度按时间pieyvz9o1#
User_Book不是实体。因此,您不能使用
createQuery
,但可以使用createNativeQuery
执行SQL查询:结果将为
List<Object[]>