Hibernate:无法检索关联的列表

发布于 2024-09-16 07:11:42 字数 4999 浏览 3 评论 0原文

实体 X 具有实体 Y 的列表,实体 Y 具有实体 Z 的实例。X

到 Y 之间的关系是 OneToMany,Y 到 Z 之间的关系是 ManyToOne。

我想检索 X 并检索所有关联的实体。

我要编写什么 HQL 查询才能一次检索整个链。目前是 hibernateTemplate.find("from X")。 或者我应该使用什么注释?

X=ServiceProvider, Y=BusinessLocations.java, Z=State.java

我有下面注释的实体,并且我将整个链保存到数据库中,但是当我尝试检索 Y(BusinessLocation) 列表时,我

什么也没得到。

我该如何将 X 与 Y 以及 Y 与 Z 连接起来?

下面是实体 x、Y 和 Z。

ServiceProvider.java

@Entity
public class ServiceProvider implements Serializable{

    private Long id;    
    private Set<BusinessLocation> businessLocations = new HashSet<BusinessLocation>();

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @OneToMany(mappedBy="serviceProvider", targetEntity=BusinessLocation.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    public Set<BusinessLocation> getBusinessLocations() {
        return businessLocations;
    }

    public void setBusinessLocations(Set<BusinessLocation> businessLocations) {
        this.businessLocations = businessLocations;
    }

    public boolean equals(Object obj) {
        if(!(obj instanceof ServiceProvider)) return false;
        ServiceProvider other = (ServiceProvider) obj;
        return new EqualsBuilder().append(businessLocations, other.businessLocations).isEquals();
    }
}

BusinessLocation.java

@Entity
public class BusinessLocation implements Serializable{

    private Long id;
    private String address;
    private String city;
    private State state;
    private String pincode;
    private ServiceProvider serviceProvider;

    public BusinessLocation() {     
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="state_id")
    public State getState() {
        return state;
    }
    public void setState(State state) {
        this.state = state;
    }   
    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

    public String getPincode() {
        return pincode;
    }

    @ManyToOne
    @JoinColumn(name="serviceProvider_id")
    public ServiceProvider getServiceProvider() {
        return serviceProvider;
    }
    public void setServiceProvider(ServiceProvider serviceProvider) {
        this.serviceProvider = serviceProvider;
    }

    public boolean equals(Object obj) {
        if( !(obj instanceof BusinessLocation)) return false;
        BusinessLocation other = (BusinessLocation) obj;        
        return new EqualsBuilder().append(address, other.address).append(city, other.city).append(state, other.state).append(pincode, 

other.pincode).append(serviceProvider, other.serviceProvider).isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder().append(address).append(city).append(state).append(pincode).append(serviceProvider).toHashCode();
    }
}

State.java

@Entity
public class State implements Serializable {

    private Long id;
    private String abbreviatedName;
    private String name;
    private List<BusinessLocation> businessLocations;

    @Id
    @GeneratedValue 
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getAbbreviatedName() {
        return abbreviatedName;
    }
    public void setAbbreviatedName(String abbreviatedName) {
        this.abbreviatedName = abbreviatedName;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }


    @OneToMany(mappedBy="state", targetEntity=BusinessLocation.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    public List<BusinessLocation> getBusinessLocations() {
        return businessLocations;
    }
    public void setBusinessLocations(List<BusinessLocation> businessLocations) {
        this.businessLocations = businessLocations;
    }

    public boolean equals(Object obj) {
        if(! (obj instanceof State)) return false;
        State other = (State) obj;
        return new EqualsBuilder().append(abbreviatedName, other.abbreviatedName).append(name, other.name).append(businessLocations, 

other.businessLocations).isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder().append(name).append(abbreviatedName).append(businessLocations).toHashCode();
    }

}

有人可以帮我吗?

谢谢

An entity X has a list of entity Y and the entity Y has an instance of entity Z.

The relation between X to Y is OneToMany and the relation between Y to Z is ManyToOne.

I want to retrieve X and have all the associated entities retrieved with them as well.

What HQL query do I write so that I get the whole chain retrieved all at once. At present its hibernateTemplate.find("from X").
or What annonations do I use for it?

X=ServiceProvider, Y=BusinessLocations.java, Z=State.java

I have the entities annotated below and I am having the whole chain persisted into database but when i try to retrieve the list of Y(BusinessLocation), I get

nothing.

What do I do join X with Y and Y with Z?

Below are the entities x, Y and Z.

ServiceProvider.java

@Entity
public class ServiceProvider implements Serializable{

    private Long id;    
    private Set<BusinessLocation> businessLocations = new HashSet<BusinessLocation>();

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    @OneToMany(mappedBy="serviceProvider", targetEntity=BusinessLocation.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    public Set<BusinessLocation> getBusinessLocations() {
        return businessLocations;
    }

    public void setBusinessLocations(Set<BusinessLocation> businessLocations) {
        this.businessLocations = businessLocations;
    }

    public boolean equals(Object obj) {
        if(!(obj instanceof ServiceProvider)) return false;
        ServiceProvider other = (ServiceProvider) obj;
        return new EqualsBuilder().append(businessLocations, other.businessLocations).isEquals();
    }
}

BusinessLocation.java

@Entity
public class BusinessLocation implements Serializable{

    private Long id;
    private String address;
    private String city;
    private State state;
    private String pincode;
    private ServiceProvider serviceProvider;

    public BusinessLocation() {     
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="state_id")
    public State getState() {
        return state;
    }
    public void setState(State state) {
        this.state = state;
    }   
    public void setPincode(String pincode) {
        this.pincode = pincode;
    }

    public String getPincode() {
        return pincode;
    }

    @ManyToOne
    @JoinColumn(name="serviceProvider_id")
    public ServiceProvider getServiceProvider() {
        return serviceProvider;
    }
    public void setServiceProvider(ServiceProvider serviceProvider) {
        this.serviceProvider = serviceProvider;
    }

    public boolean equals(Object obj) {
        if( !(obj instanceof BusinessLocation)) return false;
        BusinessLocation other = (BusinessLocation) obj;        
        return new EqualsBuilder().append(address, other.address).append(city, other.city).append(state, other.state).append(pincode, 

other.pincode).append(serviceProvider, other.serviceProvider).isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder().append(address).append(city).append(state).append(pincode).append(serviceProvider).toHashCode();
    }
}

State.java

@Entity
public class State implements Serializable {

    private Long id;
    private String abbreviatedName;
    private String name;
    private List<BusinessLocation> businessLocations;

    @Id
    @GeneratedValue 
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getAbbreviatedName() {
        return abbreviatedName;
    }
    public void setAbbreviatedName(String abbreviatedName) {
        this.abbreviatedName = abbreviatedName;
    }
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }


    @OneToMany(mappedBy="state", targetEntity=BusinessLocation.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    public List<BusinessLocation> getBusinessLocations() {
        return businessLocations;
    }
    public void setBusinessLocations(List<BusinessLocation> businessLocations) {
        this.businessLocations = businessLocations;
    }

    public boolean equals(Object obj) {
        if(! (obj instanceof State)) return false;
        State other = (State) obj;
        return new EqualsBuilder().append(abbreviatedName, other.abbreviatedName).append(name, other.name).append(businessLocations, 

other.businessLocations).isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder().append(name).append(abbreviatedName).append(businessLocations).toHashCode();
    }

}

Could someone help me out here?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安穩 2024-09-23 07:11:42

怎么样:

Query q - entityManager.createQuery("Select x from ServiceProvider x inner join x.businessLocations as y and inner join y.state as z where x.id = ?1");

What about:

Query q - entityManager.createQuery("Select x from ServiceProvider x inner join x.businessLocations as y and inner join y.state as z where x.id = ?1");
烟雨凡馨 2024-09-23 07:11:42

我有下面注释的实体,并且我将整个链保存到数据库中,但是当我尝试检索 Y(BusinessLocation)列表时,我得到...什么都没有

您应该激活 SQL 日志记录以查看发生了什么并检查数据因为注释部分看起来是正确的:

  • ServiceProviderBusinessLocation 之间的一对多是 EAGER
  • 之间的多对一BusinessLocationStateEAGER (默认情况下),

因此应该急切地检索整个链。如果情况并非如此,您可能需要检查数据和 SQL,因此提出了建议。

作为 EAGER 关联的替代方案,您可以使用 FETCH JOIN 来预取相关数据:

FROM ServiceProvider provider
  INNER JOIN FETCH provider.businessLocations location
  LEFT JOIN FETCH location.state

但请注意,JPA 1.0 不允许 JPQL 中的嵌套连接获取,这是 Hibernate 特定的。

参考资料

  • Hibernate 核心参考指南
  • JPA 1.0 规范
    • 第 9.1.22 节“ManyToOne 注释”
    • 第 4.4.5.3 节“获取连接”

I have the entities annotated below and I am having the whole chain persisted into database but when i try to retrieve the list of Y (BusinessLocation), I get... nothing

You should activate SQL logging to see what is happening and check the data because the annotation part looks correct:

  • the one-to-many between ServiceProvider and BusinessLocation is EAGER
  • the many-to-one between BusinessLocation and State is EAGER (by default)

So the whole chain should be retrieved eagerly. If this is not what is happening, you might want to check the data and the SQL, hence the suggestion.

As an alternative to EAGER associations, you could use a FETCH JOIN to prefetch the related data:

FROM ServiceProvider provider
  INNER JOIN FETCH provider.businessLocations location
  LEFT JOIN FETCH location.state

But note that JPA 1.0 does not allow nested join fetches in JPQL, this is Hibernate specific.

References

  • Hibernate Core Reference Guide
  • JPA 1.0 specification
    • Section 9.1.22 "ManyToOne Annotation"
    • Section 4.4.5.3 "Fetch Joins"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文