使用hibernate

时间:2016-08-02 15:38:04

标签: java mysql hibernate

我正在尝试使用hibernate查询query.uniqueResult() 我的查询包含

  来自com.classes.Project的

,其中NAME ='test'

但是我收到了一个错误:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   在'.project project0_附近,其中NAME ='test''在第1行

hibernate生成的完整查询是:

  

选择project0_.PROJECT_ID作为PROJECT1_60_,project0_.CL_ID为   CL2_60_,project0_.ATTACHMENT_LIST_ID为ATTACHMENT3_60_,   project0_.TCL_ID为TCL4_60_,project0_.RL_ID为RL5_60_,   project0_.NAME为NAME60_,project0_.DESCRIPTION为DESCRIPT7_60_,   project0_.LABEL为LABEL60_,project0_.ACTIVE为ACTIVE60_,   project0_.CREATED_BY为CREATED10_60_,project0_.CREATED_ON为   CREATED11_60_,project0_.LAST_MODIFIED_BY为LAST12_60_,   project0_.LAST_MODIFIED_ON为LAST13_60_,project0_.PROJECT_TYPE为   PROJECT14_60_,project0_.TA_SERVER_ID为TA15_60_来自   squashtmdev.squashtm.project project0_其中NAME ='test'

关联映射:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2 ao?t 2016 14:54:16 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.classes.Project" table="project" catalog="squashtmdev">
        <id name="projectId" type="java.lang.Long">
            <column name="PROJECT_ID" />
            <generator class="identity" />
        </id>
        <many-to-one name="campaignLibrary" class="com.classes.CampaignLibrary" fetch="select">
            <column name="CL_ID" />
        </many-to-one>
        <many-to-one name="attachmentList" class="com.classes.AttachmentList" fetch="select">
            <column name="ATTACHMENT_LIST_ID" not-null="true" />
        </many-to-one>
        <many-to-one name="testCaseLibrary" class="com.classes.TestCaseLibrary" fetch="select">
            <column name="TCL_ID" />
        </many-to-one>
        <many-to-one name="requirementLibrary" class="com.classes.RequirementLibrary" fetch="select">
            <column name="RL_ID" />
        </many-to-one>
        <property name="name" type="string">
            <column name="NAME" not-null="true" />
        </property>
        <property name="description" type="string">
            <column name="DESCRIPTION" />
        </property>
        <property name="label" type="string">
            <column name="LABEL" />
        </property>
        <property name="active" type="boolean">
            <column name="ACTIVE" not-null="true" />
        </property>
        <property name="createdBy" type="string">
            <column name="CREATED_BY" length="50" not-null="true" />
        </property>
        <property name="createdOn" type="timestamp">
            <column name="CREATED_ON" length="19" not-null="true" />
        </property>
        <property name="lastModifiedBy" type="string">
            <column name="LAST_MODIFIED_BY" length="50" />
        </property>
        <property name="lastModifiedOn" type="timestamp">
            <column name="LAST_MODIFIED_ON" length="19" />
        </property>
        <property name="projectType" type="char">
            <column name="PROJECT_TYPE" length="1" not-null="true" />
        </property>
        <property name="taServerId" type="java.lang.Long">
            <column name="TA_SERVER_ID" />
        </property>
        <set name="testCaseLibraryNodes" table="test_case_library_node" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="PROJECT_ID" />
            </key>
            <one-to-many class="com.classes.TestCaseLibraryNode" />
        </set>
        <set name="campaignLibraryNodes" table="campaign_library_node" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="PROJECT_ID" />
            </key>
            <one-to-many class="com.classes.CampaignLibraryNode" />
        </set>
        <set name="projectFilterEntries" table="project_filter_entry" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="PROJECT_ID" not-null="true" />
            </key>
            <one-to-many class="com.classes.ProjectFilterEntry" />
        </set>
        <set name="requirementLibraryNodes" table="requirement_library_node" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="PROJECT_ID" />
            </key>
            <one-to-many class="com.classes.RequirementLibraryNode" />
        </set>
    </class>
</hibernate-mapping>

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

这就是我解决问题的方法:

1 - 使用配置文件中的hibernate.default_catalog属性删除hbm文件上的目录并重新生成类。

2 - 如果它不起作用且错误现在为could not find the table yourDatabase.yourTable或使用HQL编辑器yourDatabase.yourTable is not mapped尝试删除相关hbm文件中的table="yourTable"

我不确定为什么必须这样做, 我希望它会有所帮助