Java JPA 加入 Spring Boot 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35149071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA join in spring boot application
提问by qizer
I've read examples but have my personal question to you. I have 2 tables:
我读过例子,但有我个人的问题要问你。我有2张桌子:
Role:
id, name
User:
id, login, name, role_id
Role:
id, name
User:
id, login, name, role_id
Role entity
角色实体
@Entity
@Table(name = "role")
public class Role {
@Id
@Column(name = "id")
private long id;
@Column(name = "name", length = 45)
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "role")
private Set<User> user = new HashSet<>();
//getters and setters
User entity
用户实体
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id",insertable = false, updatable = false)
private long id;
@Column(name = "login")
private String login;
@Column(name = "user_name")
private String userName;
@ManyToOne(fetch = FetchType.LAZY)
private Role role;
//getters and setters
And repository:
和存储库:
public interface UserRepository extends JpaRepository<User, Long> {
String Q_GET_ALL_USERS = "from User u left join Role r on u.role_id=r.id";
@Query(Q_GET_ALL_USERS)
Collection<User> getAllUsers();
This code is showing: Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [from com.example.jpa.model.User u left join Role r on u.role_id=r.id]
此代码显示: Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [from com.example.jpa.model.User u left join Role r on u.role_id=r.id]
How I understand entity can't contains 'id' (in my case in Role
) for references and I should remove this field. But entity should have '@Id'.
我如何理解实体不能包含“id”(在我的例子中Role
)作为参考,我应该删除这个字段。但是实体应该有“@Id”。
In this case I should create new column in 'Role'? or I can use more beautiful decision?
在这种情况下,我应该在“角色”中创建新列吗?或者我可以使用更漂亮的决定?
I put all project to bb
我把所有的项目都放到了bb
回答by Roman C
No, you should create a new column in User
.
不,您应该在User
.
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "role_id")
private Role role;
回答by v.ladynev
To use join in HQL (JPQL) you don't need on
clause
要在 HQL (JPQL) 中使用连接,您不需要on
子句
String Q_GET_ALL_USERS = "select u from User u left join u.role";
This query doesn't have any sence because of you don't use role
in the where clause.
这个查询没有任何意义,因为你没有role
在 where 子句中使用。
If you want to get users with a fetched role you can use join fetch
如果您想获得具有提取角色的用户,您可以使用 join fetch
String Q_GET_ALL_USERS = "select u from User u left join fetch u.role";
Update
更新
Your schema for User
and Role
is not commonly used. I advice to you make @ManyToMany
association from user to roles and remove any user
association from the Role
您的架构User
和Role
不常用。我建议您将@ManyToMany
用户与角色user
关联起来,并从Role
@Entity
@Table(name = "user")
public class User {
@ManyToMany(fetch = FetchType.LAZY)
private Set<Role> roles;
}
@Entity
@Table(name = "role")
public class Role {
@Id
@Column(name = "id")
private long id;
@Column(name = "name", length = 45)
private String name;
}
回答by qizer
Thank you all for answers. Right entities and query below (plus tables schema).
谢谢大家的回答。下面是正确的实体和查询(加上表模式)。
Tables (queries)
表(查询)
CREATE TABLE role (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(45) NOT NULL
);
CREATE TABLE user (
id INT NOT NULL PRIMARY KEY IDENTITY,
login VARCHAR(45) NOT NULL,
user_name VARCHAR(45) NOT NULL,
role_id INT NOT NULL,
FOREIGN KEY (role_id) REFERENCES role (id)
);
Entities:
实体:
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id",insertable = false, updatable = false)
private long id;
@Column(name = "login")
private String login;
@Column(name = "user_name")
private String userName;
@ManyToOne(fetch = FetchType.LAZY)
private Role role;
//getters and setters
}
and
和
@Entity
@Table(name = "role")
public class Role {
@Id
@Column(name = "id")
private long id;
@Column(name = "name", length = 45)
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "role")
private Set<User> user = new HashSet<>();
//getters and setters
}
Repository
存储库
public interface UserRepository extends JpaRepository<User, Long> {
String Q_GET_ALL_USERS = "select u from User u left join u.role";
@Query(Q_GET_ALL_USERS)
Collection<User> getAllUsers();
}
@v-ladynevproposed alternative decision(use only @ManyToMany
in User
). More details you can find in comments under this answer.
When I check this decision I will update this answer (I hope I don't forget it :-))
@v-ladyev提出了替代决定(仅@ManyToMany
在 中使用User
)。您可以在此答案下的评论中找到更多详细信息。当我检查这个决定时,我会更新这个答案(我希望我不会忘记它:-))
回答by Ram Pukar
Models
楷模
@Entity
@Table(name = "sys_std_user")
public class StdUser {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "class_id")
public int classId;
@Column(name = "user_name")
public String userName;
}
@Entity
@Table(name = "sys_std_profile")
public class StdProfile {
@Id
@Column(name = "pro_id")
public int proId;
@Column(name = "full_name")
public String fullName;
}
Controllers
控制器
@PersistenceUnit
private EntityManagerFactory emf;
@GetMapping("/join")
public List actionJoinTable() {
EntityManager em = emf.createEntityManager();
List arr_cust = em
.createQuery("SELECT u.classId, u.userName, p.fullName FROM StdUser u, StdProfile p WHERE u.classId=p.proId")
.getResultList();
return arr_cust;
}
Result:
结果:
[
[
1,
"Ram",
"Ram Pukar Chaudhary"
],
[
2,
"Raja",
"Raja Kishor Shah"
]
]