我有以下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的name
和hqAddress
的stateProvinceAbbrev
查找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惯例吗?
1条答案
按热度按时间dbf7pr2w1#
VendorEntity类是一个抽象类,因此不能直接在JPQL查询中使用它。相反,需要使用扩展VendorEntity的具体子类。例如,如果有一个名为ConcreteVendorEntity的子类,则可以按如下所示修改存储库方法:
注意,需要使用@Query注解指定JPQL查询,还需要指定查询中的具体实体类名(ConcreteVendorEntity),还可以使用@Param注解指定查询参数。
此外,请确保已导入必要的注解: