Hibernate Exception: While migrating from mysql to oracle 11g db - mysql

I am trying to change my application database from mysql to oracle 11g. I am using hibernate in this application but hibernate is throwing:
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2231)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
at org.hibernate.loader.Loader.list(Loader.java:2120)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:312)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1722)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:175)
Below is my hibernate.cfg.xml file:
*<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- <property name="hibernate.default_schema">wind</property> -->
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">validate</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.use_query_cache">false</property>
<!-- <property name="connection.autoReconnect">true</property> -->
<!-- <property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">100</property>-->
<property name="hibernate.c3p0.timeout">25200</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>*
I am using one config file for database url and id password.
DATABASE_URL=jdbc:oracle:thin:#localhost:1521/wind
DATABASE_USERNAME=appuser
DATABASE_PASSWORD=appuser
DATABASE_CONN_POOL_MIN=5
DATABASE_CONN_POOL_MAX=50
Mapping file:
<hibernate-mapping>
<class name="com.nsn.abc.entity.UserInfo" table="user_info" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="assigned" />
</id>
<property name="username" type="string">
<column name="username" length="30" not-null="true" unique="true" />
</property>
<property name="firstName" type="string">
<column name="first_name" length="30" not-null="true" />
</property>
<property name="lastname" type="string">
<column name="lastname" length="30" />
</property>
<property name="email" type="string">
<column name="email" length="50" />
</property>
<property name="password" type="string">
<column name="password" length="30" not-null="true" />
</property>
<property name="phoneNumber" type="string">
<column name="phone_number" length="30" />
</property>
<property name="rights" type="string">
<column name="rights" length="30" not-null="true" />
</property>
<property name="counter" type="int">
<column name="counter" not-null="true" />
</property>
<property name="role" type="string">
<column name="role" length="20" />
</property>
<property name="activeFlag" type="boolean">
<column name="active_flag" not-null="true" />
</property>
<property name="info" type="string">
<column name="info" length="45" />
</property>
<set name="massProvisioningHistories" table="mass_provisioning_history" inverse="true" lazy="true" fetch="select">
<key>
<column name="user_ref" not-null="true" />
</key>
<one-to-many class="com.nsn.abc.entity.MassProvisioningHistory" />
</set>
</class>
</hibernate-mapping>
I have checked all the configuration and other details but still no progress.
Its throwing SQL Error: 923, SQLState: 42000. with ORA-00923: FROM keyword not found where expected
In the logs its genrating below query:
Hibernate: select userinfo0_.id as id25_, userinfo0_.username as username25_, userinfo0_.first_name as first3_25_, userinfo0_.lastname as lastname25_, userinfo0_.email as email25_, userinfo0_.password as password25_, userinfo0_.phone_number as phone7_25_, userinfo0_.rights as rights25_, userinfo0_.counter as counter25_, userinfo0_.role as role25_, userinfo0_.active_flag as active11_25_, userinfo0_.info as info25_ from user_info userinfo0_ where userinfo0_.username=?
Please suggest.
regards,
skr

In the application, we were using the SELECT 1 for checking the hibernate connection.
MySql supports "SELECT 1" but in oracle we need to mention "SELECT 1 from dual".
Now its working fine.
thanks for help.

Related

org.hibernate.MappingException: Association references unmapped class: empresa.Empleados

I have a configuration of hibernate with two tables in Mysql and the files of configuration of Pojo are the following:
<hibernate-mapping>
<class name="empresa.Departamentos" table="departamentos" catalog="empresa2" optimistic-lock="version">
<id name="deptno" type="int">
<column name="deptno" />
<generator class="assigned" />
</id>
<property name="dnombre" type="string">
<column name="dnombre" length="15" />
</property>
<property name="loc" type="string">
<column name="loc" length="15" />
</property>
<set name="empleadoses" table="empleados" inverse="true" lazy="true" fetch="select">
<key>
<column name="deptno" not-null="true" />
</key>
<one-to-many class="empresa.Empleados" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="empresa.Empleados" table="empleados" catalog="empresa2" optimistic-lock="version">
<id name="empno" type="short">
<column name="empno" />
<generator class="assigned" />
</id>
<many-to-one name="departamentos" class="empresa.Departamentos" fetch="select">
<column name="deptno" not-null="true" />
</many-to-one>
<property name="apellido" type="string">
<column name="apellido" length="10" />
</property>
<property name="oficio" type="string">
<column name="oficio" length="10" />
</property>
<property name="dir" type="java.lang.Short">
<column name="dir" />
</property>
<property name="fechaalt" type="date">
<column name="fechaalt" length="10" />
</property>
<property name="salario" type="java.lang.Float">
<column name="salario" precision="6" />
</property>
<property name="comision" type="java.lang.Float">
<column name="comision" precision="6" />
</property>
</class>
</hibernate-mapping>
Next, when I execute the following sentence in hql:
select e from Empleados e where e.departamentos.deptno in (select d.deptno from Departamentos d where d.loc='London')
It gives me the following error:
org.hibernate.MappingException: Association references unmapped class: empresa.Empleados
at org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2557)
at org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2808)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
Regards,
Javier
The solution is:
select e.apellido
from Empleados e
where e.departamentos.deptno in
(select d.deptno
from Departamentos d
where d.loc='Sevilla')

Exception : using inner joins in hibernate

I have a sql query like below,
SELECT patient.*,
contact.telephone
from patient
INNER JOIN contact ON patient.`idPatient` = contact.`idPatient`
These are my works,
Patient.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2016 1:55:08 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="db.Patient" table="patient" catalog="example_hibernate" optimistic-lock="version">
<id name="idPatient" type="java.lang.Integer">
<column name="idPatient" />
<generator class="identity" />
</id>
<property name="title" type="string">
<column name="title" length="45" />
</property>
<property name="firstName" type="string">
<column name="firstName" length="45" />
</property>
<property name="lastName" type="string">
<column name="lastName" length="45" />
</property>
<property name="middleName" type="string">
<column name="middleName" length="45" />
</property>
<property name="dob" type="date">
<column name="dob" length="10" />
</property>
<property name="martitalStatus" type="java.lang.Boolean">
<column name="martitalStatus" />
</property>
<property name="gender" type="string">
<column name="gender" length="45" />
</property>
<property name="nic" type="string">
<column name="nic" length="45" />
</property>
<property name="dateCreated" type="timestamp">
<column name="dateCreated" length="19" />
</property>
<property name="lastUpdated" type="timestamp">
<column name="lastUpdated" length="19" not-null="true" />
</property>
<set name="contacts" table="contact" inverse="true" lazy="true" fetch="select">
<key>
<column name="idPatient" not-null="true" />
</key>
<one-to-many class="db.Contact" />
</set>
</class>
</hibernate-mapping>
Contact.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2016 1:55:08 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="db.Contact" table="contact" catalog="example_hibernate" optimistic-lock="version">
<id name="idContact" type="java.lang.Integer">
<column name="idContact" />
<generator class="identity" />
</id>
<many-to-one name="patient" class="db.Patient" fetch="select">
<column name="idPatient" not-null="true" />
</many-to-one>
<property name="telephone" type="string">
<column name="telephone" length="45" />
</property>
<property name="address" type="string">
<column name="address" length="45" />
</property>
</class>
</hibernate-mapping>
Then I used above sql like below,
SessionFactory sessionFactory = new HibernateUtil().getSessionFactory();
Session session = sessionFactory.openSession();
List<Object[]> africanContinents = session.createQuery(
"SELECT patient.*,\n"
+ "contact.telephone\n"
+ "from patient\n"
+ "INNER JOIN contact ON patient.`idPatient` = contact.`idPatient`")
.list();
HibernateUtil.shutdown();
But this bought me following exception
org.hibernate.QueryException: unexpected char: '`' [SELECT patient.*,
contact.telephone
from patient
INNER JOIN contact ON patient.`idPatient` = contact.`idPatient`]
I have no idea about how to handle this exception . Because this sql query is correct.
Have any ideas about this exception ?
The query in createQuery should be written in HQL or JPQL.
So you could rewrite your query in HQL (something 'like select p,c.telephone from patient inner join contact') see documentation here
Or you could use your SQL query by calling session.createSQLQuery instead of session.createQuery
Hope this helps.
Sebastien

Hibernate join of two tables with foreign keys

I am trying to learn how to use hibernate and have run into a problem I have been stuck on for a while.
I am trying to join two tables together and display the values in a GUI form. I have tried researching for an answer but my case seems to be different then others listed. It seems to be a problem with the relationship between the ids of each table.
Here is the only statement I can get to display any results but it basically displays the same GameTemplates table twice.
Close as I can get:
from GameTemplates gt join gt.id.partNum Tickets
Desired SQL:
select * from Game_Templates
inner join Tickets
where
Game_Templates.part_Num = Tickets.game_Templates_Part_Num
GameTemplates.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2015 7:01:43 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="entity.GameTemplates" table="game_templates" catalog="fire_tickets">
<composite-id name="id" class="entity.GameTemplatesId">
<key-property name="partNum" type="string">
<column name="part_num" length="12" />
</key-property>
<key-property name="distsIdDistId" type="int">
<column name="dists_id_dist_id" />
</key-property>
<key-property name="mfgIdMfgId" type="int">
<column name="mfg_id_mfg_id" />
</key-property>
</composite-id>
<many-to-one name="distsId" class="entity.DistsId" update="false" insert="false" fetch="select">
<column name="dists_id_dist_id" not-null="true" />
</many-to-one>
<many-to-one name="mfgId" class="entity.MfgId" update="false" insert="false" fetch="select">
<column name="mfg_id_mfg_id" not-null="true" />
</many-to-one>
<property name="gameName" type="string">
<column name="game_name" length="20" />
</property>
<property name="gameCost" type="java.lang.Double">
<column name="game_cost" precision="22" scale="0" />
</property>
<property name="ticketCost" type="java.lang.Double">
<column name="ticket_cost" precision="22" scale="0" />
</property>
<property name="numTickets" type="java.lang.Integer">
<column name="num_tickets" />
</property>
<property name="ideaGross" type="java.lang.Integer">
<column name="idea_gross" />
</property>
<property name="ideaPrizes" type="java.lang.Integer">
<column name="idea_prizes" />
</property>
<property name="ideaNet" type="java.lang.Integer">
<column name="idea_net" />
</property>
<property name="lastSale" type="java.lang.Integer">
<column name="Last_sale" />
</property>
<property name="lastSaleAllow" type="java.lang.Integer">
<column name="Last_sale_allow" />
</property>
<property name="prizeAmt1" type="java.lang.Integer">
<column name="Prize_amt1" />
</property>
<property name="prizeAmt2" type="java.lang.Integer">
<column name="Prize_amt2" />
</property>
<property name="prizeAmt3" type="java.lang.Integer">
<column name="Prize_amt3" />
</property>
<property name="prizeAmt4" type="java.lang.Integer">
<column name="Prize_amt4" />
</property>
<property name="prizeAmt5" type="java.lang.Integer">
<column name="Prize_amt5" />
</property>
<property name="prizeAmt6" type="java.lang.Integer">
<column name="Prize_amt6" />
</property>
<property name="prizeAmt7" type="java.lang.Integer">
<column name="Prize_amt7" />
</property>
<property name="prizeAmt8" type="java.lang.Integer">
<column name="Prize_amt8" />
</property>
<property name="prizeAmt9" type="java.lang.Integer">
<column name="Prize_amt9" />
</property>
<property name="prizeAmt10" type="java.lang.Integer">
<column name="Prize_amt10" />
</property>
<property name="prizeAmt11" type="java.lang.Integer">
<column name="Prize_amt11" />
</property>
<property name="prizeAmt12" type="java.lang.Integer">
<column name="Prize_amt12" />
</property>
<property name="prizeAmt13" type="java.lang.Integer">
<column name="Prize_amt13" />
</property>
<property name="prizeAmt14" type="java.lang.Integer">
<column name="Prize_amt14" />
</property>
<property name="prizeAmt15" type="java.lang.Integer">
<column name="Prize_amt15" />
</property>
<property name="prizeAll1" type="java.lang.Integer">
<column name="Prize_all1" />
</property>
<property name="prizeAll2" type="java.lang.Integer">
<column name="Prize_all2" />
</property>
<property name="prizeAll3" type="java.lang.Integer">
<column name="Prize_all3" />
</property>
<property name="prizeAll4" type="java.lang.Integer">
<column name="Prize_all4" />
</property>
<property name="prizeAll5" type="java.lang.Integer">
<column name="Prize_all5" />
</property>
<property name="prizeAll6" type="java.lang.Integer">
<column name="Prize_all6" />
</property>
<property name="prizeAll7" type="java.lang.Integer">
<column name="Prize_all7" />
</property>
<property name="prizeAll8" type="java.lang.Integer">
<column name="Prize_all8" />
</property>
<property name="prizeAll9" type="java.lang.Integer">
<column name="Prize_all9" />
</property>
<property name="prizeAll10" type="java.lang.Integer">
<column name="Prize_all10" />
</property>
<property name="prizeAll11" type="java.lang.Integer">
<column name="Prize_all11" />
</property>
<property name="prizeAll12" type="java.lang.Integer">
<column name="Prize_all12" />
</property>
<property name="prizeAll13" type="java.lang.Integer">
<column name="Prize_all13" />
</property>
<property name="prizeAll14" type="java.lang.Integer">
<column name="Prize_all14" />
</property>
<property name="prizeAll15" type="java.lang.Integer">
<column name="Prize_all15" />
</property>
</class>
</hibernate-mapping>
Tickets.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 8, 2015 7:01:43 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="entity.Tickets" table="tickets" catalog="fire_tickets">
<composite-id name="id" class="entity.TicketsId">
<key-property name="serial" type="string">
<column name="Serial" length="12" />
</key-property>
<key-property name="gameTemplatesPartNum" type="string">
<column name="game_templates_part_num" length="12" />
</key-property>
</composite-id>
<property name="datePlaced" type="date">
<column name="Date_placed" length="10" />
</property>
<property name="dateRemoved" type="date">
<column name="Date_removed" length="10" />
</property>
<property name="unsoldAmt" type="java.lang.Integer">
<column name="Unsold_amt" />
</property>
<property name="actualGross" type="java.lang.Integer">
<column name="Actual_gross" />
</property>
<property name="actualPrizes" type="java.lang.Integer">
<column name="Actual_prizes" />
</property>
<property name="actualNet" type="java.lang.Integer">
<column name="Actual_net" />
</property>
<property name="bin" type="java.lang.Integer">
<column name="Bin" />
</property>
<property name="inplay" type="java.lang.Boolean">
<column name="Inplay" />
</property>
<property name="closed" type="java.lang.Integer">
<column name="Closed" />
</property>
<property name="unsoldTickets" type="java.lang.Integer">
<column name="Unsold_tickets" />
</property>
<property name="lastSaleRem" type="java.lang.Byte">
<column name="Last_sale_rem" />
</property>
<property name="prizeRem1" type="java.lang.Integer">
<column name="Prize_rem1" />
</property>
<property name="prizeRem2" type="java.lang.Integer">
<column name="Prize_rem2" />
</property>
<property name="prizeRem3" type="java.lang.Integer">
<column name="Prize_rem3" />
</property>
<property name="prizeRem4" type="java.lang.Integer">
<column name="Prize_rem4" />
</property>
<property name="prizeRem5" type="java.lang.Integer">
<column name="Prize_rem5" />
</property>
<property name="prizeRem6" type="java.lang.Integer">
<column name="Prize_rem6" />
</property>
<property name="prizeRem7" type="java.lang.Integer">
<column name="Prize_rem7" />
</property>
<property name="prizeRem8" type="java.lang.Integer">
<column name="Prize_rem8" />
</property>
<property name="prizeRem9" type="java.lang.Integer">
<column name="Prize_rem9" />
</property>
<property name="prizeRem10" type="java.lang.Integer">
<column name="Prize_rem10" />
</property>
<property name="prizeRem11" type="java.lang.Integer">
<column name="Prize_rem11" />
</property>
<property name="prizeRem12" type="java.lang.Integer">
<column name="Prize_rem12" />
</property>
<property name="prizeRem13" type="java.lang.Integer">
<column name="Prize_rem13" />
</property>
<property name="prizeRem14" type="java.lang.Integer">
<column name="Prize_rem14" />
</property>
<property name="prizeRem15" type="java.lang.Integer">
<column name="Prize_rem15" />
</property>
<property name="datePurch" type="date">
<column name="date_purch" length="10" />
</property>
<property name="invoiceNum" type="string">
<column name="invoice_num" length="10" />
</property>
</class>
</hibernate-mapping>
Try this:
from GameTemplates gt, Tickets t where gt.id.partNum = t.id.gameTemplatesPartNum

Eclipse Virgo Injected JPA EntityManager does not connect to Database

I am using Eclipse Virgo and I am trying to get annotation driven transaction management running but I keep running into the same issue.
Problem is that the EntityManager is injected and not null. But any query fails due to a connection error.
I used the setup according to GreenPages.
I have the following EntityManageFactory and TransactionManager configured
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="ShiftManagement" />
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
<property name="databasePlatform"
value="org.eclipse.persistence.platform.database.MySQLPlatform" />
</bean>
</property>
<property name="packagesToScan" value="....." />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
Also I have the following DataSources configured. Problem is, I tried them all, but when I try to connect to the database using a query, it doesn't work. Now, I am 100% sure that the credentials are OK, the DB is UP etc etc
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/shiftmanagement" />
<property name="username" value="root" />
<property name="password" value="rootroot" />
<property name="initialSize" value="5" />
<property name="maxActive" value="20" />
<property name="maxIdle" value="5" />
<!-- TRANSACTION_READ_COMMITTED = 2 -->
<property name="defaultTransactionIsolation" value="2" />
<!-- TRANSACTION_READ_UNCOMMITTED = 1 -->
<!-- <property name="defaultTransactionIsolation" value="1" /> -->
<property name="validationQuery" value="select 1 from dual" />
<property name="testOnBorrow" value="true" />
</bean>
Error when using commons-dbcp
Caused by: java.lang.UnsupportedOperationException: Not supported by BasicDataSource
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:901)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:132)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
Using Spring Datasource
<bean id="dataSource2"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/shiftmanagement" />
<property name="username" value="root" />
<property name="password" value="rootroot" />
</bean>
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/shiftmanagement
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
OK so then I tried the promising tomcat-jdbc library
<bean id="dataSource3" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/shiftmanagement" />
<property name="username" value="root" />
<property name="password" value="rootroot" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
<property name="minIdle" value="2" />
</bean>
And the following similar exception pops up
Caused by: org.eclipse.virgo.kernel.osgi.framework.ExtendedClassNotFoundException: com.mysql.jdbc.Driver in KernelBundleClassLoader: [bundle=org.apache.tomcat.jdbc_1.1.0.1]
at org.eclipse.virgo.kernel.userregion.internal.equinox.KernelBundleClassLoader.loadClass(KernelBundleClassLoader.java:139)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246)
I am using the following persistence.xml
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="ShiftManagement" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
....
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- Database options -->
<property name="eclipselink.target-database" value="MySQL"/>
<property name="eclipselink.weaving" value="false"/>
<property name="eclipselink.orm.throw.exceptions" value="true"/>
<!--
<property name="eclipselink.jdbc.read-connections.min" value="1"/>
<property name="eclipselink.jdbc.write-connections.min" value="1"/>
-->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/shiftmanagement"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="rootroot"/>
<!-- Logging -->
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.timestamp" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.logging.thread" value="false" />
</properties>
</persistence-unit>
I inject the EntityManager using
#PersistenceContext
private EntityManager em;
I read some posts to remove the username and password from the persistence.xml but then the system complains there are that user = [null].
Any help is greatly appreciated.
Note: mysqld is running and I can connect using mysql -uroot -prootroot
OK so I used a fourth datasource and all my problems dissapeared :-)
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/shiftmanagement" />
<property name="username" value="${javax.persistence.jdbc.user}" />
<property name="password" value="${javax.persistence.jdbc.password}" />
</bean>
You have to use the org.springframework.jdbc.datasource.DriverManagerDataSource see this thread
And in this case you see that the mysql driver is not found
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/shiftmanagement

Calling stored procedure from Hibernate / MySQL: Parameter index out of range (3 > number of parameters, which is 2)

I receive the following error when calling insert stored procedure in Hibernate while working with MySQL db:
Hibernate:
{ call InsertPayment(?, ?) }
sie 28, 2013 10:17:19 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: S1009
sie 28, 2013 10:17:19 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Parameter index out of range (3 > number of parameters, which is 2).
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [model_mapping_xml.TPayment]
Stored procedure definition in MySQL db:
CREATE PROCEDURE InsertPayment(
IN pIdAnother INT,
IN pAmount DECIMAL(19,4)
)
BEGIN
...
END
TPayment.hbm.xml file contains:
<sql-insert callable="true" check="none">
{ call InsertPayment(?, ?) }
</sql-insert>
Implicit call of the stored procedure:
// calling the stored procedure to add payment
TPayment newp = new TPayment();
newp.setAnother((TAnother) session.load(TAnother.class, 1));
newp.setAmount(BigDecimal.valueOf(20));
session.save(newp);
Why does it say "3 > number of parameters" where everywhere there are 2 parameters for this procedure?
(I can call deletePayment and modifyPayment stored procedures in similar way and they work fine...).
TPayment mapping:
<hibernate-mapping>
<class name="model_mapping_xml.TPayment" table="TPayment" catalog="DB">
<id name="idPayment" type="java.lang.Integer">
<column name="IdPayment" />
<generator class="identity" />
</id>
<version name="rowvers" type="timestamp" generated="always">
<column name="Rowvers" length="19" not-null="true" />
</version>
<many-to-one name="another" class="model_mapping_xml.TAnother" fetch="select">
<column name="IdAnother" not-null="true" />
</many-to-one>
<property name="amount" type="big_decimal">
<column name="Amount" scale="4" not-null="true" />
</property>
<property name="date1" type="timestamp">
<column name="Date1" length="19" not-null="false" />
</property>
<sql-insert callable="true" check="none">
{ call InsertPayment(?, ?) }
</sql-insert>
<sql-update callable="true" check="none">
{ call ModifyPayment(?, ?, ?, ?, ?) }
</sql-update>
<sql-delete callable="true" check="none">
{ call DeletePayment(?, ?) }
</sql-delete>
</class>
</hibernate-mapping>
TAnother mapping:
<hibernate-mapping>
<class name="model_mapping_xml.TAnother" table="TAnother" catalog="DB">
<id name="idAnother" type="java.lang.Integer">
<column name="IdAnother" />
<generator class="identity" />
</id>
<property name="dateBegin" type="date">
<column name="DateBegin" length="10" not-null="true" />
</property>
<property name="dateEnd" type="date">
<column name="DateEnd" length="10" />
</property>
<property name="rowvers" type="timestamp">
<column name="Rowvers" length="19" not-null="true" />
</property>
<set name="payment" table="TPayment"
inverse="true" lazy="true" fetch="select">
<key>
<column name="IdAnother" not-null="true" />
</key>
<one-to-many class="model_mapping_xml.TPayment" />
</set>
</class>
</hibernate-mapping>