我有SIMProvisionedRange表,它有一个复合键和唯一键, M2M_SIM_Provisioned_Range_Accounts用于映射SIMProvisionedRange和Accounts表。 我在hibernate映射中遇到了一对多单向的问题。
@Entity(name = "M2M_SIM_Provisioned_Range")
@Table
public class SIMProvisionedRange {
@Column(unique=true)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer SIMProvisionedRangeId;
@OneToMany
@JoinTable(name = "M2M_SIM_Provisioned_Range_Accounts", joinColumns = {
@JoinColumn(name = "SIMProvisionedRangeId")}, inverseJoinColumns = {
@JoinColumn(name = "AccountId")})
private Set<AdminAccounts> adminAccounts=new HashSet<AdminAccounts>(0);
@EmbeddedId
private BatchNumberIccidFrom batchNumberIccidFrom;
@Embeddable
public class BatchNumberIccidFrom implements Serializable {
private static final long serialVersionUID = 1L;
@Column
private Integer BatchNumber;
@Column
private String ICCIDFrom;
@Repository
@Transactional
public class AccountStatesRepository {
@Autowired
private SessionFactory sessionFactory;
private Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
public AccountStatesRepository() {}
@SuppressWarnings("unchecked")
public List<AdminAccountsStates> getAll()
{
Query query = this.getCurrentSession().createQuery("from M2M_AccountStates");
List<AdminAccountsStates> adminAccountsStatesList = query.list();
return adminAccountsStatesList;
}
}
**M2M_SIM_Provisioned_Range table**
CREATE TABLE IF NOT EXISTS `M2M_SIM_Provisioned_Range` (
`SIMProvisionedRangeId` INT NOT NULL AUTO_INCREMENT,
`BatchNumber` INT NOT NULL,
`ICCIDFrom` VARCHAR(45) NULL,
`AccountId` INT NULL,
UNIQUE INDEX `SIMProvisionedRangeId_UNIQUE` (`SIMProvisionedRangeId` ASC) ,
PRIMARY KEY (`BatchNumber`,ICCIDFrom),
INDEX `fk_M2M_SIM_Provisioned_Range_M2M_Accounts_idx` (`AccountId` ASC) ,
CONSTRAINT `fk_M2M_SIM_Provisioned_Range_M2M_Accounts`
FOREIGN KEY (`AccountId`)
REFERENCES `M2M_Accounts` (`AccountId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION )
ENGINE=InnoDB AUTO_INCREMENT=1;
**M2M_SIM_Provisioned_Range_Accounts table**
CREATE TABLE IF NOT EXISTS `M2M_SIM_Provisioned_Range_Accounts` (
`SIMProvisionedRangeId` INT NOT NULL,
`AccountId` INT NOT NULL,
INDEX `fk_M2M_SIM_Prov_Range_Accounts_M2M_SIM_Prov_Range_idx` (`SIMProvisionedRangeId` ASC) ,
CONSTRAINT `fk_M2M_SIM_Prov_Range_Accounts_M2M_SIM_Prov_Range`
FOREIGN KEY (`SIMProvisionedRangeId`)
REFERENCES `M2M_SIM_Provisioned_Range` (`SIMProvisionedRangeId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
INDEX `fk_M2M_SIM_Provisioned_Range_Accounts_M2M_Accounts_idx` (`AccountId` ASC) ,
CONSTRAINT `fk_M2M_SIM_Provisioned_Range_Accounts_M2M_Accounts`
FOREIGN KEY (`AccountId`)
REFERENCES `M2M_Accounts` (`AccountId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION) ENGINE=InnoDB AUTO_INCREMENT=1;
2015-11-10 17:24:10,032 WARN [AnnotationConfigWebApplicationContext] (AbstractApplicationContext.java:487) - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountStatesRepository': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.globetouch.business.entities.repositories.AccountStatesRepository.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.globetouch.config.ServletConfig: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: A Foreign key refering com.globetouch.business.entities.SIMProvisionedRange from com.globetouch.business.entities.AdminAccounts has the wrong number of column. should be 2
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5001)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1095)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1930)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.globetouch.business.entities.repositories.AccountStatesRepository.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.globetouch.config.ServletConfig: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: A Foreign key refering com.globetouch.business.entities.SIMProvisionedRange from com.globetouch.business.entities.AdminAccounts has the wrong number of column. should be 2
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 26 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.globetouch.config.ServletConfig: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: A Foreign key refering com.globetouch.business.entities.SIMProvisionedRange from com.globetouch.business.entities.AdminAccounts has the wrong number of column. should be 2
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 28 more
Caused by: org.hibernate.AnnotationException: A Foreign key refering com.globetouch.business.entities.SIMProvisionedRange from com.globetouch.business.entities.AdminAccounts has the wrong number of column. should be 2
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:420)
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1340)
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1153)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:733)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:668)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:69)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1639)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1393)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1784)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1868)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 38 more
2015-11-10 17:24:10,074 ERROR [ContextLoader] (ContextLoader.java:331) - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountStatesRepository': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.globetouch.business.entities.repositories.AccountStatesRepository.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.globetouch.config.ServletConfig: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: A Foreign key refering com.globetouch.business.entities.SIMProvisionedRange from com.globetouch.business.entities.AdminAccounts has the wrong number of column. should be 2
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5001)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1095)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1930)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.globetouch.business.entities.repositories.AccountStatesRepository.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.globetouch.config.ServletConfig: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: A Foreign key refering com.globetouch.business.entities.SIMProvisionedRange from com.globetouch.business.entities.AdminAccounts has the wrong number of column. should be 2
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 26 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.globetouch.config.ServletConfig: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: A Foreign key refering com.globetouch.business.entities.SIMProvisionedRange from com.globetouch.business.entities.AdminAccounts has the wrong number of column. should be 2
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 28 more
Caused by: org.hibernate.AnnotationException: A Foreign key refering com.globetouch.business.entities.SIMProvisionedRange from com.globetouch.business.entities.AdminAccounts has the wrong number of column. should be 2
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:420)
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1340)
at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1153)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:733)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:668)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:69)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1639)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1393)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1784)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1868)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 38 more
解决:
table1有1个唯一列和1个复合键。 table2有1个主键。 问:如何在hibernate中进行映射,(一对多单向)
溶液: 在joinColumns中使用@JoinColumn(name =“abc”,referencedColumnName =“abc”)
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "M2M_SIM_Provisioned_Range_Accountid_Mapping", joinColumns = {
@JoinColumn(name = "Sim_prov_uid",referencedColumnName="Sim_prov_uid") },
inverseJoinColumns = { @JoinColumn(name = "AccountId",referencedColumnName="AccountId") })
private Set<AdminAccounts> adminAccounts = new HashSet<AdminAccounts>();