简体   繁体   中英

HQL hibernate left join where clause

I have a problem about how to write hql with left join and where option in hibernate. I tried to write hql in arama function in the below but the result wasn't appeared as an output.

How could I write it. Is it possible to write it for me . Thank you. I also shared my codes.

Here are my codes below.

Ogrenci Entity

@Entity
@Table(name = "OGRENCI", schema = "OGRENCI")
public class OGRENCI {

    @Id
    @SequenceGenerator(name = "ActorSeq", sequenceName = "ACTOR_SEQ", allocationSize = 1)
    @GeneratedValue(generator = "ActorSeq", strategy = GenerationType.SEQUENCE)
    @Column(name = "ID")
    private Long Id;

    @Column(name = "AD")
    private String ad;

    @ManyToMany(mappedBy="ogrenciler")  
    Set<Ders> dersler = new HashSet<Ders>();


    public Set<Ders> getMovies() {
        return dersler;
    }

    public void setMovies(Set<Ders> dersler) {
        this.dersler = dersler;
    }

    public OGRENCI() {
        super();
        // TODO Auto-generated constructor stub
    }

    public OGRENCI(String ad) {
        super();
        this.ad = ad;
    }

    public String getAd() {
        return ad;
    }

    public void setName(String ad) {
        this.ad = ad;
    }

}

DERS entity

@Entity
@Table(name = "DERS", schema = "OGRENCI")
public class Ders {

    @Id
    @SequenceGenerator(name = "DersSeq", sequenceName = "Ders_SEQ", allocationSize = 1)
    @GeneratedValue(generator = "DersSeq", strategy = GenerationType.SEQUENCE)
    @Column(name = "ID")
    private Long Id;

    @Column(name = "NAME")
    private String Name;

    @ManyToMany(cascade=CascadeType.PERSIST)
    @JoinTable(name="Ders_Ogrenciler",
      joinColumns={@JoinColumn(name="Ders_ID")},
      inverseJoinColumns={@JoinColumn(name="OGRENCI_ID")}
              ) 
    Set<OGRENCI> ogrenciler = new HashSet<OGRENCI>();

    public Set<OGRENCI> getActors() {
        return ogrenciler;
    }

    public void setActors(Set<OGRENCI> ogrenciler) {
        this.ogrenciler = ogrenciler;
    }

    public Ders() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Ders(String name) {
        super();
        Name = name;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

}

SQL problem

private static void arama() {
        // TODO Auto-generated method stub

        Scanner in = new Scanner(System.in);
        System.out.println("-----------------------------------------------------------------------");
        System.out.print("Dersin Adını Giriniz :");
        String ders = in.nextLine();

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction trx = session.getTransaction();

        try {

            trx.begin();
            System.out.println("------------------------KALANLAR-----------------------");
            List ogrenci = session.createQuery("Select o.ad FROM OGRENCI o left join o.dersler ders WHERE ders.id='"+ders+"'").list();

            for (Iterator iterator1 = ogrenci.iterator(); iterator1.hasNext();) {
                OGRENCI ogr = (OGRENCI) iterator1.next();
                System.out.println(ogr.getAd());

            }

            trx.commit();
        } catch (Exception e) {

            if (trx != null)
                trx.rollback();

        } finally {
            session.close();
        }

    }

Try that

Query q= session.createQuery("Select o.ad FROM OGRENCI o left join o.dersler ders WHERE ders.id=:id");
q.setParameter("id",ders);
q.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