如何通过hibernate从多个表查询中获取数据?

时间:2014-02-04 20:15:04

标签: java sql hibernate

我有一个用户管理应用程序,可以为每个用户分配一个团队,并为一个或多个访问不同的应用程序。现在,对于报告页面,我试图通过Hibernate从两个表(UserInfo& UserAppAccess)中获取数据,但我不能。 以下是表格:

表1(UserInfo):

@Entity
@Table(name = "user_info", uniqueConstraints = { @UniqueConstraint(columnNames =   "username"), @UniqueConstraint(columnNames = "email") })
public class UserInfo implements java.io.Serializable {

    public enum UserStatus {
        active, inactive
    }

    public enum UserType {
        user, creator
    }

    private static final long serialVersionUID = 2650114334774359089L;

    @Id
    @Column(name = "id", unique = true, nullable = false, length = 100)
    private String id;

    @Column(name = "username", unique = true, nullable = false, length = 50)
    private String username;

    @Column(name = "password", nullable = false, length = 80)
    private String password;

    @Column(name = "status", nullable = false, length = 10)
    @Enumerated(EnumType.STRING)
    private UserStatus status;

    @Column(name = "type", nullable = false, length = 10)
    @Enumerated(EnumType.STRING)
    private UserType type;

    @Column(name = "phone", nullable = true, length = 30)
    private String phone;

    @Column(name = "email", nullable = true, length = 50)
    private String email;

    @Column(name = "first_name", nullable = true, length = 50)
    private String firstName;

    @Column(name = "last_name", nullable = true, length = 50)
    private String lastName;

    @Column(name = "login", nullable = true, length = 100)
    private long login;

    @Column(name = "alert", nullable= true, length=500)
    private String alert;

    @OneToOne
    @JoinColumn(name = "team_id")
    private Team team;
}

表2(团队):

@Entity
@Table(name = "team", uniqueConstraints = { @UniqueConstraint(columnNames = "team_name"), @UniqueConstraint(columnNames = "team_code") })
public class Team implements java.io.Serializable {

    private static final long serialVersionUID = 7933770163144650730L;

    @Id
    @Column(name = "id", unique = true, nullable = false, length = 80)
    private String id;

    @Column(name = "team_name", unique = true, nullable = false, length = 100)
    private String name;

    @Column(name = "team_code", unique = true, nullable = false, length = 10)
    private String code;
}

表3(访问):

@Entity
@Table(name = "access_def")
public class Access implements java.io.Serializable {

    private static final long serialVersionUID = 7933770163144650730L;

    @Id
    @Column(name = "id", unique = true, nullable = false, length = 80)
    private String id;

    @Column(name = "access_name", unique = true, nullable = false, length = 100)
    private String name;

    @Column(name = "access_code", unique = true, nullable = false, length = 10)
    private String code;
}

表4(申请):

@Entity
@Table(name = "application", uniqueConstraints = { @UniqueConstraint(columnNames = "name") })

public class Application implements java.io.Serializable {

    private static final long serialVersionUID = 5803631085624275364L;

    @Id
    @Column(name = "name", nullable = false, length = 100)
    private String name;
}

表5(UserAppAccess):

@Entity
@Table(name = "user_app_access")
@Embeddable
public class UserAppAccess implements java.io.Serializable {

    private static final long serialVersionUID = 7933770163144650730L;

    @Id
    @Column(name = "id", unique = true, nullable = false, length = 80)
    private String id;

    @OneToOne
    @JoinColumn(name = "user_id")
    private UserInfo userInfo;

    @Column(name = "app_name", nullable = false, length = 100)
    private String appName;

    @OneToOne
    @JoinColumn(name = "access_id")
    private Access access;
}

我有一个报告页面,允许管理员选择多个选项(例如:列出团队测试和应用程序APP1中的所有活动用户)。

这是我获取数据的代码,但它不起作用:

public List<?> getReport(String teamId,String appName,UserStatus active,UserStatus inactive) {
    Session session = sessionFactory.getCurrentSession();
    String hql = "SELECT u.firstName,u.username,u.status,u.lastName,u.phone,u.team  From  UserInfo u,AppAccess a WHERE u.status =? OR u.status =? AND u.team.id = ? AND a.appName = :appName ";
    Query query = session.createQuery(hql);
    query.setParameter(0, active);
    query.setParameter(1, inactive);
    query.setParameter(2, teamId);
    query.setParameter("appName", appName);
    System.out.println(query.list());
    return query.list();
}

例如,当我通过

  • 活跃用户:有效
  • 非活动用户:null
  • 队:测试
  • 申请:app1

    teamId:28f66133-26c3-442b-a071-4d19d64ec0aeappName:app1active:activeinactive:null

我从return query.list();

取回来了
[[Ljava.lang.Object;@2961116f, [Ljava.lang.Object;@23bfa3a2, [Ljava.lang.Object;@7a8ff303, [Ljava.lang.Object;@9b88d2, [Ljava.lang.Object;@6333934d, [Ljava.lang.Object;@4f0bd71c, [Ljava.lang.Object;@125797cf, [Ljava.lang.Object;@34afa071, [Ljava.lang.Object;@764e75bc, [Ljava.lang.Object;@1913c652, [Ljava.lang.Object;@61413e5a, [Ljava.lang.Object;@264b898, [Ljava.lang.Object;@22930462, [Ljava.lang.Object;@6204cfa9, [Ljava.lang.Object;@29dd9285, [Ljava.lang.Object;@11be6f3c, [Ljava.lang.Object;@6d78d53d, [Ljava.lang.Object;@17f7cff1, [Ljava.lang.Object;@e74e382, [Ljava.lang.Object;@1c047338, [Ljava.lang.Object;@68286fe6, [Ljava.lang.Object;@36ca9a76, [Ljava.lang.Object;@2f62d514, [Ljava.lang.Object;@1932c5a, [Ljava.lang.Object;@6544c984, [Ljava.lang.Object;@70a2d0d, [Ljava.lang.Object;@2d13b417, [Ljava.lang.Object;@6894691f, [Ljava.lang.Object;@6781a7dc, [Ljava.lang.Object;@7133919a]  

3 个答案:

答案 0 :(得分:1)

我建议使用本机SQL和JDBC进行报告(请参阅 How should I use Hibernate Mapping while dealing with huge data table

出于性能原因,最好在DAO中从结果集创建视图模型对象。它可能看起来像混合抽象级别(视图层与持久层),但是当您需要获取大量数据并且不希望从持久性模型进行不必要的对象转换以查看模型时,它就可以了。

如果你想坚持使用hibernate,你可以在一个视图中定义一个syntetic实体和map,只包含多个必要的列:

@Entity
@Table("V_USER_REPORT")
public class UserAppData {
   // columns from table "user"
   @Id
   @Column(name = "id", unique = true, nullable = false, length = 100)
   private String id;
   @Column(name = "username", unique = true, nullable = false, length = 50)
   private String username;

   // columns from table "user"
   @Column(name = "app_name", nullable = false, length = 100)
   private String appName;

   // columns from table "team"
   @Column(name = "team_id", unique = true, nullable = false, length = 80)
   private String team_id;
   @Column(name = "team_name", unique = true, nullable = false, length = 100)
   private String name;
   @Column(name = "team_code", unique = true, nullable = false, length = 10)
   private String code;

   // and so on...
}

然后,您可以通过参数获取此类实体,就像使用普通实体一样。

答案 1 :(得分:0)

添加LEFT JOIN FETCHFETCH ALL PROPERTIES。这将强制JOINS而不是延迟初始化

 String hql = "SELECT u.firstName,u.username,u.status,u.lastName,u.phone,u.team  From  UserInfo u,AppAccess a FETCH ALL PROPERTIES WHERE u.status =? OR u.status =? AND u.team.id = ? AND a.appName = :appName ";

更多信息可在HQL Documentation

中找到

答案 2 :(得分:0)

首先:我希望你的每个实体类都有一个toString()方法(可以用eclipse自动生成),这样你就可以打印它们了。打印对象参考不足以推断您是否得到了您想要的东西。

其次,HQL连接的语法通常如下:

String queryString = "select distinct f from Foo f inner join foo.bars as b" +
                " where f.creationDate >= ? and f.creationDate < ? and b.bar = ?";

(摘自How do you create a Distinct query in HQL