2015-04-16 43 views
4

子句中我一到许多,许多到一个双向的关系,我想执行下列SQL-Hibernate- @OneToMany与凡收集

select * from user_credential c 
join user_profile p on c.login_id = p.login_id 
join user_address a on p.address_id = a.address_id 
where p.profile_id = 1 

但是,我越来越的SQL-

的结果
select * from user_credential c 
join user_profile p on c.login_id = p.login_id 
join user_address a on p.address_id = a.address_id 
where p.credential_id = 1 

的休眠实体详情 -

@Entity 
    @Table(name = "user_credential") 
    public class UserCredential implements Serializable { 
     private static final long serialVersionUID = -2839071606927921689L; 

     @Id 
     @GeneratedValue(strategy = GenerationType.AUTO) 
     @Column(name = "login_id", insertable = false, updatable = false, nullable = false) 
     private int login_id = 0; 

     @Column(name = "password", insertable = true, updatable = true, nullable = false) 
     private String password = null; 

     @OneToMany(mappedBy = "login_id", fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
     private Set<UserProfile> profiles = null; 
//getters/setters 
} 

@Entity 
@Table(name = "user_profile") 
public class UserProfile implements Serializable { 

    private static final long serialVersionUID = 5765280899633539336L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "profile_id", length = 10, insertable = false, updatable = false, nullable = false) 
    private int profile_id = 0; 

    @ManyToOne 
    @JoinColumn(name = "login_id", insertable = true, updatable = true, nullable = false) 
    private UserCredential login_id = null; 

    @Column(name = "name", length = 20, insertable = true, updatable = true, nullable = false) 
    private String name = null; 

    @Column(name = "age", length = 3, insertable = true, updatable = true, nullable = false) 
    private byte age = 0; 

    @OneToMany(mappedBy = "profile_id", fetch = FetchType.EAGER, cascade = CascadeType.ALL) 
    private Set<UserAddress> address = null; 

//getters/setters 
} 

@Entity 
@Table(name = "user_address") 
public class UserAddress extends BaseTable{ 
    private static final long serialVersionUID = 5036341911955664992L; 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "address_id", length = 10, insertable = false, updatable = false, nullable = false) 
    private int address_id = 0; 

    @ManyToOne 
    @JoinColumn(name = "profile_id", insertable = true, updatable = true, nullable = false) 
    private UserProfile profile_id = null; 

    @Column(name = "state", length = 20, insertable = true, updatable = true, nullable = false) 
    private String state = null; 

    @Column(name = "city", length = 20, insertable = true, updatable = true, nullable = false) 
    private String city = null; 
//getters/setters 
} 

HQL:

select credential from UserCredential credential 
join credential.profiles profile 
where profile.profile_id = 1 

我不明白,为什么hibernate过滤父id的数据,以及我的SQL将如何执行。我正在使用休眠4.3.8

请告诉,如果有任何其他信息是必要的。

回答

1

我会写这样的查询:

select distinct c 
from Profile p 
join p.login_id c 
where p.profile_id = 1 

的副本被删除,从ChildParent加盟可能会帮助你解决问题。

+0

这种方法很有用,除非我在最后一个孩子** UserAddress **上添加连接和条件。另外,在这种情况下'join p.login_id c'不是必需的,因为它肯定会使用** UserProfile **创建一个_join_,并将获取唯一的结果。出于同样的原因,“不同”也不是必需的。但是,在使用上述查询时,我得到了** UserCredential **和** UserProfile **之间的ClassCastException。 –

+0

您得到一个ClassCastException,因为查询返回一个配置文件列表,而您没有更改以前的凭据列表。 –

+0

我没有调试过。它实际上是在返回一个单独的Credential记录而不是Profile,因为你正在提取_distinct c_。但结果仍然是一样的。如果您需要任何其他信息,请告诉。 –