Hibernate joining two table

96
March 13, 2018, at 5:11 PM

It is only recently that I am on top of the hibernate framwork and I can not get a join between two tables as a result. Could you help me understand where I'm wrong?

My Oracle Query is:

select * from ld_folder_scan ls, ld_folder_scan_content lss where ls.folder_id = lss.folder_id and ls.folder_id = 1;

I have two entity class ld_folder_scan and ld_folder_scan_content.I need to join both the tables and fetch all records which matching the given condition.

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      |
+-----------+-------------+

What I would like to get from the query result:

+-----------+----------------------------+-------------+------------+-------------+
| 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      |
+-----------+----------------------------+-------------+------------+-------------+

The Java Code:

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();
    }
}

What I get by debugging:

Folder Name::F001 Document Id::1
Folder Name::F001 Document Id::1
Folder Name::F001 Document Id::1
Folder Name::F001 Document Id::1

What I would like to get:

Folder Name::F001 Document Id::1
Folder Name::F001 Document Id::2
Folder Name::F001 Document Id::3
Folder Name::F001 Document Id::4

Where am I wrong?

Thanks.

Answer 1

You have missed the many-to-one mapping the child table. In parent table you have set it as one-to-many. likewise you have to configure the child table as well.

READ ALSO
Vertical scroll on dragging div instead of scrollbar

Vertical scroll on dragging div instead of scrollbar

I have a div that scrolls vertically

105
Replace not working?? Javascript/Jquery

Replace not working?? Javascript/Jquery

I guess I'm too dumb for thisWhy my code only replace the first instance of each word? I want to highlight all words with those names

127
jQuery: Transform delegations to self event listener

jQuery: Transform delegations to self event listener

I'm working on a huge js refactor of an app in which all event are listened usingon() function that delegates all elements that it need

121
how to avoid scrolling of table when on focus of selectize dropdown

how to avoid scrolling of table when on focus of selectize dropdown

I am using selectize drop down in an html table, where in last few columns of the table has drop down

85