GoZ GoZ - 4 months ago 11
Java Question

Error in SQL Syntax using hibernate

I'm trying to use an hibernate query doing

query.uniqueResult()

my query contains


from com.classes.Project where NAME='test'


But I get an error :


You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '.project project0_ where NAME='test'' at line 1


The full query generated by hibernate is :


select project0_.PROJECT_ID as PROJECT1_60_, project0_.CL_ID as
CL2_60_, project0_.ATTACHMENT_LIST_ID as ATTACHMENT3_60_,
project0_.TCL_ID as TCL4_60_, project0_.RL_ID as RL5_60_,
project0_.NAME as NAME60_, project0_.DESCRIPTION as DESCRIPT7_60_,
project0_.LABEL as LABEL60_, project0_.ACTIVE as ACTIVE60_,
project0_.CREATED_BY as CREATED10_60_, project0_.CREATED_ON as
CREATED11_60_, project0_.LAST_MODIFIED_BY as LAST12_60_,
project0_.LAST_MODIFIED_ON as LAST13_60_, project0_.PROJECT_TYPE as
PROJECT14_60_, project0_.TA_SERVER_ID as TA15_60_ from
squashtmdev.squashtm.project project0_ where NAME='test'


Associated mapping :

<?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>


Any ideas why this is happening ?

GoZ GoZ
Answer

This is how I fixed my problem :

1 - Remove the catalog on the hbm files by using the hibernate.default_catalog property in the configuration file and regenerating the classes.

2 - Then if it didn't work and the error is now could not find the table yourDatabase.yourTable or using HQL editor yourDatabase.yourTable is not mapped try to remove the table="yourTable" in the concerned hbm file

I'am not sure about why I had to do that, I hope it will help