session.list上的HSQLException。错误:意外的令牌"实体名称"

时间:2015-09-24 02:34:44

标签: java sql hibernate hsqldb

我正在为其中一个项目进行hibernate / JPA设置,我遇到了奇怪的错误。

每当我尝试进行session.list()调用时,我首先得到以下警告。

6565 [main] WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper  - SQL Error: -5581, SQLState: 42581 

在此之后,我收到以下错误。

意外令牌:ADSUBCONTE8_

详细的堆栈跟踪: -

org.hibernate.exception.SQLGrammarException: could not prepare statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:82)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:188)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:159)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1858)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1835)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1815)
    at org.hibernate.loader.Loader.doQuery(Loader.java:899)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:341)
    at org.hibernate.loader.Loader.doList(Loader.java:2522)
    at org.hibernate.loader.Loader.doList(Loader.java:2508)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2338)
    at org.hibernate.loader.Loader.list(Loader.java:2333)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:124)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1662)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)

Caused by: java.sql.SQLSyntaxErrorException: unexpected token: ADSUBCONTE8_
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
    at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:281)
    at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:313)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:161)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:182)
    ... 49 more
Caused by: org.hsqldb.HsqlException: unexpected token: ADSUBCONTE8_
    at org.hsqldb.error.Error.parseError(Unknown Source)
    at org.hsqldb.ParserBase.unexpectedToken(Unknown Source)
    at org.hsqldb.ParserDQL.XreadSimpleValueExpressionPrimary(Unknown Source)
    at org.hsqldb.ParserDQL.XreadAllTypesValueExpressionPrimary(Unknown Source)
    at org.hsqldb.ParserDQL.XreadAllTypesPrimary(Unknown Source)
    at org.hsqldb.ParserDQL.XreadAllTypesFactor(Unknown Source)
    at org.hsqldb.ParserDQL.XreadAllTypesTerm(Unknown Source)
    at org.hsqldb.ParserDQL.XreadAllTypesCommonValueExpression(Unknown Source)
    at org.hsqldb.ParserDQL.XreadValueExpression(Unknown Source)
    at org.hsqldb.ParserDQL.XreadSelect(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQuerySpecification(Unknown Source)
    at org.hsqldb.ParserDQL.XreadSimpleTable(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryPrimary(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryTerm(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryExpressionBody(Unknown Source)
    at org.hsqldb.ParserDQL.XreadQueryExpression(Unknown Source)
    at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
    at org.hsqldb.ParserCommand.compilePart(Unknown Source)
    at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
    at org.hsqldb.Session.compileStatement(Unknown Source)
    at org.hsqldb.StatementManager.compile(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    ... 55 more

manyToOne relationShip中两个类的java类如下(突出显示感兴趣的行)

/**
 * The persistent class for the ad_context_tags database table.
 * 
 */
@Entity
@Table(name = "ad_context_tags")
@NamedQuery(name = "AdContextTagEntity.findAll", query = "SELECT a FROM AdContextTagEntity a")
public class AdContextTagEntity implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(unique = true, nullable = false)
    private int id;

    @Column(name = "created_by", nullable = false, length = 100)
    private String createdBy;

    @Temporal(TemporalType.DATE)
    @Column(name = "creation_date", nullable = false)
    private Date creationDate;

    @Temporal(TemporalType.DATE)
    @Column(name = "last_update_date")
    private Date lastUpdateDate;

    // bi-directional many-to-one association to AdCategoryEntity
    @ManyToOne
    @JoinColumn(name = "category", nullable = false)
    private AdCategoryEntity adCategory;

    // bi-directional many-to-one association to AdPageInfoEntity
    @ManyToOne
    @JoinColumn(name = "page_info", nullable = false)
    private AdPageInfoEntity adPageInfo;

    // bi-directional many-to-one association to AdSlotFamilyEntity
    @ManyToOne
    @JoinColumn(name = "`slot_group`", nullable = false)
    private AdSlotFamilyEntity adSlotFamily;

    // bi-directional many-to-one association to MarketplaceMappingEntity
    @ManyToOne
    @JoinColumn(name = "siterealm")
    private LegacyMarketplaceMappingEntity marketplaceMapping;

// ========== the field that gives the error below. Class defined later in the post ======
        @ManyToOne
        @JoinColumn(name = "subcontext")
        private AdSubcontextEntity subcontextEntity;

        public AdSubcontextEntity getSubcontextEntity()
        {
             return subcontextEntity;
        }

         public void setSubcontextEntity(AdSubcontextEntity subcontextEntity)
         {
             this.subcontextEntity = subcontextEntity;
          }


    public AdContextTagEntity()
    {
    }

    public int getId()
    {
        return this.id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getCreatedBy()
    {
        return this.createdBy;
    }

    public void setCreatedBy(String createdBy)
    {
        this.createdBy = createdBy;
    }

    public Date getCreationDate()
    {
        return this.creationDate;
    }

    public void setCreationDate(Date creationDate)
    {
        this.creationDate = creationDate;
    }

    public Date getLastUpdateDate()
    {
        return this.lastUpdateDate;
    }

    public void setLastUpdateDate(Date lastUpdateDate)
    {
        this.lastUpdateDate = lastUpdateDate;
    }

    public AdCategoryEntity getAdCategory()
    {
        return this.adCategory;
    }

    public void setAdCategory(AdCategoryEntity adCategory)
    {
        this.adCategory = adCategory;
    }

    public AdPageInfoEntity getAdPageInfo()
    {
        return this.adPageInfo;
    }

    public void setAdPageInfo(AdPageInfoEntity adPageInfo)
    {
        this.adPageInfo = adPageInfo;
    }

    public AdSlotFamilyEntity getAdSlotFamily()
    {
        return this.adSlotFamily;
    }

    public void setAdSlotFamily(AdSlotFamilyEntity adSlotFamily)
    {
        this.adSlotFamily = adSlotFamily;
    }

    public LegacyMarketplaceMappingEntity getMarketplaceMapping()
    {
        return this.marketplaceMapping;
    }

    public void setMarketplaceMapping(LegacyMarketplaceMappingEntity marketplaceMapping)
    {
        this.marketplaceMapping = marketplaceMapping;
    }

    public DcAdLocation constructSerializable()
    {
        // TODO Auto-generated method stub
        return new DcAdLocation();
    }

}

有问题的课程如下: -

@Entity
@Table(name="ad_subcontext")
@NamedQuery(name="AdSubcontextEntity.findAll", query="SELECT a FROM AdSubcontextEntity a")
public class AdSubcontextEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(unique=true, nullable=false)
    private int id;

    @Column(nullable=false, length=20)
    private String primary;

    @Column(length=11)
    private String secondary;

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPrimary() {
        return this.primary;
    }

    public void setPrimary(String primary) {
        this.primary = primary;
    }

    public String getSecondary() {
        return this.secondary;
    }

    public void setSecondary(String secondary) {
        this.secondary = secondary;
    }


}

hibernate生成的SQL如下: -

/**
 * select this_.id as id1_6_7_, this_.category as category5_6_7_,
 * this_.page_info as page6_6_7_, this_."slot_group" as slot7_6_7_,
 * this_.created_by as created2_6_7_, this_.creation_date as creation3_6_7_,
 * this_.foo as foo8_6_7_, this_.last_update_date as last4_6_7_, this_.siterealm
 * as sitereal9_6_7_, adcategory2_.id as id1_3_0_, adcategory2_.name as
 * name2_3_0_, adpageinfo3_.id as id1_8_1_, adpageinfo3_.page_family as
 * page5_8_1_, adpageinfo3_.browse_node as browse2_8_1_, adpageinfo3_.page_name
 * as page3_8_1_, adpageinfo3_.page_short_name as page4_8_1_, adpageamab4_.id as
 * id1_7_2_, adpageamab4_.amabotId as amabotId2_7_2_, adpageamab4_.family as
 * family3_7_2_, adslotfami5_.id as id1_12_3_, adslotfami5_."slot_family_name"
 * as slot2_12_3_, foo6_.id as id1_0_4_, foo6_.primary as primary2_0_4_,
 * foo6_.secondary as secondar3_0_4_, legacymark7_.id as id1_24_5_,
 * legacymark7_.client_id as client4_24_5_, legacymark7_.client_zone as
 * client2_24_5_, legacymark7_.marketplace_id as marketpl3_24_5_,
 * legacyclie8_.id as id1_19_6_, legacyclie8_.client_name as client2_19_6_ from
 * ad_context_tags this_ inner join ad_category adcategory2_ on
 * this_.category=adcategory2_.id inner join ad_page_info adpageinfo3_ on
 * this_.page_info=adpageinfo3_.id left outer join ad_page_amabot_mapping
 * adpageamab4_ on adpageinfo3_.page_family=adpageamab4_.id inner join
 * ad_slot_family adslotfami5_ on this_."slot_group"=adslotfami5_.id inner join
 * Foo foo6_ on this_.foo=foo6_.id left outer join marketplace_mappings
 * legacymark7_ on this_.siterealm=legacymark7_.id left outer join clients
 * legacyclie8_ on legacymark7_.client_id=legacyclie8_.id
 */

我也做了一些调查,看起来这个ADSUBCONTE8无论如何都会生成。我尝试将其重命名为Crap.java,但后来我一直将错误视为意外的令牌Crap6。 如果我尝试从父AdContextTagEntity中删除AdSubContext,则测试和构建传递正常。

请帮助我,因为我真的被困在这个。

2 个答案:

答案 0 :(得分:3)

First of all you are right the warning and the exception are interrelated . Both warning and exception are pointing to unexpected token as specified here.


The exception is not in Hibernate, but probably in HSQLDB.The sub query seems to be the problem. Hsqldb doesn't seem to be able to parse sub queries.Some things we can try on it.(apologies if you already tried them)

  1. Try changing the column names for primary and secondary.Basically check for reserved words as it is possible to have this error when you use some in naming.
  2. Check through your SQL file if it is getting executed correctly in standalone mode and creating all tables with all expected constraints especailly related to ad_subcontext table.Saying this as dint find much in the generated query which you added to be question. Try the similar SQL on your DB i am quite sure it will have similar error.

答案 1 :(得分:1)

看起来SQL是由您在没有&#34; subcontextEntity&#34;生成的情况下生成的。它的字段原因并不包含表ad_subcontext的连接。

异常不在Hibernate中,而是在HSQLDB驱动程序中。因此,驱动程序只能通过某些原因解析SQL。

可能的原因:

使用&#34; primary&#34;,&#34; secondary&#34;作为字段名称 - 可以为主键和辅助键保留此名称。

使用奇怪的名字&#34;`slot_group`&#34;在

@JoinColumn(name = "`slot_group`", nullable = false)

或其他&#34;字段名称&#34;原因。