MySQLSyntaxErrorException in Hibernate My SQL version - java

i have the following hibernate configuration:
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>com.google.hacatone.entity.MainCategory</value>
<value>com.google.hacatone.entity.GeorgianGrapes</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
when i try to select data from table i get the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'georgiangr0_' at line 1
please help me how to fix this problem, i try to use any mysql driver but problem not change
#Entity
#Table(name = "main_category", catalog = "hacatone_db")
public class MainCategory implements java.io.Serializable {
}
#Entity
#Table(name = "georgian_ grapes", catalog = "hacatone_db")
public class GeorgianGrapes implements java.io.Serializable {
}
this is my entity, how to force hibernate to generate right query?

Related

i have added dialect in hibernate properties but why i am getting "Dialect class not found: org.hibernate.dialect.oracl11gDialect"exception

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
<value>com.org.springsApps.Student</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.oracl11gDialect</prop>
</props>
</property>
</bean>
i have added dialect name in xml file but it's asking again for dialect
wrong class name in your xml: org.hibernate.dialect.Oracle10gDialect
hibernate.dialect setting needs to be a classname for a org.hibernate.dialect.Dialect subclass. Hibernate comes bundled with several dialects for some common RDBMS.
If your target database is Oracle 11g, then use org.hibernate.dialect.Oracle10gDialect as the setting value, per the table.

Hibernate No #NamedStoredProcedureQuery was found with that name

I am trying to call on a stored procedure from a db2 database using hibernate's entity manager and return the results as an object. I am using the #NamedStoredProcedureQuery annotation which does not seem to be found by hibernate. I am getting the error: "No #NamedStoredProcedureQuery was found with that name : Create_Division". Any ideas would be great.
beans:
<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="demoJPAUnit" />
<property name="packagesToScan">
<list>
<value>com.merchantBoarding.db2.queryObjects</value>
</list>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.archive.autodetection">class,hbm</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>
Query output object:
#NamedStoredProcedureQuery(
name="Create_Division",
procedureName="MD.PMDBD017",
resultClasses = {CreateDivisionResult.class},
parameters={
#StoredProcedureParameter(name="IN_ENTY",mode=ParameterMode.IN,type=Integer.class),
#StoredProcedureParameter(name="IN_PARNT_ENTY",mode=ParameterMode.IN,type=Integer.class),
#StoredProcedureParameter(name="IN_ACTION",mode=ParameterMode.IN,type=String.class),
#StoredProcedureParameter(name="IN_ENTY_XREF",mode=ParameterMode.IN,type=String.class),
#StoredProcedureParameter(name="OUT_ENTY",mode=ParameterMode.OUT,type=Integer.class),
#StoredProcedureParameter(name="OUT_ID",mode=ParameterMode.OUT,type=String.class),
#StoredProcedureParameter(name="OUT_ENTY_XREF",mode=ParameterMode.OUT,type=String.class),
#StoredProcedureParameter(name="OUT_SQLCODE",mode=ParameterMode.OUT,type=Integer.class),
#StoredProcedureParameter(name="OUT_STATUS",mode=ParameterMode.OUT,type=String.class),
#StoredProcedureParameter(name="OUT_ERRORTEXT",mode=ParameterMode.OUT,type=String.class),
}
)
public class CreateDivisionResult implements Serializable {
#Id
public String OUT_ENTY;
public String OUT_ID;
public String OUT_ENTY_XREF;
public String OUT_SQLCODE;
public String OUT_STATUS;
public String OUT_ERRORTEXT;
}
DAO code:
StoredProcedureQuery query = manager.createNamedStoredProcedureQuery("Create_Division");
query.setParameter("IN_ENTY", 111);
query.setParameter("IN_PARNT_ENTY", 11111);
query.setParameter("IN_ACTION", "CREATE");
query.setParameter("IN_ENTY_XREF", "ED");
List<CreateDivisionResult> results = query.getResultList();
EDIT:
So I added #Entity and that did fix the original error, but the output parameters still aren't mapping to the CreateDivisionResult object. I can only get each indiviual field with query.getOutputParameterValue. Is this just a restriction of JPA?
#NamedStoredProcedureQuery should be applied to Entity class or mapped class
Seems you are missing the #Entity in your 'CreateDivisionResult'

Joining two tables from two different libraries using java, DB2, spring and hibernate

I’m using DB2 on AS400 (iSeries), hibernate 3, spring 2.0 and Java 6 and I have two tables (physical files) on two different libraries like this: Library1/TableA and Library2/Table2, so normally I need to a sessionFactory for each library like this:
<bean id="sessionFactory1AS400"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSourceAS400" />
<property name="annotatedClasses">
<list>
<value>com.appllication.model.TableA</value>
</list>
</property>
<property name="hibernateProperties">
<props>
…
<prop key="hibernate.default_schema">Library1</prop>
</props>
</property>
</bean>
And
<bean id="sessionFactory2AS400"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSourceAS400" />
<property name="annotatedClasses">
<list>
<value>com.appllication.model.TableB</value>
</list>
</property>
<property name="hibernateProperties">
<props>
…
<prop key="hibernate.default_schema">Library2</prop>
</props>
</property>
</bean>
I’m trying to join the tables on my class like this:
#Entity(name = "TableA")
public class TableA {
#ManyToOne(targetEntity=TableB.class, fetch=FetchType.LAZY)
#JoinColumns(
{
#JoinColumn(name="column1", referencedColumnName="column1", insertable=false, updatable=false),
#JoinColumn(name="column2", referencedColumnName="column2", insertable=false, updatable=false)
})
private TableB tableB;
…
}
But when I run my unit test it fails because the DAO class can only load one sessionFactory at a time and my TableADao loads the sessionFactory1AS400 which has no knowledge of the existence of the TableB.
To overcome that problem I moved my TableB to the same sessionFactory as TableA:
<bean id="sessionFactory1AS400"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSourceAS400" />
<property name="annotatedClasses">
<list>
<value>com.appllication.model.TableA</value>
<value>com.appllication.model.TableB</value>
</list>
</property>
<property name="hibernateProperties">
<props>
…
<prop key="hibernate.default_schema">Library1</prop>
</props>
</property>
</bean>
And added the definition of the schema on class TableB:
#Entity(name="TableB")
#Table(schema="Library2")
public class TableB implements Serializable {
…
}
This time my test ran OK and gave me the correct query:
SELECT * FROM Library1.TableA ta INNER JOIN Library2.TableB tb ON ta.column1 = tb.column1 AND ta.column2 = tb.column2
Now my problem is that the schema definition on TableB is hardcoded instead of being loaded from a configuration file which is the perfect scenario, since we have different environments where the libraries names are different.
Is there a way to have the schema definition on TableB come from a configuration on spring or any other way to join these tables?
Thanks in advance for your time.
Joining tables over 2 different libraries is dead easy, all you need are some extra JDBC parameters:
In the JDBC url add parameter "libraries" and "naming". E.g.
jdbc:as400://systemName/;libraries="Library1 Library2";naming=system
With the above JDBC url you will be able to perform this query:
select * from TableA
inner join TableB on ... = ...
The iSeries machine will use the "libraries" parameter to find the physical files. Make you your physical file names are unique across both libraries to prevent conflicts.
Cheers,
How about using Hibernate's *.hbm.xml config instead of Annotations?
<class name="TableB" table="table_b" schema="Library2">
. . .
</class>
You can then specify which *.hbm.xml files to load in the Spring context XML, and then used properties configured through PropertyPlaceHolderConfigurer or similar.

HSQL org.hsqldb.HsqlException: invalid schema name

I am using HSQL to run a number of unit tests on my java application. I am using Spring + Hibernate. I am having a problem when switching from MySQL to HSQL. The tests run perfectly on MySQL but whenever I change to HSQL I get the following exception:
Caused by: org.hsqldb.HsqlException: invalid schema name: LMS
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.SchemaManager.getSchemaHsqlName(Unknown Source)
at org.hsqldb.SchemaManager.getSchemaName(Unknown Source)
at org.hsqldb.Session.getSchemaName(Unknown Source)
at org.hsqldb.SchemaManager.getTable(Unknown Source)
at org.hsqldb.ParserDQL.readTableName(Unknown Source)
at org.hsqldb.ParserDQL.readSimpleRangeVariable(Unknown Source)
at org.hsqldb.ParserDML.compileInsertStatement(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)
My Spring configuration is the following:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver" />
<property name="url" value="jdbc:hsqldb:file:lms" />
<property name="username" value="SA"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.dreamteam.lms.**.*</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>-->
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
</props>
</property>
</bean>
Sample Annotation on one of my classes:
#Entity
#Table(name = "answer", catalog = "lms")
public class Answer implements Cloneable, Serializable, IPojoGenEntity, IAnswer {
.
.
Any insight would be appreciated.
Regards
Chris
make "create-schema.sql" file
CREATE SCHEMA lms;
add "dataSourceInitializer" bean
<bean id="dataSourceInitializer" class="org.springframework.jdbc.datasource.init.DataSourceInitializer">
<property name="dataSource" ref="dataSource" />
<property name="databasePopulator">
<bean class="org.springframework.jdbc.datasource.init.ResourceDatabasePopulator">
<property name="continueOnError" value="true" />
<property name="scripts">
<list>
<value>classpath:SQL/create-schema.sql</value>
</list>
</property>
</bean>
</property>
</bean>
set "depends-on" attribute to "sessionFactory" bean
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" depends-on="dataSourceInitializer">
...
I use following bean to create schema during tests.
public class HSQLSchemaCreator {
private String schemaName;
private DataSource dataSource;
public HSQLSchemaCreator(String schemaName, DataSource dataSource) {
this.schemaName = schemaName;
this.dataSource = dataSource;
}
#PostConstruct
public void postConstruct() throws Exception {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("CREATE SCHEMA " + schemaName + " AUTHORIZATION DBA");
}
}
spring configuration:
<bean id="hsqlSchemaCreator" class="....HSQLSchemaCreator">
<constructor-arg name="schemaName" value="..."/>
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
<!-- Override entityManagerFactory to depend on hsqlSchemaCreator for tests -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="hsqlSchemaCreator">
and so on...
Just for the record I managed to solve this one by simply removing the 'catalog' attribute from my Hibernate entities. Hence,
#Entity
#Table(name = "answer", catalog = "lms")
became
#Entity
#Table(name = "answer")
Names for schemas, tables, columns, etc. are not (at least not by default) case sensitive in MySQL. HSQLDB is case sensitive, but it also converts all identifiers in query that are not quoted to the uppercase.
You can quickly test is this your problem by changing schema name to LMS everywhere (first in database). You can find more detailed story about HSQLDB and Hibernate from here: HSQLDB No such table Exception
I am not sure why this worked, but for me, at least, adding square brackets around the table names and schema did the trick for me:
#Table(name = "schema.tableName")
became
#Table(name = "[schema].[tableName]")

Joda type def loading fails during hibernate init

Joda time in pair with hibernate support is used. Configuration is as follows:
There's typedefs in org.joda.time.package-info.java:
#org.hibernate.annotations.TypeDefs({
#org.hibernate.annotations.TypeDef(
name="localDate",
typeClass =
org.joda.time.contrib.hibernate.PersistentLocalDate.class
)
})
package org.joda.time;
There's a spring context with session factory config:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>org.joda.time</value>
</list>
</property>
<property name="annotatedClasses">
<list>
<value>...</value>
...
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
Then there's test case:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration({"classpath:spring-test.xml"})
#Transactional
public class OperatorDaoTest extends AbstractTransactionalJUnit4SpringContextTests {
//autowired dao field defined
...
#Test
public void testMethod(){
//calls DAO method
}
}
Problem is in next exception:
Caused by: org.hibernate.MappingException: Could not determine type for: localDate, at table: TABLE_NAME, for columns: [org.hibernate.mapping.Column(DATE_COLUMN)]
I use standard Date to map to database, and then in my getter/setter I use joda-time and perform the conversion, to avoid issues like you are having. This may be of use.

Categories