如何从hibernate中的同一个表的结果中获取表中的数据 [英] How to fetch data from a table from the result of same table in hibernate

查看:134
本文介绍了如何从hibernate中的同一个表的结果中获取表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Rest中使用Hibernate,我从表中获取数据。我的用户名和职业中有两列。我创建了一种方法,在该方法中,我传递用户名并获得与用户名相对应的职业,现在我想通过专业搜索获得并希望获得具有该职业的所有用户名。



我使用用户名取得职业,但我无法取得拥有该职业的用户名,所以请建议我这样做。



这是我的实体类 Profession.java

  @Entity 
@Table(name =user_profession )
@JsonIgnoreProperties({hibernateLazyInitializer,handler})
public class Profession实现可序列化{


private static final long serialVersionUID = 1L;


@Id
@GeneratedValue
@Column(name =id)
私人长ID;

@Column(name =profession)
private String profession;

@Column(name =username)
private String username;


// Getters和setters

public long getId(){
return id;
}

public String getUsername(){
return username;
}

public void setUsername(String username){
this.username = username;
}

public void setId(long id){
this.id = id;
}

public String getProfession(){
return profession;
}

public void setProfession(String profession){
this.profession = profession;
}

}

这是我的 DAO class

  @SuppressWarnings(unchecked)
public List< Profession> getProfessionById(String uname)抛出异常{
session = sessionFactory.openSession();
session.beginTransaction();
String queryString =from Profession where username =:uname;
Query query = session.createQuery(queryString);
query.setString(uname,uname);
//列表< Profession> queryResult =(List< Profession>)query.uniqueResult();
session.getTransaction()。commit();
返回query.list();

}

请帮助我。提前Thanx

解决方案

您已添加此代码

  String queryString =from Profession where username =:uname; 
Query query = session.createQuery(queryString);
query.setString(uname,uname);

但缺少

 列表< Profession> professionList = query.list(); 
for(Profession:professionList){
System.out.println(获取你需要的东西+ profession.getUsername());
}


I am using Hibernate with spring Rest and i am fetching data from a table. There are two columns in my table username and profession. I created a method in which i am passing a username and getting a profession corresponding to username now i want to search by profession which i got and want to get all usernames who have that profession.

I have fetched profession using a username but i am unable to fetch usernames who have that profession so please suggest me the way to do this.

Here is my Entity class Profession.java

@Entity
@Table(name="user_profession")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Profession implements Serializable {


private static final long serialVersionUID = 1L;


@Id
@GeneratedValue
@Column(name="id")
private long id;

@Column(name="profession")
private String profession;

@Column(name="username")
private String username;


//Getters and setters

public long getId() {
    return id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public void setId(long id) {
    this.id = id;
}

public String getProfession() {
    return profession;
}

public void setProfession(String profession) {
    this.profession = profession;
}

}

Here is my DAO class

@SuppressWarnings("unchecked")
public List<Profession> getProfessionById(String uname) throws Exception {
session = sessionFactory.openSession();
  session.beginTransaction();
  String queryString = "from Profession where username = :uname";
  Query query = session.createQuery(queryString);
  query.setString("uname", uname);
  //List<Profession> queryResult = (List<Profession>) query.uniqueResult();
  session.getTransaction().commit();
  return query.list();

}

Please help me . Thanx in advance

解决方案

You have added this code

 String queryString = "from Profession where username = :uname";
 Query query = session.createQuery(queryString);
 query.setString("uname", uname);

But missing

  List<Profession> professionList = query.list();
   for(Profession profession: professionList ){
     System.out.println("Get What you need Like"+profession.getUsername());
    }

这篇关于如何从hibernate中的同一个表的结果中获取表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆