在休眠中编写内部联接

时间:2018-07-10 07:28:48

标签: java hibernate

我有三类用户,部门和名称

User.java

System.InvalidOperationException : Path 'D:\home\data\<DIR>\<dir>' does not exist.

Department.java

public class User implements Serializable{  
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;

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

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

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

    @Column(name="mobNo")
    private BigDecimal mobNo;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "deptId")
    private Department department;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "desgId")
    private Designation designation;

    @Column(name="grpId")
    private int grpId ;

    @Column(name="userLevel")
    private int userLevel ;

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

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

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

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

    @Column(name="creationDate")
    private Timestamp creationDate;

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

    @Column(name="hintId")
    private int hintId ;

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

    @Column(name="otp")
    private String otp ;
}

Designation.java

public class Department implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int deptid;

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

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

    @Column(name="creationdt")
    private Timestamp creationdt;

    @Column(name="status")
    private String status;
}

SQL

public class Designation implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int desgid;

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

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

    @Column(name="creationdt")
    private Timestamp creationdt;

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

    public Designation() {
    }
}

我想在上面写查询 如何在休眠状态下编写此sql查询? 我按条件尝试过,但是无法正常工作

2 个答案:

答案 0 :(得分:1)

在这种情况下,您实际上并不需要hql连接,只需浏览部门和名称关联即可。如果您确实希望将结果作为数组的集合,请尝试此操作

select
    u.username, u.mobno, u.department.deptnm, u.designation.desgnm, u.loginstatus
from
    User u

使用此类查询

Query query=sessionFactory.getCurrentSession().createQuery("select u.userName, u.mobNo, u.department.deptnm, u.designation.desgnm, " + "u.loginStatus from User u");
List<Object[]> list=query.list(); 

但是如果您只想获取用户列表

Query query=sessionFactory.getCurrentSession().createQuery("from User u");
List<User> list=query.list();

答案 1 :(得分:0)

您尝试这种方式吗?

  String query = "select u.username, u.mobno, 
  u.loginstatus, dp.deptnm, ds.desgnm  from  USERMST 
  u, DEPTMST dp, DESIGMST ds where 
  u.deptid=dp.deptid and u.desgid=ds.desgid";

  SQLQuery query = session.createSQLQuery(query);

query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

  List data = query.list();

  for (Object object : data) {
     Map row = (Map) object;
     sysout(row.get("username"));
     //similar-way all the values

  }