Sprig错误:使用@OneToMany或@ManyToMany定位到一个未映射的类

huangapple go评论73阅读模式
英文:

Sprig Error : Use of @OneToMany or @ManyToMany targeting an unmapped class

问题

以下是翻译的代码部分:

QuoteDetailsDTO.java:

import javax.persistence.*;

@Entity(name = "quoteDetailsDTO")
@Table(name = "QUOTE_DETAILS")
public class QuoteDetailsDTO {
    @Id
    @Column(name = "QUOTE_ID", length = 36)
    private String quoteId;

    // ... (其他属性的定义和 getter/setter 方法)

    @ManyToOne(fetch = FetchType.LAZY, targetEntity = ClientInfoDTO.class)
    @JoinColumn(name = "CLIENT_ID", nullable = false)
    private ClientInfoDTO clientInfoDTO;

    public ClientInfoDTO getClientInfoDTO() {
        return clientInfoDTO;
    }

    public void setClientInfoDTO(ClientInfoDTO clientInfoDTO) {
        this.clientInfoDTO = clientInfoDTO;
    }

    // ... (其他方法)
}

ClientInfoDTO.java:

import javax.persistence.*;
import java.util.List;

@Entity(name = "clientInfoDTO")
@Table(name = "CLIENT_INFO")
public class ClientInfoDTO {
    @Id
    @Column(name = "CLIENT_ID", length = 36)
    private String clientId;

    // ... (其他属性的定义和 getter/setter 方法)

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "clientInfoDTO", cascade = CascadeType.ALL)
    private List<QuoteDetails> quoteDetails;

    public List<QuoteDetails> getQuoteDetails() {
        return quoteDetails;
    }

    public void setQuoteDetails(List<QuoteDetails> quoteDetails) {
        this.quoteDetails = quoteDetails;
    }

    // ... (其他方法)
}

ApplicationConfiguration.java:

@Bean
public LocalSessionFactoryBean sessionFactory(DataSource dataSource, Properties hibernateProperties) {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

    sessionFactory.setDataSource(dataSource);
    sessionFactory.setPackagesToScan("xxx.xxx.xxx.xxx.xxx.model");
    sessionFactory.setHibernateProperties(hibernateProperties);

    return sessionFactory;
}

@Bean
public DataSource dataSource(ConfigurationClient configurationClient) {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setDriverClassName(env.getProperty("spring.datasource.driverClassName"));
    dataSource.setUrl(env.getProperty("spring.datasource.url"));
    dataSource.setUsername(env.getProperty("spring.datasource.username"));

    return dataSource;
}

@Bean(name = "transactionManager")
public HibernateTransactionManager hibernateTransactionManager(LocalSessionFactoryBean sessionFactory, DataSource dataSource) {
    HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();

    hibernateTransactionManager.setSessionFactory(sessionFactory.getObject());
    hibernateTransactionManager.setDataSource(dataSource);

    return hibernateTransactionManager;
}

@Bean
public Properties hibernateProperties(ConfigurationClient configurationClient) {
    Properties properties = new Properties();

    properties.put("hibernate.dialect", env.getProperty("spring.jpa.database-platform"));
    properties.put("hibernate.show_sql", true);
    properties.put("hibernate.format_sql", true);
    properties.put("hibernate.hbm2ddl.auto", "create");

    return properties;
}

@Bean
public TransactionTemplate transactionTemplate(HibernateTransactionManager hibernateTransactionManager) {
    TransactionTemplate transactionTemplate = new TransactionTemplate();

    transactionTemplate.setTransactionManager(hibernateTransactionManager);
    transactionTemplate.setTimeout(30);

    return transactionTemplate;
}

请注意,这些是您提供的代码的翻译版本,其中包括实体类以及应用程序配置。如果您还有其他问题或需要进一步的帮助,请随时提问。

英文:

I'm working on a small Spring project, I'm trying to join two objects but I keep getting an org.hibernate.AnnotationException error when I try to start up the project, I've mapped both entities too. It works well when I don't add the joins but bombs out when I do.

My one DTO looks like this:

import javax.persistence.*;
@Entity(name = &quot;quoteDetailsDTO&quot;)
@Table(name = &quot;QUOTE_DETAILS&quot;)
public class QuoteDetailsDTO {
@Id
@Column(name = &quot;QUOTE_ID&quot;, length = 36)
private String quoteId;
@Column(name = &quot;LEAD_ID&quot;)
private String leadId;
@Column(name = &quot;ADULT_DEPENDENTS&quot;)
private String adultDependents;
@Column(name = &quot;CHILD_DEPENDENTS&quot;)
private String childDependents;
@Column(name = &quot;QUOTE_TCS_ACCEPTED&quot;)
private String quoteTCsAccepted;
@Column(name = &quot;PRODUCT_TCS_ACCEPTED&quot;)
private String productTCsAccepted;
@Column(name = &quot;LJP_APPLICABLE&quot;)
private String LjpApplicable;
@Column(name = &quot;QUOTE_DATE&quot;)
private String quoteDate;
@Column(name = &quot;NOTIFICATION_ID&quot;)
private String notificationId;
@Column(name = &quot;QUOTE_PDF_REF&quot;)
private String quotePDFRef;
@Column(name = &quot;QUOTE_CSV_REF&quot;)
private String quoteCSVRef;
@Column(name = &quot;AGENT_ENTITY&quot;)
private String agentEntity;
@ManyToOne(fetch = FetchType.LAZY, targetEntity = ClientInfoDTO.class)
@JoinColumn(name = &quot;CLIENT_ID&quot;, nullable = false)
private ClientInfoDTO clientInfoDTO;
public ClientInfoDTO getClientInfoDTO() {
return clientInfoDTO;
}
public void setClientInfoDTO(ClientInfoDTO clientInfoDTO) {
this.clientInfoDTO = clientInfoDTO;
}
public String getQuoteId() {
return quoteId;
}
public void setQuoteId(String quoteId) {
this.quoteId = quoteId;
}
public String getLeadId() {
return leadId;
}
public void setLeadId(String leadId) {
this.leadId = leadId;
}
public String getAdultDependents() {
return adultDependents;
}
public void setAdultDependents(String adultDependents) {
this.adultDependents = adultDependents;
}
public String getChildDependents() {
return childDependents;
}
public void setChildDependents(String childDependents) {
this.childDependents = childDependents;
}
public String getQuoteTCsAccepted() {
return quoteTCsAccepted;
}
public void setQuoteTCsAccepted(String quoteTCsAccepted) {
this.quoteTCsAccepted = quoteTCsAccepted;
}
public String getProductTCsAccepted() {
return productTCsAccepted;
}
public void setProductTCsAccepted(String productTCsAccepted) {
this.productTCsAccepted = productTCsAccepted;
}
public String getLjpApplicable() {
return LjpApplicable;
}
public void setLjpApplicable(String ljpApplicable) {
LjpApplicable = ljpApplicable;
}
public String getQuoteDate() {
return quoteDate;
}
public void setQuoteDate(String quoteDate) {
this.quoteDate = quoteDate;
}
public String getNotificationId() {
return notificationId;
}
public void setNotificationId(String notificationId) {
this.notificationId = notificationId;
}
public String getQuotePDFRef() {
return quotePDFRef;
}
public void setQuotePDFRef(String quotePDFRef) {
this.quotePDFRef = quotePDFRef;
}
public String getQuoteCSVRef() {
return quoteCSVRef;
}
public void setQuoteCSVRef(String quoteCSVRef) {
this.quoteCSVRef = quoteCSVRef;
}
public String getAgentEntity() {
return agentEntity;
}
public void setAgentEntity(String agentEntity) {
this.agentEntity = agentEntity;
}
}

And the other like this:

import javax.persistence.*;
import java.util.List;
@Entity(name = &quot;clientInfoDTO&quot;)
@Table(name = &quot;CLIENT_INFO&quot;)
public class ClientInfoDTO {
@Id
@Column(name = &quot;CLIENT_ID&quot;, length = 36)
private String clientId;
@Column(name = &quot;EMAIL_ADDRESS&quot;)
private String emailAddress;
@Column(name = &quot;CONTACT_NUMBER&quot;)
private String contactNumber;
@Column(name = &quot;FIRST_NAME&quot;)
private String firstName;
@Column(name = &quot;LAST_NAME&quot;)
private String lastName;
@Column(name = &quot;ID_TYPE&quot;)
private String idType;
@Column(name = &quot;ID_NUMBER&quot;)
private String idNumber;
@Column(name = &quot;ENTITY_NUMBER&quot;)
private String entityNumber;
@OneToMany(fetch = FetchType.LAZY, mappedBy = &quot;clientInfoDTO&quot;, cascade = CascadeType.ALL)
private List&lt;QuoteDetails&gt; quoteDetails;
public List&lt;QuoteDetails&gt; getQuoteDetails() {
return quoteDetails;
}
public void setQuoteDetails(List&lt;QuoteDetails&gt; quoteDetails) {
this.quoteDetails = quoteDetails;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getIdType() {
return idType;
}
public void setIdType(String idType) {
this.idType = idType;
}
public String getIdNumber() {
return idNumber;
}
public void setIdNumber(String idNumber) {
this.idNumber = idNumber;
}
public String getEntityNumber() {
return entityNumber;
}
public void setEntityNumber(String entityNumber) {
this.entityNumber = entityNumber;
}
}

With the ApplicationConfiguration set up like this:

 @Bean
public LocalSessionFactoryBean sessionFactory(DataSource dataSource, Properties hibernateProperties) {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setPackagesToScan(&quot;xxx.xxx.xxx.xxx.xxx.model&quot;);
sessionFactory.setHibernateProperties(hibernateProperties);
return sessionFactory;
}
@Bean
public DataSource dataSource(ConfigurationClient configurationClient) {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty(&quot;spring.datasource.driverClassName&quot;));
dataSource.setUrl(env.getProperty(&quot;spring.datasource.url&quot;));
dataSource.setUsername(env.getProperty(&quot;spring.datasource.username&quot;));
return dataSource;
}
@Bean(name = &quot;transactionManager&quot;)
public HibernateTransactionManager hibernateTransactionManager(LocalSessionFactoryBean sessionFactory, DataSource dataSource) {
HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
hibernateTransactionManager.setSessionFactory(sessionFactory.getObject());
hibernateTransactionManager.setDataSource(dataSource);
return hibernateTransactionManager;
}
@Bean
public Properties hibernateProperties(ConfigurationClient configurationClient) {
Properties properties = new Properties();
properties.put(&quot;hibernate.dialect&quot;, env.getProperty(&quot;spring.jpa.database-platform&quot;));
properties.put(&quot;hibernate.show_sql&quot;, true);
properties.put(&quot;hibernate.format_sql&quot;, true);
properties.put(&quot;hibernate.hbm2ddl.auto&quot;, &quot;create&quot;);
return properties;
}
@Bean
public TransactionTemplate transactionTemplate(HibernateTransactionManager hibernateTransactionManager) {
TransactionTemplate transactionTemplate = new TransactionTemplate();
transactionTemplate.setTransactionManager(hibernateTransactionManager);
transactionTemplate.setTimeout(30);
return transactionTemplate;
}

Update:

My Tables look like this (I'm using a h2 database):

create table CLIENT_INFO (
CLIENT_ID varchar(36) not null,
CONTACT_NUMBER varchar(255),
EMAIL_ADDRESS varchar(255),
ENTITY_NUMBER varchar(255),
FIRST_NAME varchar(255),
ID_NUMBER varchar(255),
ID_TYPE varchar(255),
LAST_NAME varchar(255),
primary key (CLIENT_ID)
);
create table QUOTE_DETAILS (
QUOTE_ID varchar(36) not null,
LJP_APPLICABLE varchar(255),
ADULT_DEPENDENTS varchar(255),
AGENT_ENTITY varchar(255),
CHILD_DEPENDENTS varchar(255),
CLIENT_ID varchar(255),
LEAD_ID varchar(255),
NOTIFICATION_ID varchar(255),
PRODUCT_TCS_ACCEPTED varchar(255),
QUOTE_CSV_REF varchar(255),
QUOTE_DATE varchar(255),
QUOTE_PDF_REF varchar(255),
QUOTE_TCS_ACCEPTED varchar(255),
primary key (QUOTE_ID)
)

The org.hibernate.AnnotationException stack trace:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;sessionFactory&#39; defined in class path resource [xxx/xxx/xxx/xxx/xxx/configuration/ApplicationConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: xxx.xxx.xxx.xxx.xxx.model.ClientInfoDTO.quoteDetails[xxx.xxx.xxx.xxx.xxx.model.QuoteDetails]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1708)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:581)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:818)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:724)
... 45 more
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: xxx.xxx.xxx.xxx.xxx.model.ClientInfoDTO.quoteDetails[xxx.xxx.xxx.xxx.xxx.model.QuoteDetails]
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1243)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:800)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:725)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:535)
at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1767)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1704)
... 56 more

答案1

得分: 1

这是准确的代码吗?

从错误信息以及您的类中观察,特别是这部分:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "clientInfoDTO", cascade = CascadeType.ALL)
private List<QuoteDetails> quoteDetails;

您在使用类名为QuoteDetails,而您的实体类名应为QuoteDetailsDTO

英文:

Is this the exact code?

Looking at the error and then to your classes, specifically this part:

@OneToMany(fetch = FetchType.LAZY, mappedBy = &quot;clientInfoDTO&quot;, cascade = CascadeType.ALL)
private List&lt;QuoteDetails&gt; quoteDetails;

You are using class QuoteDetails when your entity class is named QuoteDetailsDTO?

huangapple
  • 本文由 发表于 2020年5月4日 04:02:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/61580871.html
匿名

发表评论

匿名网友

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

确定