具有多个表的 SpringBoot JPA 本机查询将数据绑定连接到投影

问题描述

@Entity
@Table(name="property")
@NamedQuery(name="Property.findAll",query="SELECT p FROM Property p")
public class Property implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator = "property_seq")
    @SequenceGenerator(name = "property_seq",allocationSize = 1,sequenceName = "s_property")
    @Column(name="property_id",unique=true,nullable=false,precision=10)
    private long propertyId;

    @Column(length=4000)
    private String description;

    //bi-directional many-to-one association to City
    @ManyToOne
    @JoinColumn(name="city_id")
    private City city;

    //bi-directional many-to-one association to PropertyAmenity
    @OnetoMany(mappedBy="property",cascade = CascadeType.ALL)
    private List<PropertyAmenity> propertyAmenities;

@Entity
@Table(name="city")
@NamedQuery(name="City.findAll",query="SELECT c FROM City c")
public class City implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="city_id",precision=10)
    private long cityId;

    @Column(name="city_name",length=50)
    private String cityName;
    @JsonIgnore
    @Column(name="last_updated_by",length=50)
    private String lastUpdatedBy;
    @JsonIgnore
    @CreationTimestamp
    @Column(name="last_updated_date")
    private Timestamp lastUpdatedDate;

    //bi-directional many-to-one association to State
    @ManyToOne
    @JoinColumn(name="state_id",nullable=false)
    private State state;

@Entity
@Table(name="state")
@NamedQuery(name="State.findAll",query="SELECT s FROM State s")
public class State implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="state_id",precision=10)
    private long stateId;

    @Column(name="country_name",length=50)
    private String countryName;
    @JsonIgnore
    @Column(name="last_updated_by",length=50)
    private String lastUpdatedBy;
    
    @CreationTimestamp
    @JsonIgnore
    @Column(name="last_updated_date")
    private Timestamp lastUpdatedDate;

    @Column(name="state_name",length=50)
    private String stateName;

@Entity
@Table(name="property_amenities")
@NamedQuery(name="PropertyAmenity.findAll",query="SELECT p FROM PropertyAmenity p")
public class PropertyAmenity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator = "property_amenity")
    @SequenceGenerator(name = "property_amenity",sequenceName = "s_property_amenities")
    @Column(name="property_amenities_id",precision=10)
    private long propertyAmenitiesId;

    @Column(name="avilable_flag",nullable=false)
    private Boolean avilableFlag;

    @Column(name="last_updated_by",length=50)
    private String lastUpdatedBy;

    @Column(name="last_updated_date")
    private Timestamp lastUpdatedDate;

    @Column(name="master_amenity_type",length=10)
    private String masteramenityType;

    //bi-directional many-to-one association to Property
    @ManyToOne
    @JoinColumn(name="property_id")
    private Property property;

}

public interface PropertyProjection {

        String getDescription() ;
        CityProjection getCity();
        List<PropertyAmenityProjection> getPropertyAmenities(); 
        
    public interface CityProjection {

            String getCityName() ;
            StateProjection getState() ;
    }
    
    public interface StateProjection {

        String getCountryName() ;
        String getStateName() ;
    }
            

    public interface PropertyAmenityProjection {
        String getMasteramenityType() ;
        String getAvilableFlag() ;
    }   

}


@Repository
public interface PropertySearchRepository  extends JpaRepository<Property,Long> {

    
    @Query(value = **"select p.description,c.city_name,s.country_name,s.state_name,pa.avilable_flag,pa.master_amenity_type from property p,city c,state s,property_amenities pa where p.city_id=c.city_id "
            + "and c.state_id=s.state_id and p.property_id = pa.property_id"**,nativeQuery = true)
     public List<Property> getAllProperties();

}

在我们的项目中,需要从 Property、City、State 和 Property Amenities 表中获取数据。为此,我编写了上面提到的本机 sql 查询 Respositary。此查询数据需要绑定到 PropertyProjection 对象中。绑定数据时,服务器日志中出现结果集错误。能否请您告诉我,如何绑定上述复杂投影的数据。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)