org.hibernate.HibernateException:缺少列

Posted

技术标签:

【中文标题】org.hibernate.HibernateException:缺少列【英文标题】:org.hibernate.HibernateException: Missing column 【发布时间】:2013-08-14 12:53:05 【问题描述】:

我在带有 Maven 的应用程序中使用 Spring 3 和 Hibernate 4。我遇到了一些由于 Hibernate Mapping 而导致的问题,所以我正在处理它。但现在我被卡住了,无法找到解决方案。如果我做错了什么,请纠正我。我的实体类是我编写 NamedQuery 的地方:-

    @Entity

       @NamedQuery(
            name = "findAllProduct",
            query = "from PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION pvpo"
            +" INNER JOIN pvpo.pcvId  pcv" 
            +" INNER JOIN pcv.productCatalogId pc"
            +" INNER JOIN pc.productCatalogId id"
            +" INNER JOIN pvpo.pincode pin"
            +" where id = :id"
            )


        public class PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION extends baseEntity.Entity 

            @Id
            @GeneratedValue(strategy=GenerationType.AUTO)
            private int id;

            @Column(name="Payment_Id")
            @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="id") 
            private Set<Product_Catalog_Vendor> pcvId; 


            @Column(name="pincode_id")
            @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="pincode_id")
            private Set<Pincodes> pincode;          


      @Column(name = "PAYMENT_OPTIONS")
            @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="paymentOptions") 
            private Set<Payment_Options> paymentOptions;
        //setter getter
        

我的 Payment_Options 类是:-

@Entity
@Table(name="payement_options")     
public class Payment_Options extends baseEntity.Entity 

    @Id
    @JoinColumn(name="PAYMENT_OPTIONS") 
    private int paymentOptions;

    @Column(name="Description")
    private String description;
//Setter Getter

PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION 和 PAYMENT_OPTIONS 数据库中的表:-

    CREATE TABLE PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION (
      ID INTEGER UNSIGNED  NOT NULL   AUTO_INCREMENT,
      Payment_Id INTEGER UNSIGNED  NOT NULL  ,   
      PINCODE_ID INTEGER UNSIGNED  NOT NULL  ,
      PAYMENT_OPTIONS INTEGER UNSIGNED NOT NULL ,   
    PRIMARY KEY(ID,Payment_Id, PINCODE_ID)  , 
    INDEX PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION_FKIndex1(PAYMENT_OPTIONS),
    INDEX PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION_FKIndex2(Payment_Id)  ,
    INDEX PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION_FKIndex3(PINCODE_ID),
      FOREIGN KEY(PAYMENT_OPTIONS)   
      REFERENCES PAYEMENT_OPTIONS(PAYMENT_OPTIONS)
          ON DELETE NO ACTION
          ON UPDATE NO ACTION,
      FOREIGN KEY(Payment_Id)
        REFERENCES PRODUCT_CATALOG_VENDOR(Id)
          ON DELETE NO ACTION
          ON UPDATE NO ACTION,
      FOREIGN KEY(PINCODE_ID)
        REFERENCES PINCODES(PINCODE_ID)
          ON DELETE NO ACTION
          ON UPDATE NO ACTION);

...

    CREATE TABLE PAYEMENT_OPTIONS (
      PAYMENT_OPTIONS INTEGER UNSIGNED  NOT NULL   AUTO_INCREMENT,
      Description VARCHAR(500)  NULL    ,
    PRIMARY KEY(PAYMENT_OPTIONS));

我在部署时遇到错误。在下面为我提供一些解决方案。谢谢!

18:12:26,614 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] (MSC service thread 1-3) Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6e677ea2: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,productController,usermanagementHibernateProperties,jspViewResolver,usermanagementSessionFactory,usermanagementDataSource,city,state,country,pincodes,notification,notification_types,transactions,address,user_master,notification_channels,notification_time,prefilled_response,payment_options,catagory,vendor,requester,requirement_type,discount_offer_type,discount_offers,requirements,product_catalog,product_catalog_vendor,product_vendor_payment_option_location,baseDaoImpl,productDAO,productService,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionManager,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
18:12:26,624 SEVERE [org.springframework.web.servlet.DispatcherServlet] (MSC service thread 1-3) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ecom.data.access.transaction.ProductService com.ecom.data.access.controller.ProductController.productService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ecom.data.access.product.ProductDAO com.ecom.data.access.transaction.ProductServiceImpl.productDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.ecom.data.access.product.ProductDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usermanagementSessionFactory' defined in class path resource [applicationContext-usermanagement-dao.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing column: paymentOptions in 2402.payement_options
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1122) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) [spring-context-3.2.2.RELEASE.jar:]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) [spring-context-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459) [spring-webmvc-3.2.2.RELEASE.jar:]
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) [spring-webmvc-3.2.2.RELEASE.jar:]
    at javax.servlet.GenericServlet.init(GenericServlet.java:242) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1202) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1102) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3631) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3844) [jbossweb-7.0.1.Final.jar:7.0.2.Final]
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:70) [jboss-as-web-7.0.2.Final.jar:7.0.2.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) [:1.6.0_43]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) [:1.6.0_43]
    at java.lang.Thread.run(Thread.java:662) [:1.6.0_43]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ecom.data.access.transaction.ProductService com.ecom.data.access.controller.ProductController.productService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ecom.data.access.product.ProductDAO com.ecom.data.access.transaction.ProductServiceImpl.productDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.ecom.data.access.product.ProductDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usermanagementSessionFactory' defined in class path resource [applicationContext-usermanagement-dao.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing column: paymentOptions in 2402.payement_options
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) [spring-beans-3.2.2.RELEASE.jar:]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) [spring-beans-3.2.2.RELEASE.jar:]
    ... 27 more

【问题讨论】:

【参考方案1】:

您需要创建数据库表,以便它们与 Entitymapping 匹配。

第一个问题是,Payment_Options 表中的 id 列名为 PAYMENT_OPTIONS。但是您在Payment_Options 类中的字段名为paymentOptions(并且没有@Column 注释)- 这不匹配!

所以在字段中添加@Column(name="PAYMENT_OPTIONS")

@Id
@Column(name="PAYMENT_OPTIONS")
@JoinColumn(name="PAYMENT_OPTIONS") //I have no idea what you want to do with this annotation
private int paymentOptions;

但真正的问题是您对Payment_OptionsPRODUCT_VENDOR_PAYMENT_OPTION_LOCATION 之间关系的映射非常奇怪。

首先,我认为您将它们命名错误。没有sPaymentOption

我推荐:

public class PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION
    ...

   @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER, mappedBy="pvpol") 
   private Set<Payment_Options> paymentOptions;      
   ...


public class PaymentOption ...
   ...
   @ManyToOne
   PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION pvpol;
   ...

【讨论】:

感谢您的回复。您的建议是正确的,但我不想在我的任何实体类或数据库表中创建新属性。我已经在数据库中创建了表。请参考 PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION 表的已编辑问题,其中我有 3 个外键和一个复合键。我正在使用 joincolumn 引用 Payment_Options 类的外键,同样我正在引用 pincode 和 Product_Catalog_Vendor 键的键,它们工作正常,所以为什么我遇到 Payment_Options 类的问题。所以告诉我这个映射有什么问题,我错过了什么? 问题不是 PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION 表,问题是 Payment_Option 表。但是你还没有发布它的结构! 查看我的扩展答案。【参考方案2】:

我有类似的问题。 就我而言,我必须 删除 属性:

spring.jpa.hibernate.ddl-auto=validate

来自我的 application.properties 文件

【讨论】:

这只是通过关闭验证来隐藏问题,这并不能解决问题。

以上是关于org.hibernate.HibernateException:缺少列的主要内容,如果未能解决你的问题,请参考以下文章