简体   繁体   中英

Hibernate query/realting multiple tables using criteria or hql

i am a newbie to Hibernate and wanted to implement hibernate criteria or hql to query multiple tables to get the results and i am having problem to do that.. right now i am using native sql in hibernate to accomplish what i require but wanted to try the same with hql/criteria.. Below is the native query which i am using to get the results..

select table1.dmname,table2.polname,table3.path,table4.uidname 
    from table1,table2,table3,table4
    where table3.dmnid = table1.dmnid
    and table3.polid = table2.polid
    and table3.3.uid = table4.uid

below are my entity pojo classes

table 1 :

@Entity
@Table(name = "table1", uniqueConstraints = @UniqueConstraint(columnNames = "dmname"))
        @Cache(usage=CacheConcurrencyStrategy.READ_ONLY,region="domain")
public class table1  {

    private String dmnid;
    private String dmnname;

    @Id
    @Column(name = "dmnid", unique = true, nullable = false)
    public String getDmnid() {
        return this.dmnid;
    }

    public void setDmnid(String dmnid) {
        this.dmnid = dmnid;
    }

    @Column(name = "dmnname", unique = true, nullable = false)
    public String getDmnname() {
        return this.dmnname;
    }

    public void setDmnname(String dmnname) {
        this.dmnname = dmnname;
    }

table 2 :

@Entity
@Table(name = "table2", uniqueConstraints = @UniqueConstraint(columnNames = {
        "dmnname", "polname" }))
public class table2 implements java.io.Serializable {

    private String dmnname;
    private String polid;
    private String polname;

    // getters and setter

table 3:

@Entity
@Table(name = "table3")
public class table3 implements java.io.Serializable {

    private String dmnname;
    private String polid;
    private String uid;
    private String path;

    // getters and setters

table 4:

@Entity
@Table(name = "table4", uniqueConstraints = @UniqueConstraint(columnNames = "uidname"))
public class table4 implements java.io.Serializable {

    private String uid;
    private String uidname;

    // getters and setters

I havent implemented out the mapping between the tables in the pojo classes yet and i am having difficulty in understanding that.. so coming out for any help from the community to save me out.. thanks for looking into this and any help will be much appreciated..

I would suggest you refer to the Hibernate docs here for detailed understanding of associations. http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#associations

Regarding your question on HQL, it would look something like this to convert your native SQL (unless you need any explicit join condition):

  Query query = session.createQuery('from table1 t1,table2 t2,table3 t3,table4 t4
        where t3.dmnid = t1.dmnid
        and t3.polid = t2.polid
        and t3.uid = t4.uid');
  List results = query.list();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM