我想使用JPQL内连接进行查询(SELECT)。我正在使用hibernate ddl auto从实体创建表。
我有2个实体,它们之间是多对多关系(一个工作室可以由许多经理(用户)管理,一个经理(用户)可以管理许多工作室)。
如你所知,对于多对多关系,我们使用一个中间表来做一个本机SQL的“SELECT”,但是我使用JPA和JPQL作为查询语言,所以我的问题是:如何在它们之间建立多对多关系的两个表之间进行JOIN SELECT?
以下实体是我的实体:
实体1(工作室):
@Entity
@Table(name = "studio")
public class Studio implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "studio_name", nullable = false)
private String studioName;
/**
* Indicates if the studio is active or inactive in the data base.
*/
@Column(name = "active")
private Boolean active;
/**
* The studio owner is the main manager for a studio.
*/
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "studio_owner", referencedColumnName = "id", nullable = false)
private User studioOwner;
/**
* Represents all the possible managers for this studio.
*/
@ManyToMany(fetch = FetchType.LAZY)
private List<User> studioManagers;
//--Getters and setter below
}
实体2(用户):
@Entity
@Table(name = "userr")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "user_name", nullable = false, unique = true)
private String userName;
@Column(name = "email", nullable = false, unique = true)
private String email;
@Column(name = "password", length = 60)
private String password;
@Column(name = "name")
private String name;
@Column(name = "last_name")
private String lastName;
@Column(name = "user_state", nullable = false)
@Enumerated(EnumType.STRING)
private UserState userState;
/**
* The roles for this user.
*/
@ManyToMany(fetch = FetchType.EAGER)
private List<Role> roles;
public User() {
}
//--Getters and setter below
}
对不起,我说英语,我说西班牙语。
StudioRepository.java只写一个方法
List<Studio> findById(Integer id);
这将获取与此工作室关联的所有用户的工作室。
例如:Studio{"id": 1,"studioName":"一些","studioOwner":[{………},{……}]}