2014-03-12 51 views
0

表我創建2個表,銀行分行SQLGrammarException用於與嵌入主鍵

我爲兩個POJO創建了DAO/BO類和接口。

我使用註解映射它們。

我正在使用Hibernate和Spring。

我在銀行表測試了findAll方法...

public List<Bank> findAll() { 
    return getHibernateTemplate().find("from Bank"); 
} 

...它是成功的。

ID 銀行,包括 - 沿着ID分公司表 - 在分公司表的主鍵。

所以我做了一個Embeddable類:

@Embeddable 
public class BranchPK implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = -8721469933759558600L; 
    private Integer brchNum; 
    private Integer bankNum; 

    public BranchPK() { 

    } 
    @Column(name="BRCHNUM", precision=3, scale=0) 
    public Integer getBrchNum() { 
     return this.brchNum; 
    } 
    @Column(name="BANKNUM", precision=3, scale=0) 
    public Integer getBankNum() { 
     return this.bankNum; 
    } 

    public void setBrchNum(Integer brchNum) { 
     this.brchNum = brchNum; 
    } 


    public void setBankNum(Integer bankNum) { 
     this.bankNum = bankNum; 
    } 


} 

分公司表是這樣表示:

@Entity 
@Table(name="BRANCH") 
public class Branch implements Serializable { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = 9075916046034338274L; 
    @EmbeddedId 
    private BranchPK primaryKey; 
    @ManyToOne(fetch=FetchType.LAZY) 
@JoinColumn(name="BANKNUM") 
    private Bank bank; 
......... 

一切似乎不錯,爲了,但是當我重複以前的findAll方法取所有分支

public List<Branch> findAll() { 
    return getHibernateTemplate().find("from Branch"); 
} 

我收到此錯誤:

could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

更新

Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.612 sec <<< FAILURE! 
testFinddAll(bdl.cdr.core.BankBankDAOTest) Time elapsed: 0.19 sec <<< ERROR! 
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query 
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:630) 
    at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) 
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424) 
    at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) 
    at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:921) 
    at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:913) 
    at bdl.cdr.core.dao.impl.BankBranchDAOImpl.findAll(BankBranchDAOImpl.java:14) 
    at bdl.cdr.core.bo.impl.BankBranchBOImpl.findAll(BankBranchBOImpl.java:19) 
    at bdl.cdr.core.BankBankDAOTest.testFinddAll(BankBankDAOTest.java:26) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at junit.framework.TestCase.runTest(TestCase.java:176) 
    at junit.framework.TestCase.runBare(TestCase.java:141) 
    at junit.framework.TestResult$1.protect(TestResult.java:122) 
    at junit.framework.TestResult.runProtected(TestResult.java:142) 
    at junit.framework.TestResult.run(TestResult.java:125) 
    at junit.framework.TestCase.run(TestCase.java:129) 
    at junit.framework.TestSuite.runTest(TestSuite.java:255) 
    at junit.framework.TestSuite.run(TestSuite.java:250) 
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84) 
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) 
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) 
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) 
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) 
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) 
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) 
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
    at org.hibernate.loader.Loader.doList(Loader.java:2545) 
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276) 
    at org.hibernate.loader.Loader.list(Loader.java:2271) 
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:459) 
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:365) 
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196) 
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268) 
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102) 
    at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:930) 
    at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419) 
    ... 31 more 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'bankbranch0_.T_BANKNUM' in 'field list' 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    at com.mysql.jdbc.Util.getInstance(Util.java:386) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2834) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156) 
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2313) 
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208) 
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1953) 
    at org.hibernate.loader.Loader.doQuery(Loader.java:802) 
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274) 
    at org.hibernate.loader.Loader.doList(Loader.java:2542) 
    ... 40 more 
+0

您能否發佈完整的堆棧跟蹤?文字上面的例外也 – Zeus

+0

@宙斯檢查更新請(你會注意到一些類有不同的名稱,我做了上述例子澄清事情)。 –

回答

1

你的錯誤顯示Unknown column 'bankbranch0_.T_BANKNUM' in 'field list'試圖找到如果表在表中的列T_BANKNUM。它不會在那裏,從映射pojo中刪除它。

+0

THAAAAAAAAAAAANKS! :D它被命名爲'F_BANKNUM',所以我輸錯了它!我必須學習如何讀取異常。 –