Spring Boot JPQL、嵌套属性和SQLGrammarException

cxfofazt  于 2023-03-08  发布在  Spring
关注(0)|答案(1)|浏览(143)

我有以下JPA实体:

@MappedSuperclass
@Data
public class AbstractBaseEntity {

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

    private String refId;

}

@Entity
@Table(name = "addresses")
@AttributeOverrides({
    @AttributeOverride(name = "id", column = @Column(name = "address_id")),
    @AttributeOverride(name = "refId", column = @Column(name = "address_ref_id"))
})
@Data
public class AddressEntity extends AbstractBaseEntity {

    @Column(name = "address_line_1")
    private String line1;

    @Column(name = "address_line_2")
    private String line2;

    @Column(name = "address_line_3")
    private String line3;

    @Column(name = "address_city")
    private String city;

    @Column(name = "address_state_province_abbrev")
    private String stateProvinceAbbrev;

    @Column(name = "address_postal_code")
    private String postalCode;

    @Column(name = "address_country_code")
    private String countryCode;

}

@Entity
@Table(name = "organizations")
@AttributeOverrides({
    @AttributeOverride(name = "id", column = @Column(name = "organization_id")),
    @AttributeOverride(name = "refId", column = @Column(name = "organization_ref_id"))
})
@DiscriminatorColumn(name = "type")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Data
public abstract class AbstractOrganizationEntity extends AbstractBaseEntity {

    @Column(name = "organization_name")
    private String name;

    @Column(name = "organization_website")
    private String website;

    @Column(name = "organization_main_phone")
    private String mainPhone;

    @OneToOne
    @JoinColumn(name = "organization_hq_address_id")
    private AddressEntity hqAddress;

}

@Entity
@Data
public class VendorEntity extends AbstractOrganizationEntity {
    @Column(name="vendor_specialty")
    private String specialty;
}

我正在尝试实现(使用JPQL)和VendorRepository方法,该方法基于VendorEntity的namehqAddressstateProvinceAbbrev查找VendorEntity。

@Repository
public interface VendorRepository extends JpaRepository<VendorEntity,Long> {

    Optional<VendorEntity> findByNameAndHqAddressStateProvinceAbbrev(String name, String stateProvinceAbbrev);

}

...在使用有效参数执行时生成SQLGrammarException

org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

有人能指出我哪里偏离了JPQL惯例吗?

dbf7pr2w

dbf7pr2w1#

VendorEntity类是一个抽象类,因此不能直接在JPQL查询中使用它。相反,需要使用扩展VendorEntity的具体子类。例如,如果有一个名为ConcreteVendorEntity的子类,则可以按如下所示修改存储库方法:

@Repository
public interface VendorRepository extends JpaRepository<VendorEntity, Long> {

@Query("SELECT v FROM ConcreteVendorEntity v WHERE v.name = :name AND 
v.hqAddress.stateProvinceAbbrev = :stateProvinceAbbrev")
Optional<ConcreteVendorEntity> 
findByNameAndHqAddressStateProvinceAbbrev(@Param("name") String name, 
@Param("stateProvinceAbbrev") String stateProvinceAbbrev);

}

注意,需要使用@Query注解指定JPQL查询,还需要指定查询中的具体实体类名(ConcreteVendorEntity),还可以使用@Param注解指定查询参数。
此外,请确保已导入必要的注解:

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

相关问题