直到最近我才处于hibernate框架之上,因此无法在两个表之间进行连接。你能帮我理解我错在哪里吗?
我的Oracle查询是:
select * from ld_folder_scan ls, ld_folder_scan_content lss where ls.folder_id = lss.folder_id and ls.folder_id = 1;
我有两个实体类ld_folder_scan和ld_folder_scan_content。我需要连接这两个表并获取与给定条件匹配的所有记录。
ld_folder_scan
+-----------+----------------------------+-------------+------------+
| folder_id | status_folder_scan_type_id | folder_name | service_id |
+-----------+----------------------------+-------------+------------+
| 1 | Sent | F001 | 1 |
| 2 | Receive | F002 | 1 |
+-----------+----------------------------+-------------+------------+
ld_folder_scan_content
+-----------+-------------+
| folder_id | document_id |
+-----------+-------------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
| 2 | 5 |
| 2 | 6 |
| 2 | 7 |
| 2 | 8 |
| 2 | 9 |
+-----------+-------------+
我想从查询结果中得到什么:
+-----------+----------------------------+-------------+------------+-------------+
| folder_id | status_folder_scan_type_id | folder_name | service_id | document_id |
+-----------+----------------------------+-------------+------------+-------------+
| 1 | Sent | F001 | 1 | 1 |
| 1 | Sent | F001 | 1 | 2 |
| 1 | Sent | F001 | 1 | 3 |
| 1 | Sent | F001 | 1 | 4 |
+-----------+----------------------------+-------------+------------+-------------+
Java代码:
package org.hibernate;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "ld_folder_scan")
public class ld_folder_scan {
@Id
@GeneratedValue
private long folder_id;
private short status_folder_scan_type_id;
private String folder_name;
private short service_id;
@OneToMany( cascade = CascadeType.ALL, orphanRemoval = true )
@JoinColumn(name = "folder_id")
private List<ld_scatole_scan_content> ld_scatole_scan_content = new ArrayList<>();
public long getFolder_id() {
return folder_id;
}
public void setFolder_id(long folder_id) {
this.folder_id = folder_id;
}
public short getStatus_folder_scan_type_id() {
return status_folder_scan_type_id;
}
public void setStatus_folder_scan_type_id(short status_folder_scan_type_id) {
this.status_folder_scan_type_id = status_folder_scan_type_id;
}
public String getFolder_name() {
return folder_name;
}
public void setFolder_name(String folder_name) {
this.folder_name = folder_name;
}
public short getService_id() {
return service_id;
}
public void setService_id(short service_id) {
this.service_id = service_id;
}
public void setLd_folder_scan_content(List<ld_folder_scan_content> ld_folder_scan_content) {
this.ld_folder_scan_content = ld_folder_scan_content;
}
}
---------------------------------------------------
package org.hibernate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "ld_folder_scan_content")
public class ld_folder_scan_content {
@Id
@GeneratedValue
private long folder_id;
private long document_id;
public long getFolder_id() {
return folder_id;
}
public void setFolder_id(long folder_id) {
this.folder_id = folder_id;
}
public long getDocument_id() {
return lavorazione_id;
}
public void setDocument_id(long document_id) {
this.document_id = document_id;
}
}
---------------------------------------------------------------------------
package org.hibernate;
import java.util.List;
import org.hibernate.cfg.Configuration;
public class OracleConnection {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
String hql = "from ld_folder_scan where folder_id = 1";
List<ld_scatole_scan> list = (List<ld_folder_scan>)session.createQuery(hql).getResultList();
for(ld_folder_scan sca : list){
for(int i=0;i<sca.getLd_folder_scan_content().size();i++){
System.out.println("Folder Name::"+sca.getFolder_name() + " Document Id::" +sca.getLd_folder_scan_content().get(i).getDocument_id());
}
}
session.getTransaction().commit();
}
}
调试得到的结果:
Folder Name::F001 Document Id::1
Folder Name::F001 Document Id::1
Folder Name::F001 Document Id::1
Folder Name::F001 Document Id::1
我想得到什么:
Folder Name::F001 Document Id::1
Folder Name::F001 Document Id::2
Folder Name::F001 Document Id::3
Folder Name::F001 Document Id::4
我哪里错了?
感谢。
答案 0 :(得分:0)
您错过了子表的多对一映射。在父表中,您将其设置为一对多。同样你也必须配置子表。