gpt4 book ai didi

java - 具有可嵌入主键的表的 SQLGrammarException

转载 作者:行者123 更新时间:2023-12-01 13:17:22 26 4
gpt4 key购买 nike

我创建了 2 个表:BankBranches

我为这两个 POJO 创建了 DAO/BO 类和接口(interface)。

我使用注释映射了它们。

我正在使用 Hibernate 和 Spring。

我在 Bank 表上测试了 findAll 方法...

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

...并且成功了。

Bank 表的 idBranch 表的 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;
}


}

Branch 表的表示方式如下:

@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

最佳答案

您的错误显示“字段列表”中存在未知列“bankbranch0_.T_BANKNUM”尝试查找表中是否有 T_BANKNUM 列。它不会在那里,将其从映射 pojo 中删除。

关于java - 具有可嵌入主键的表的 SQLGrammarException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22360136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com