春天JPA,@SqlResultSetMapping映射到JPA库
问题描述:
应用:春季启动春天JPA,@SqlResultSetMapping映射到JPA库
我试图映射非实体类的JPA存储库@SqlResultSetMapping
和@NamedNativeQuery
。
我正在运行应用程序时出现以下错误:
不满意依赖通过现场“cityAddressRepository”表示;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'cityAddressRepository'的Bean时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:不是托管类型:class com.city.strom.core.domain.CityAddressEntity at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) 〜[弹簧豆-5.0.0.RC1.jar:5.0.0.RC1]
任何帮助将appeciated,是什么东西错在我的实现
CityAddress.java
public class CityAddress {
private static final long serialVersionUID = -4860426805452667396L;
private Integer objectid;
private String streetname;
private String streetnumber;
public Integer getObjectid() {
return objectid;
}
public void setObjectid(Integer objectid) {
this.objectid = objectid;
}
public String getStreetname() {
return streetname;
}
public void setStreetname(String streetname) {
this.streetname = streetname;
}
public String getStreetnumber() {
return streetnumber;
}
public void setStreetnumber(String streetnumber) {
this.streetnumber = streetnumber;
}
}
CityAddressEntity.java
@Entity
@SqlResultSetMapping(
name="cityaddressmap",
entities={
@EntityResult(
entityClass=CityAddress.class,
fields={
@FieldResult(name="objectid", column="ID"),
@FieldResult(name="streetnumber", column="STREETNUMBER"),
@FieldResult(name="streetname", column="STREETNAME")
}
)
}
)
@NamedNativeQuery(
name="CityAddressEntity.AllCityAddresses",
query="select OBJECTID, ADD_NUM STREETNUMBER, trim(ADD_STREET || ' ' || ADD_STREET_TYPE) STREETNAME from SWPF_PARCEL_V;",
resultSetMapping="cityaddressmap")
public class CityAddressEntity implements Serializable{
private static final long serialVersionUID = -4860426805452667396L;
public CityAddressEntity()
{
}
}
CityAddressRepository.java
public interface CityAddressRepository extends JpaRepository<CityAddressEntity, Long>{
@Query(name="CityAddressEntity.AllCityAddresses")
public List<CityAddress> AllCityAddresses();
}
答
CityAddress不是实体,使用targetClass代替entityClass。尝试类似的东西:
@SqlResultSetMapping(
name="cityaddressmap",
classes={
@ConstructorResult(
targetClass=CityAddress.class,
columns={
@FieldResult(name="ID", type=Integer.class),
@FieldResult(name="STREETNUMBER", type=String.class),
@FieldResult(name="STREETNAME", type=String.class)
}
)
}
)
,并添加构造函数CityAddress
public class CityAddress {
private Integer objectid;
private String streetname;
private String streetnumber;
public CityAddress(Integer objectid, String streetname, String streetnumber) {
this.objectid = objectid;
this.streetname = streetname;
this.streetnumber = streetnumber;
}
被一些人可以在此帮助吗? – user8796936