我有一个关于如何用左连接和hibernate中的选项编写hql的问题。我试着在下面的arama函数中编写hql,但结果并没有作为输出出现。
我怎么写呢。是否有可能为我写。谢谢。我也分享了我的代码。
以下是我的代码。
Ogrenci实体
@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
@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问题
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();
}
}
答案 0 :(得分:1)
试试
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();