Java Spring Jpa嵌入式实体PropertyNotFoundException无法找到字段名问题。

huangapple 未分类评论48阅读模式
英文:

Java Spring Jpa Embedded Entity PropertyNotFoundException Could not locate field name Issue

问题

I am trying to build a multiple table query with JPA using the following information:

Cust Srch Entity

// ... (entity class definition)

Cust Icv Xref Entity

// ... (entity class definition)

The class I am trying to use as the combination of both of these with the query is: Customer Search

// ... (entity class definition)

For reference:
Application

// ... (application class definition)

Repository:

// ... (repository interface definition)

The stack trace of the error on startup is:

// ... (error stack trace)

I am thinking it might have something to do with how I am embedding the entity classes. The ultimate goal is to create a multiple table query with JPA and get the results back that I can act on.

英文:

I am trying to build a multiple table query with JPA using the following information:

Cust Srch Entity

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
//@NamedQueries({
//        @NamedQuery(
//                name = "CustSearchDto.retrieveCustomerByTaxPayerId",
//                query = "SELECT cs FROM CustSrchDto cs WHERE cs.taxPayerId = ?1 and cs.customerLifecycleCode = ?2 and cs.customerTypeCode = ?3"
//        )
//})
@Entity(name = "CustSrchDto")
@Table(name = "CUST_SRCH",
        schema = "SAMS"
)
@Embeddable
public class CustSrchDto implements Serializable {

    private static final long serialVersionUID = 5572174433231867682L;

    @Id
    @Column(name = "SRCH_NM")
    private String searchName;

    @Id
    @Column(name = "INDIV_FIRST_NM")
    private String individualFirstName;

    @Id
    @Column(name = "INDIV_MIDL_NM")
    private String individualMiddleName;

    @Id
    @Column(name = "CUST_ID")
    private String customerId;

    @Column(name = "TXPYR_ID")
    private String taxPayerId;

    @Column(name = "SRCH_POSTAL_CD")
    private String searchPostalCode;

    @Column(name = "SWB_ORG_CD")
    private String schwabOrgCode;

    @Column(name = "PHON_AREA_ID")
    private String phoneAreaId;

    @Column(name = "PHON_PRFX_ID")
    private String phonePrefixId;

    @Column(name = "PHON_LINE_ID")
    private String phoneLineId;

    @Column(name = "CUST_LFCYC_CD")
    private String customerLifecycleCode;

    @Column(name = "CUST_TYPE_CD")
    private String customerTypeCode;

}

Note: That the commented out named query was used to make sure I got everything working for just the one table. And when I used that as the query on the single table it works.

Cust Icv Xref Entity

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity(name = "CustIcvXrefDto")
@Table(name = "CUST_ICV_XREF",
        schema = "SAMS"
)
@Embeddable
public class CustIcvXrefDto implements Serializable {

    private static final long serialVersionUID = -8276309395441323238L;

    @Id
    @Column(name = "CUST_ID1")
    private String customerId;

    @Id
    @Column(name = "PROFL_ID")
    private String profileId;

    @Id
    @Column(name = "PARTY_ID")
    private String partyId;

    @Column(name = "SYS_OF_REC_IND")
    private String systemOfRecordIndividual;

    @Column(name = "MIGRTN_STAT")
    private String marginStatus;

    @Column(name = "AUDIT_UPDT_USER_ID")
    private String auditUpdateUserId;

    @Column(name = "AUDIT_UPDT_TS")
    private String auditUpdateTimeStamp;

}

The class I am trying to use as the combination of both of these with the query is:
Customer Search

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@NamedQueries({
        @NamedQuery(
                name = "CustomerSearchDto.retrieveCustomerByTaxPayerId",
                query = "select xr, cs from CustSrchDto cs , CustIcvXrefDto xr where cs.customerId = xr.customerId and cs.taxPayerId = ?1 and cs.customerLifecycleCode = ?2 and cs.customerTypeCode = ?3"
        )
})
@Entity(name = "CustomerSearchDto")
@Table(name = "CUST_SRCH",
        schema = "SAMS"
)
public class CustomerSearchDto implements Serializable {

    private static final long serialVersionUID = 4832171797332928024L;

    @Id
    @GeneratedValue
    private Integer id;

    @Embedded
    private CustSrchDto custSrchDto;

    @Embedded
    private CustIcvXrefDto custIcvXrefDto;


}

For reference:
Application

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
public class BrokeragePartyOnboradingApplication {

    public static void main(String[] args) {
        SpringApplication.run(BrokeragePartyOnboradingApplication.class, args);
    }

}

Repository:

import com.schwab.brokerage.party.onborading.api.outbound.db2.models.CustomerSearchDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CustSearchConnector extends JpaRepository<CustomerSearchDto, Integer> {

    List<CustomerSearchDto> retrieveCustomerByTaxPayerId(String taxPayerId,
                                                         String customerLifecycleCode,
                                                         String customerTypeCode);

}

The stack trace of the error on startup is:

2020-05-04 12:21:40.419  WARN 9952 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/schwab/cat/dynamicrouting/config/DataSourceAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.PropertyNotFoundException: Could not locate field name [customerId] on class [com.schwab.brokerage.party.onborading.api.outbound.db2.models.CustomerSearchDto]
2020-05-04 12:21:40.422  INFO 9952 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-05-04 12:21:40.438  INFO 9952 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-04 12:21:40.452 ERROR 9952 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [com/schwab/cat/dynamicrouting/config/DataSourceAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.PropertyNotFoundException: Could not locate field name [customerId] on class [com.schwab.brokerage.party.onborading.api.outbound.db2.models.CustomerSearchDto]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
	at com.schwab.brokerage.party.onborading.BrokeragePartyOnboradingApplication.main(BrokeragePartyOnboradingApplication.java:14) [main/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.PropertyNotFoundException: Could not locate field name [customerId] on class [com.schwab.brokerage.party.onborading.api.outbound.db2.models.CustomerSearchDto]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:403) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	... 16 common frames omitted
Caused by: org.hibernate.PropertyNotFoundException: Could not locate field name [customerId] on class [com.schwab.brokerage.party.onborading.api.outbound.db2.models.CustomerSearchDto]
	at org.hibernate.internal.util.ReflectHelper.findField(ReflectHelper.java:371) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.property.access.internal.PropertyAccessFieldImpl.<init>(PropertyAccessFieldImpl.java:34) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.property.access.internal.PropertyAccessStrategyFieldImpl.buildPropertyAccess(PropertyAccessStrategyFieldImpl.java:26) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:330) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.tuple.PropertyFactory.buildIdentifierAttribute(PropertyFactory.java:64) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:136) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:601) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:125) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_251]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_251]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_251]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_251]
	at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:181) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:299) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:468) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1237) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
	at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	... 20 common frames omitted

I am thinking it might have something to do with how I am embedding the entity classes. The ultimate goal is to create a multiple table query with JPA and get the results back that I can act on.

答案1

得分: 0

我认为问题在于在一个 Embeddable(被注解为 @Embeddable 的类,它们不是实体)中不能有一个带有 @Id 注解的字段。

谢谢

英文:

I think problem is that you cannot have an @Id annotated field in an Embeddable (@Embeddable annotated classes, which are not entiities).

Thanks

huangapple
  • 本文由 发表于 2020年5月5日 01:33:34
  • 转载请务必保留本文链接:https://java.coder-hub.com/61598226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定