有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate:无法获取检索到的关联列表

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

X和Y之间的关系是一对一,Y和Z之间的关系是多对一

我想检索X,并将所有相关实体与它们一起检索

我应该编写什么HQL查询以便一次检索整个链。目前它的休眠模式。查找(“从X”)。 或者我用什么注释

X=服务提供商,Y=业务地点。java,Z=State。爪哇

我在下面注释了实体,并将整个链持久化到数据库中,但当我尝试检索Y(BusinessLocation)列表时,我得到了

没什么

我怎么把X和Y,Y和Z连接起来

下面是实体x、Y和Z

服务提供商。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();
    }
}

商务地点。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();
    }
}

状态。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();
    }

}

有人能帮我吗

谢谢


共 (2) 个答案

  1. # 1 楼答案

    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

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

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

    因此,整个链条都应该被急切地收回。如果情况并非如此,您可能希望检查数据和SQL,因此建议

    作为即时关联的替代方法,您可以使用FETCH JOIN预取相关数据:

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

    但是请注意,JPA1.0不允许JPQL中使用nested join fetches,这是特定于Hibernate的

    参考文献

  2. # 2 楼答案

    那么:

    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");