使用HQL选择提取集合失败

时间:2011-09-14 09:17:15

标签: hibernate select collections hql

我的问题是我无法在我的选择查询中包含类的集合属性 我确实在两个班级(团队和成员)之间有父/子关系

为了提高提取性能我想使用HSL查询来仅选择 返回对象的必要属性(Row) 这可以通过以下方式完成:

List<Object> returnlist = session.createQuery("select t.name, t.members from Team t").list();

但这会导致hibernate异常:

WARN org.hibernate.util.JDBCExceptionReporter,JDBCExceptionReporter:100 - SQL Error: -1, SQLState: 42X01
ERROR org.hibernate.util.JDBCExceptionReporter,JDBCExceptionReporter:101 - Syntax error: Encountered "." at line 1, column 33.

我在下面附上了完整的类源。 非常感谢你提前帮助我 Du我必须放置一个额外的连接来使它工作,或者这是一个hibernate错误?

Team.java

@Entity()
@Table(name = "TEAM")
public class Team implements Serializable {

  private Long id; 
  private String name;
  private String remark; 
  private List<Member> members = new ArrayList<Member>();

  @Id
  @Column(name = "id")
  @GeneratedValue(strategy=GenerationType.AUTO)
  public Long getId() {
      return id;
  }

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

  @CollectionOfElements
  @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="team")
  public List<Member> getMembers() {
      return members;
  }


  public void addMember(Member member) {
      if (!getMembers().contains(member)) {
        getMembers().add(member);
          if (member.getTeam() != null) {
            member.getTeam().getMembers().remove(members);
          }
          member.setTeam(this);
      }
  }

  public void setMembers(List<Member> members) {
      this.members = members;
  }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getRemark() {
        return remark;
    }

}

Member.java

@Entity()
@Table(name = "MEMBER")
public class Member implements Serializable {

    private Long id;
    private String name = "";
    private Team team;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getId() {
        return id;
    }

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


    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setTeam(Team team) {
        this.team = team;
    }

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "team_id")
    public Team getTeam() {
        return team;
    }

}

0 个答案:

没有答案