0

Say, I have two entities:

Par

@Entity
@Table(name = "PAR")
public class Par implements java.io.Serializable, Simple {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", updatable = false, nullable = false)
    private int id; // id

    @Column(name = "CD", updatable = false, nullable = false)
    private String cd; // cd 

    @Column(name = "NAME", updatable = false, nullable = false)
    private String name; 

    @Column(name = "VAL_TP", updatable = false, nullable = true)
    private String tp; 

    @Column(name = "DATA_TP", updatable = false, nullable = true)
    private String dataTp; 
    
    public Integer getId() {
        return this.id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    
    public String getCd() {
        return this.cd;
    }
    public void setCd(String cd) {
        this.cd = cd;
    }
    
    public String getTp() {
        return tp;
    }
    public void setTp(String tp) {
        this.tp = tp;
    }
    
    public String getDataTp() {
        return dataTp;
    }
    public void setDataTp(String dataTp) {
        this.dataTp = dataTp;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}

Dw

@Entity
@Table(name = "DW")
public class Dw implements java.io.Serializable, Simple {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", updatable = false, nullable = false)
    private int id;
    
    @Column(name = "S1", updatable = true)
    private String s1;

    @Column(name = "NS1", updatable = true)
    private Double n1;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="FK_HFP", referencedColumnName="ID")
    private Par par; 

    public Dw() {
    }

    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    public String getS1() {
        return s1;
    }
    public void setS1(String s1) {
        this.s1 = s1;
    }

    public Double getN1() {
        return n1;
    }

    public void setN1(Double n1) {
        this.n1 = n1;
    }   

    public Par getPar() {
        return par;
    }

    public void setPar(Par par) {
        this.par = par;
    }
    
    
}

I need to find the row of "dw" which matches to the par.cd="TEST1" and get the value of S1 field.

I have to do it by iteration through dw:

for (Dw dw: dwMng.findAll()) {
    if (dw.getPar().getCd()=="TEST1") {
     System.out.println("Found row with par.cd:"+dw.getPar().getCd()+" value of S1="+dw.getS1);
    }
}           
    

But i think it is not convinient way to do it.

How can i quickly get such row?

For example (without using dw.getPar() ):

for (Dw dw: dwMng.findAll()) {
    if (dw.getCdOfPar("Test1")=="TEST1") {
     System.out.println("Found row with par.cd:"+dw.getPar().getCd()+" value of S1="+dw.getS1);
    }
}           

Or may be exist another way to do this job?

Can i do it without iteration and without HQL and SQLQuery?

Edt

I just need to "embed" child field into the parent

Community
  • 1
  • 1
Leo
  • 1,029
  • 4
  • 19
  • 40
  • Why don't you want to use HQL? Is the hibernate criteria language an alternative? Also, you should not compare Strings using "==". See http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – samjaf May 17 '16 at 05:12
  • agree with you about "==". But I don't want use any HQL or another Query because i already have the entity, and i am looking for the way how to get childs field of it. – Leo May 17 '16 at 06:24

1 Answers1

0

You can very well write a NamedQuery as well in this case.

You can write something like below

String sql = "select * from Employer employer
              join Employee employee on employee.employerid = employer.id 
              where employer.name = :name";                 

SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Object.class);
query.setParameter("name", name);
List results = query.list();
Abhijit Bashetti
  • 8,518
  • 7
  • 35
  • 47
shankarsh15
  • 1,947
  • 1
  • 11
  • 16