带有InheritanceType.JOINED的JPA / Hibernate在子类上执行select

时间:2011-09-30 14:40:13

标签: hibernate jpa

我使用JPA和Hibernate进行基本的继承层次结构设置,如下所示:

@Entity
@Table(uniqueConstraints=@UniqueConstraint(columnNames="email"))
@Inheritance(strategy = InheritanceType.JOINED)
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    protected String firstName;
    protected String surname;
    protected String email; 
    protected String organization;
      ...
}

使用像这样的子类

@Entity
@PrimaryKeyJoinColumn(name="UserId", referencedColumnName="id")
public class Coach extends User{

    public Coach(){

    }

}

这将为Players和其他派生子类创建一个Base User表和一个表,但现在我无法对Coach实体执行EJQ-QL查询。我试图通过以下方式返回单元测试中的教练列表:

public List<Coach> getCoachByName(String firstName) {
    return getJpaTemplate().find("select c from Coach c where c.firstName='"+firstName+"'");
}

但是我得到了GenericJDBC异常。

有什么建议吗?

这是请求的堆栈跟踪:

org.springframework.orm.hibernate3.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [select coach0_.UserId as id0_, coach0_1_.firstName as firstName0_, coach0_1_.surname as surname0_, coach0_1_.email as email0_, coach0_1_.organization as organiza5_0_, coach0_1_.hashedPassword as hashedPa6_0_, coach0_1_.confirmedRegistration as confirme7_0_ from Coach coach0_ inner join User coach0_1_ on coach0_.UserId=coach0_1_.id]; SQL state [3D000]; error code [1046]; could not execute query; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:642)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:95)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:212)
    at org.springframework.orm.jpa.JpaAccessor.translateIfNecessary(JpaAccessor.java:152)
    at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:189)
    at org.springframework.orm.jpa.JpaTemplate.executeFind(JpaTemplate.java:151)
    at org.springframework.orm.jpa.JpaTemplate.find(JpaTemplate.java:311)
    at org.springframework.orm.jpa.JpaTemplate.find(JpaTemplate.java:307)
    at com.playermetrics.dao.impl.CoachDaoImpl.getCoachByName(CoachDaoImpl.java:25)
    at com.playermetrics.entities.CoachDaoTest.testSaveCoach(CoachDaoTest.java:43)
    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:616)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
    at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.access$001(AbstractAnnotationAwareTransactionalTests.java:71)
    at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.run(AbstractAnnotationAwareTransactionalTests.java:175)
    at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTest(AbstractAnnotationAwareTransactionalTests.java:283)
    at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTestTimed(AbstractAnnotationAwareTransactionalTests.java:254)
    at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runBare(AbstractAnnotationAwareTransactionalTests.java:172)
    at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:174)
    at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:255)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.exception.GenericJDBCException: could not execute query
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.loader.Loader.doList(Loader.java:2214)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
    at org.hibernate.loader.Loader.list(Loader.java:2090)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:388)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:64)
    at org.springframework.orm.jpa.JpaTemplate$9.doInJpa(JpaTemplate.java:319)
    at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:184)
    ... 31 more
Caused by: java.sql.SQLException: No database selected
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2281)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1778)
    at org.hibernate.loader.Loader.doQuery(Loader.java:662)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
    at org.hibernate.loader.Loader.doList(Loader.java:2211)
    ... 41 more

0 个答案:

没有答案