懂hibernate hql查询的进来,count查询出来的数据不正确,只有58分了,只求高手来秒杀

bingfengfzl 2012-05-12 12:01:06
员工和部门是多对多关系,一个员工有多个部门
员工和职位是多对多关系,一个员工有多个职位
部门和职位是一对多关系,一个部门下有多个职位

员工表

@Entity
@Table(name = "t_employees")
public class Employees implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -3878808306609451005L;
private Long id;
private String employeesName;
private Set<EmployeesLeave> employeesLeave = new HashSet<EmployeesLeave>();
private Set<Departments> departmentses = new HashSet<Departments>();
private Set<EmployeesPosition> employeesPositions = new HashSet<EmployeesPosition>();
private Set<RemunerationNotice> remunerationNotices = new HashSet<RemunerationNotice>();
private BranchCompanyData branchCompanyData;


.....


@OneToMany(mappedBy = "employees", fetch = FetchType.EAGER)
public Set<EmployeesLeave> getEmployeesLeave()
{
return employeesLeave;
}

@ManyToMany(targetEntity = Departments.class, cascade =
{ CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER)
@JoinTable(name = "T_Employees_Departments", joinColumns = @JoinColumn(name = "employeesId"), inverseJoinColumns = @JoinColumn(name = "departmentsId"))
public Set<Departments> getDepartmentses()
{
return departmentses;
}

@ManyToMany(targetEntity = EmployeesPosition.class, cascade =
{ CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER)
@JoinTable(name = "T_Employees_EmployeesPosition", joinColumns = @JoinColumn(name = "employeesId"), inverseJoinColumns = @JoinColumn(name = "employeesPositionsId"))
public Set<EmployeesPosition> getEmployeesPositions()
{
return employeesPositions;
}

@OneToMany(mappedBy = "employees", fetch = FetchType.EAGER)
public Set<RemunerationNotice> getRemunerationNotices()
{
return remunerationNotices;
}

@ManyToOne
public BranchCompanyData getBranchCompanyData()
{
return branchCompanyData;
}

@OneToMany(mappedBy = "employees", fetch = FetchType.LAZY)
public Set<ClockIn> getClockIn()
{
return clockIn;
}

}


职位表
@Entity
@Table(name = "t_employeesPosition")
public class EmployeesPosition
{
private Long id;
private String positionName;
private String positionForMoreInformation;

private Set<Employees> employeeses = new HashSet<Employees>();
private Departments departments;

@Id
@GeneratedValue
public Long getId()
{
return id;
}

@Column(length = 88)
@ManyToMany(targetEntity = Employees.class, cascade =
{ CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "employeesPositions")
public Set<Employees> getEmployeeses()
{
return employeeses;
}

@ManyToOne
public Departments getDepartments()
{
return departments;
}
......



部门表
@Entity
@Table(name = "t_departments")
public class Departments
{
private Long id;
private String departmentsName;
private String departmentsForMoreInformation;
private Set<Employees> employeeses = new HashSet<Employees>();

private Set<EmployeesPosition> employeesPositions = new HashSet<EmployeesPosition>();

@Id
@GeneratedValue
public Long getId()
{
return id;
}
@ManyToMany(targetEntity = Employees.class, cascade =
{ CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "departmentses")
public Set<Employees> getEmployeeses()
{
return employeeses;
}


@OneToMany(mappedBy = "departments", fetch = FetchType.EAGER)
public Set<EmployeesPosition> getEmployeesPositions()
{
return employeesPositions;
}
.....
}

现在问题就是用hql的做外连接查询总数的时候数据库员工明明就是两条数据
但查出来了60多条数据
StringBuilder countHql = new StringBuilder();
countHql.append("select count(*) from Employees employees LEFT OUTER JOIN employees.departmentses departmentses LEFT OUTER JOIN employees.employeesPositions positions");
为什么会查出来那么多?如何解决?
...全文
394 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bingfengfzl 2012-05-14
  • 打赏
  • 举报
回复
1.直接用前缀报错,说集合不能这么查询,后来按照网上的 弄了就不报错了,也就成了上面的语句了。。
2.是不是不加前缀查询出来的结果就不对啊?

chenhu87 2012-05-14
  • 打赏
  • 举报
回复
。。。 id忘记加别名 是 count(distinct employees.id)
chenhu87 2012-05-14
  • 打赏
  • 举报
回复
把count(*) 换成 count(distinct id)
ylovep 2012-05-12
  • 打赏
  • 举报
回复
换成 select * from Employees ,departmentses,employeesPositions
不懂楼主为什么加上 employees.departmentses 这个前缀
古布 2012-05-12
  • 打赏
  • 举报
回复
因为员工和职位是多对多关系。

countHql.append("select count(*) from Employees employees LEFT OUTER JOIN employees.departmentses departmentses LEFT OUTER JOIN employees.employeesPositions positions");

==>

countHql.append("select count(*) from Employees employees LEFT OUTER JOIN employees.departmentses ");

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧