英文:
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 = "quoteDetailsDTO")
@Table(name = "QUOTE_DETAILS")
public class QuoteDetailsDTO {
@Id
@Column(name = "QUOTE_ID", length = 36)
private String quoteId;
@Column(name = "LEAD_ID")
private String leadId;
@Column(name = "ADULT_DEPENDENTS")
private String adultDependents;
@Column(name = "CHILD_DEPENDENTS")
private String childDependents;
@Column(name = "QUOTE_TCS_ACCEPTED")
private String quoteTCsAccepted;
@Column(name = "PRODUCT_TCS_ACCEPTED")
private String productTCsAccepted;
@Column(name = "LJP_APPLICABLE")
private String LjpApplicable;
@Column(name = "QUOTE_DATE")
private String quoteDate;
@Column(name = "NOTIFICATION_ID")
private String notificationId;
@Column(name = "QUOTE_PDF_REF")
private String quotePDFRef;
@Column(name = "QUOTE_CSV_REF")
private String quoteCSVRef;
@Column(name = "AGENT_ENTITY")
private String agentEntity;
@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;
}
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 = "clientInfoDTO")
@Table(name = "CLIENT_INFO")
public class ClientInfoDTO {
@Id
@Column(name = "CLIENT_ID", length = 36)
private String clientId;
@Column(name = "EMAIL_ADDRESS")
private String emailAddress;
@Column(name = "CONTACT_NUMBER")
private String contactNumber;
@Column(name = "FIRST_NAME")
private String firstName;
@Column(name = "LAST_NAME")
private String lastName;
@Column(name = "ID_TYPE")
private String idType;
@Column(name = "ID_NUMBER")
private String idNumber;
@Column(name = "ENTITY_NUMBER")
private String entityNumber;
@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;
}
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("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;
}
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 'sessionFactory' 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 = "clientInfoDTO", cascade = CascadeType.ALL)
private List<QuoteDetails> quoteDetails;
You are using class QuoteDetails when your entity class is named QuoteDetailsDTO?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论