2016-07-22 8 views
0

私のアプリケーションをPostgresqlとHibernate 5(MysqlとHibernate 4でした)と連動させていますが、私は困惑しています。Postgresql + Hibernate 5カラムが見つかりませんでしたが、coluimnが存在します

ユニットテストを実行すると、列の欠落についてはエラーが発生しますが、列は存在します。エラーメッセージのテーブル名と列名は、データベースの内容と100%同じです。

Entityクラスの抜粋:

@Entity 
@Table(name="bractiondata") 
public class BusinessRuleActionData { 

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private long id; 

    @ManyToOne(targetEntity=com.model.business.BusinessRule.class) 
    @Cascade({CascadeType.SAVE_UPDATE}) 
    @JoinColumn(name = "businessRule") 
    private BusinessRule businessRule; 

    @Enumerated(EnumType.STRING) 
    private RuleActionEnum actionType; 

    @OneToMany(mappedBy="action", targetEntity=com.model.business.action.BusinessRuleActionDataTuple.class) 
    @Cascade({CascadeType.ALL}) 
    @MapKey(name="fieldName") 
    private Map<String, BusinessRuleActionDataTuple> tuples = new HashMap<String, BusinessRuleActionDataTuple>(); 

...methods 

エラーメッセージ:

java.lang.IllegalStateException: Failed to load ApplicationContext 
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) 
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) 
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in file [E:\business\projecten\yobu\yobu-model\bin\testContext-model-hibernate.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128) 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) 
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108) 
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251) 
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) 
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) 
    ... 25 more 
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:884) 
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) 
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:338) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373) 
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) 
    ... 40 more 
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [actionType] in table [bractiondata] 
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:148) 
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.performValidation(SchemaValidatorImpl.java:100) 
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:67) 
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184) 
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:64) 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:458) 
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465) 
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:881) 
    ... 46 more 

PostgreSQLは、テーブルのステートメントを作成:

CREATE TABLE public.bractiondata 
(
    id bigint NOT NULL DEFAULT nextval('bractiondata_id_seq'::regclass), 
    "businessRule" bigint, 
    "actionType" character varying(100), 
    CONSTRAINT bractiondata_pkey PRIMARY KEY (id), 
    CONSTRAINT "bractiondata_businessRule_fkey" FOREIGN KEY ("businessRule") 
     REFERENCES public.businessrule (id) MATCH SIMPLE 
     ON UPDATE RESTRICT ON DELETE RESTRICT 
) 
WITH (
    OIDS=FALSE 
); 

テーブルが存在し、明らかに見つけることができます。列actionTypeも存在しますが、何らかの理由で見つからない場合があります。私は違いがないと思うが、@Columnアノテーションを追加しないようにしました。誰もがこの状況のこのエラーの原因を知ることができますか?

答えて

1

問題は、大文字と小文字が混在する列名です。それらの中から選択するSQLは、名前の前後に引用符を入れなければなりません。私はHibernateを知らないのですが、私の推測では、あなたのコードの引用符が実際の引用符ではなく文字列を囲む役割を果たすでしょう。

これはpsqlのセッションからコピーされます。

create temporary table btest1 
(id bigint, 
"businessRule" bigint, 
"actionType" character varying(100) 
); 

select id, businessRule, actionType from btest1; 
ERROR: column "businessrule" does not exist 

select id, "businessRule", "actionType" from btest1; 
id | businessRule | actionType 
----+--------------+------------ 
(0 rows) 

はHibernateによって生成されたSQLに二重引用符を取得し、またはすべて小文字を使用するように列名を変更する方法を見つけ出すことができます。

+0

ありがとうございます。はい、私はそれがこのようなものであるという強い疑いを持っていました。あるテーブルの中のいくつかのカラムをすべて小文字に改名してみました。私は名前を変更する必要がある列の名前を変更すると思います。これは、引用符を使わずにdbを直接操作するときにクエリを書く方が簡単になるためです。 。 – jvos

関連する問題