如何使用hibernate查询连接表? [英] how to query the join table using hibernate?
问题描述
我在Oracle SQL Developer中创建了三个表,即
$ b
1.Test_Employee2
2.Test_Project2
3.Employee_Project2
。
表格 Employee_Project2
是连接表,作为 Test_Project2
和 Employee_Project2
是多对多
。
在hibernate中,我创建了两个hibernate类 TestEmployee
和 TestProject
分别为 Test_Project2
和 Employee_Project2
表格>
,表 Employee_Project2
定义在 TestProject
hibernate类中,如下所示:
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name =Employee_Project2,joinColumns = @JoinColumn(name =proj_id ),inverseJoinColumns = @JoinColumn(name =emp_id))
private Set< TestEmployee> employeesList;
我填充了表 Test_Project2
和 Employee_Project2
和一些记录,并且连接表 Employee_Project2
自动填充了一些记录。
现在我现在面临的问题是,我想在连接表 Employee_Project2
上使用hiberante的简单select语句,如下所示:
String hql =FROM Employee_Project2;
Query query = session.createQuery(hql);
列表结果= query.list();
for(Object row:results){
//在这里做什么
}
尽管连接表'Employee_Project2'不是一个hibernate类,我该如何做到这一点?
update strong>: 我想检索hibernate表TestProject中的所有记录,所以我写了下面的代码: 现在的问题是,在运行时我收到类似以下的内容: 如何获取每行中包含的值? 你想要做的是创建类型化查询。通过适当的映射,您可以获得相关对象 - 无需查询连接表为 使用propper关系映射,您可以获得像这样的关系: 至于如何 - 主题过于宽泛无法覆盖在单个答案等,但你应该能够从这里开始 - http://hibernate.org/orm /documentation/5.1/ I have created three tables in Oracle SQL Developer namely I populated the tables how can I do that despite the join table 'Employee_Project2' is not a hibernate class.? update: I would like to retrieve all the records in the hibernate table "TestProject", so i wrote the following code now the problem is, at run time i receive something like the following how can i get the values contained in the each row?? What you want to do is to create typed query. With proper mapping you can get related objects as well - no need to query join tables as And with propper relation mapping you can get relations like this: As for "how to"s - the topic is too broad to cover it in single answer etc. but you should be able to start from here - http://hibernate.org/orm/documentation/5.1/ 这篇关于如何使用hibernate查询连接表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
String hql =FROM TestProject;
Query query = session.createQuery(hql);
列表结果= query.list();
System.out.println(results.get(0)+ results.get(0).toString());
results.get(0)msc.hibernate.persistence.TestProject@12ec9534
ORM
将为您执行此操作:
查询查询= session.createQuery(hql);
List< TestProject> results = query.list();
(TestProject row:results){
//在这里做什么
//做任何你想做的事
}
(TestProject row:results){
Set< TestEmployee>员工= row.getEmployeesList();
//做更多工作。
}
1.Test_Employee2
2.Test_Project2
3.Employee_Project2
.
The table Employee_Project2
is the join table as the relation between Test_Project2
and Employee_Project2
is Many-To-Many
.
In hibernate I created to two hibernate classes TestEmployee
and TestProject
for Test_Project2
and Employee_Project2
tables respectively,
and the table Employee_Project2
was defined in TestProject
hibernate class as follows:@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "Employee_Project2", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "emp_id"))
private Set<TestEmployee> employeesList;
Test_Project2
and Employee_Project2
with some records, and the join table Employee_Project2
automatically got populated with some records.
now the problem I am facing currently is, I want to use a simple select statement on the join table Employee_Project2
using hiberante as follows:String hql = "FROM Employee_Project2";
Query query = session.createQuery(hql);
List results = query.list();
for (Object row : results) {
//what to do here
}
String hql = "FROM TestProject";
Query query = session.createQuery(hql);
List results = query.list();
System.out.println("results.get(0)" + results.get(0).toString());
results.get(0)msc.hibernate.persistence.TestProject@12ec9534
ORM
will do this for you:Query query = session.createQuery(hql);
List<TestProject> results = query.list();
for (TestProject row : results) {
//what to do here
// do whatever you want
}
for (TestProject row : results) {
Set<TestEmployee> employees=row.getEmployeesList();
// do more work.
}