开发者

using HQL select fetching collections failed

开发者 https://www.devze.com 2023-04-04 14:12 出处:网络
My problem is that i am not able to include collection properties of a class in my select query I do have a parent/child relation between two classes (Team and Member)

My problem is that i am not able to include collection properties of a class in my select query I do have a parent/child relation between two classes (Team and Member)

for improving fetching performance i would like to use a HSL query to only select the necessary properties of the return object (Row) this is done by doing:

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

but this causes a hibernate exception:

WARN org.hibernate.util.JDBCExceptionReporter,JDBCExceptionReporter:100 - SQL Error: -1, SQLState: 42X01
ERROR org.hibernate.util.JDBCExceptionReporter,JDBCExceptionRepo开发者_如何学Pythonrter:101 - Syntax error: Encountered "." at line 1, column 33.

I attached the complete class sources below. Thanking you very much in advance for helping me Du i have to place an additional join to get it working, or is this a hibernate bug?

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

精彩评论

暂无评论...
验证码 换一张
取 消