Hibernate模式验证:表x中缺少列[q]

时间:2019-05-12 12:38:50

标签: hibernate spring-boot spring-data-jpa spring-repositories

嗨,我正在尝试从休眠状态的mysql上执行本机sql查询。 下面的原始sql可以在终端上正常工作

size = 1000
df = pd.DataFrame({i: np.random.randint(1,100,size=size) for i in ['metric']})
df['label'] =  np.random.randint(0,2, size=size)
df['group_1'] =  pd.Series(np.random.randint(1,12, size=size)).astype(object)
df['group_2'] =  pd.Series(np.random.randint(1,10, size=size)).astype(object)

group_0 = df[df['label'] == 0]
group_0 = group_0.reset_index(drop=True)
join_columns_enrich = ['group_1', 'group_2']
join_real = ['metric']
join_real.extend(join_columns_enrich)
group_0 = group_0[join_real]
display(group_0.head())
group_1 = df[df['label'] == 1]
group_1 = group_1.reset_index(drop=True)
display(group_1.head())
df = group_1.merge(group_0, on=join_columns_enrich)
display(df.head())
print(group_1.shape)
df.shape

但是当我将其与存储库一起使用时,我遇到了异常

select (6371 * ACOS(COS(RADIANS(my_latitude)) * COS(RADIANS(a.latitude)) * COS(RADIANS(a.longitude) - RADIANS(my_longitude)) + SIN(RADIANS(my_latitude)) * SIN(RADIANS(a.latitude)))) AS distance FROM address a HAVING distance < 5 ORDER BY distance

我的地址实体如下:

org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [q] in table [address]

我的NearByDto如下:

@Entity
@Table(name = "address")
public class Address extends Auditable<String> {


    @Getter
    @Setter
    private Double latitude;

    @Setter
    @Getter
    private Double longitude;

    @Setter
    @Getter
    private String address;

    @Getter
    @Setter
    @ManyToOne
    @JoinColumn(name = "user_id")
    @JsonManagedReference
    private User user;
}

存储库如下:

public interface NearByDto {
    Long getId();
    String getAddress();
    Double getDistance();
}

可审核

@Repository
public interface AddressRepository extends JpaRepository<Address, Long> {

    @Query(value = "select (6371 * ACOS(COS(RADIANS(my_latitude)) * COS(RADIANS(a.latitude)) * COS(RADIANS(a.longitude) - RADIANS(my_longitude)) + SIN(RADIANS(my_latitude)) * SIN(RADIANS(a.latitude)))) AS distance FROM address a HAVING distance < 5 ORDER BY distance", nativeQuery = true)
    List<NearByDto> findNearby();

}

用户类别

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor
public abstract class Auditable<U> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Getter
    @Setter
    private Long id;


    @CreatedBy
    @Getter
    @Setter
    @JsonIgnore
    protected U createdBy;

    @Getter
    @Setter
    @CreatedDate
    @Temporal(TIMESTAMP)
    @JsonIgnore
    protected Date creationDate;

    @Getter
    @Setter
    @LastModifiedBy
    @JsonIgnore
    protected U lastModifiedBy;


    @Getter
    @Setter
    @LastModifiedDate
    @Temporal(TIMESTAMP)
    @JsonIgnore
    protected Date lastModifiedDate;

    @Getter
    @Setter
    @JsonIgnore
    protected Boolean isActive;

}

*供参考

my_latitude =查询字符串中的纬度

my_longitude =查询字符串中的hardcoded_longitude

1 个答案:

答案 0 :(得分:0)

最后,在学习了基础知识之后,我开始使用它了。

我的域模型中具有字段名称String q =“ @ Query(... long ... sql)”。 hibernate:ddl-auto: validate已通过验证,我没有编写脚本来添加这个新创建的字段,这是我的错,这是正确的休眠信息。

我更新后的存储库如下,其中包括动态纬度,经度和半径:

@Repository
public interface AddressRepository extends JpaRepository<Address, Long> {

    @Query(value = "select a.id, a.address, (6371 * ACOS(COS(RADIANS(:latitude)) * COS(RADIANS(a.latitude)) * COS(RADIANS(a.longitude) - RADIANS(:longitude)) + SIN(RADIANS(:latitude)) * SIN(RADIANS(a.latitude)))) AS distance FROM address a HAVING distance < :radius ORDER BY distance", nativeQuery = true)
    List<NearByDto> findNearbyGangs(Double latitude, Double longitude, Double radius);

}

感谢@dina指导我逐步进行操作,因为我丢失了许多文件中所做的更改。