Invalid column name with Hibernate NamedQuery - java

My Hibernate hbm file looks like this. I had issues when running this query. The query was running fine on sql developer, but when running through my java application, it is throwing SQL Error: 17006, SQLState: 99999 error which is saying Invalid Column. I tried many ways to take columns off and change property names but could not find a solution.
2015-07-16 12:15:14,374 [http-bio-8080-exec-8] DEBUG com.adp.au.hibernate.HibernateUtils - HibernateUtils.closeSession(false) for session depth: [1]
2015-07-16 12:15:14,389 [http-bio-8080-exec-8] ERROR com.adp.au.timesheet.action.SubmitError - Client: [29010], User: [ADMIN] exception: [could not execute query]
2015-07-16 12:15:14,389 [http-bio-8080-exec-8] ERROR com.adp.au.timesheet.action.SubmitError - Client: [29010], User: [ADMIN] stacktrace: org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2545)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
at org.hibernate.loader.Loader.list(Loader.java:2271)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1842)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:890)
Could someone please look into this and help me?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class mutable="false" name="com.adp.au.timesheet.external.PLClient">
<id name="clientId"/>
<property name="branch"/>
<property name="name"/>
<property name="timesheetType"/>
<property name="costSplitting" type="yes_no"/>
<property name="payconReportCode"/>
<property name="ownEftUseInd" type="yes_no"/>
<property name="essUse" type="integer"/>
</class>
<sql-query name="plClient.byClientId.forABAFile" read-only="true">
<return alias="client" class="com.adp.au.timesheet.external.PLClient"/>
SELECT
TRIM(CLI_ID) AS {client.clientId},
OWN_EFT_USE_IND AS {client.ownEftUseInd}
FROM SAG.PL_CLI`
WHERE (TRIM(PL_CLI.CEASED_DATE) IS NULL OR PL_CLI.CEASED_DATE >TO_CHAR(SYSDATE, 'YYYYMMDD'))
AND PL_CLI.CLI_ID = RPAD(:clientId, 7)
</sql-query>
</hibernate-mapping>

Related

Hibernate saveOrUpdate Not working with Dynamic mapping

I am using Hibernate dynamic modeling for persisting my objects. The issue i am facing right now is it is throwing exception when i am calling saveOrUpdate function. The exception is
2020-07-08 23:14:06 WARN SqlExceptionHelper:137 - SQL Error: 1062, SQLState: 23000
2020-07-08 23:14:06 ERROR SqlExceptionHelper:142 - Duplicate entry 'TYUTQ-2020-07-08 20:19:53' for key 'PRIMARY'
2020-07-08 23:14:06 ERROR ExceptionMapperStandardImpl:39 - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
When object is going to insert first time, it will successfully inserted but when object is already present in the database then saveOrUpdate method again call the insert method. I verified by using SHOW_SQL = "true" property.
The question is why it is not calling the update method?
Hibernate mapping file is :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="myTable">
<composite-id>
<key-property name="code" column="code" type="java.lang.String" />
<key-property name="datetime" column="datetime" type="java.sql.Timestamp" />
</composite-id>
<property name="A" column="A_colummn" type="java.math.BigDecimal" />
<property name="B" column="B_column" type="java.math.BigDecimal" />
<property name="C" column="C_column" type="java.math.BigDecimal" />
<property name="D" column="D_column" type="java.math.BigDecimal" />
<property name="E" column="E_column" type="java.math.BigDecimal" />
<property name="F" column="F_column" type="int" />
<property name="G" column="G_column" type="java.sql.Timestamp" />
</class>
</hibernate-mapping>
And my code is
List<Map<String, Object>> toBeSaved = getTransformedObjects();// all objects belong to same table
Transaction tx = null;
try (SessionFactory factory = HibernateUtils.getSessionFactoryFromXml(profile, job.getXmlMapping());// Creating Session from XML in my utils class
Session session = factory.openSession()) {
tx = session.beginTransaction();
toBeSaved.forEach(map -> {
session.saveOrUpdate(job.getTableName(), map);
});
tx.commit();// Here exception occurs
} catch (HibernateException e) {
rollbackTransactionAndPrintException(e, destTx);
}
Currently there is only one record in map and content of map is :
{
datetime=2020-07-08 20:19:53.21,
F_column=1,
A_colummn=0.421,
G_column=2020-06-16 17:21:05.0,
code=TYUTQ,
B_column=0.000,
C_column=0.000,
D_column=50.540
}
i am using hibernate version 5.3.5.Final and i am badly stuck in it.
Can anyone tell me how to resolve this ? Thanks in advance.

iBatis Error parsing XPath '/sqlMapConfig/sqlMap'

i'm new to ibatis. i like to split the SQLMapConfig file and SQL map resources into 2 file. but when i run the project its seems to show an error.
so below are my 2 file SQL Map.
SQLMapConfig.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config
2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="32"
maxSessions="10"
maxTransactions="5"
useStatementNamespaces="false"
/>
<transactionManager type="JDBC" >
<dataSource type="DBCP">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/sms_converter"/>
<property name="JDBC.Username" value="root"/>
<property name="JDBC.Password" value=""/>
</dataSource>
</transactionManager>
<sqlMap resource="configuration/db/tbluser.xml" />
</sqlMapConfig>
and here is the SQL Map resources tbluser.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="MUser">
<select id="getUserById" parameterClass = "com.me.bean.MUser" resultClass="com.me.bean.MUser">
SELECT user_id AS sUserId FROM users where user.id = #sUserId#
</select>
</sqlMap>
but after i run the project i've got this error :
Exception in thread "main" java.lang.RuntimeException: Error occurred. Cause: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource configuration/db/tbluser.xml
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:49)
at com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(SqlMapClientBuilder.java:63)
at com.me.dao.DBDao.main(DBDao.java:77)
Caused by: com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource configuration/db/tbluser.xml
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:53)
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(SqlMapConfigParser.java:46)
... 2 more
Caused by: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource configuration/db/tbluser.xml
at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:123)
at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:84)
at com.ibatis.common.xml.NodeletParser.process(NodeletParser.java:102)
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:72)
at com.ibatis.common.xml.NodeletParser.parse(NodeletParser.java:51)
... 3 more
Caused by: java.io.IOException: Could not find resource configuration/db/tbluser.xml
at com.ibatis.common.resources.Resources.getResourceAsStream(Resources.java:110)
at com.ibatis.common.resources.Resources.getResourceAsStream(Resources.java:95)
at com.ibatis.common.resources.Resources.getResourceAsReader(Resources.java:161)
at com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser$10.process(SqlMapConfigParser.java:262)
at com.ibatis.common.xml.NodeletParser.processNodelet(NodeletParser.java:121)
... 7 more
can anyone help me? thanks.
Could not find resource configuration/db/tbluser.xml, so try add prefix classpath:
<mapper resource="classpath:configuration/db/tbluster.xml">
try use
<typeAlias alias="classMUser" type="com.me.bean.MUser" />
<resultMap id="userMap" class="classMUser">
<result property="sUserId" column="sUserId" />
</resultMap>
<select id="getUserById" parameterClass = "com.me.bean.MUser" resultMap="userMap">
SELECT user_id AS sUserId FROM users where user.id = #sUserId#
</select>
In my case, the issue was:
<select id="someId" parameterClass="hashmap"
resultClass="someResultClass" resultMap="someResultMap">
I have written "hashmap" in resultMap instead of "someResultMap".
Correcting this solved the compilation issue.

Insert Stored Procedure mapped with Hibernate

I'm trying based on some examples and Hibernate documentation for mapping a Stored Procedure, I just need to insert some data wich is not for a single table, but I got the message:
Could not parse mapping document from resource
The mapping file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Data">
<id column="col_id" name="dataId">
<generator class="assigned" />
</id>
<property column="col_liq" name="dataLiq" />
<property column="col_td" name="dataTd" />
<property column="col_numdcto" name="dataNumDoc" />
<sql-insert callable="true" check="none">
{call sp_update_data(?,?,?,?)}
</sql-insert>
</class>
</hibernate-mapping>
The "Data" object is just a POJO.
I will appreciate any idea or sugestion.
just to let others to know how it works due finally I did it.
The mapping is correct with just one point, Hibernate will set the Id as the last field, so the procedure should get it in that position, at least that you do some "trick".
In Java when calling the procedure is like a normal save, the rest is like working with a normal entity.

unable to resolve MappingNotFoundException :resource:/login.hbm.xml not found in hibernate

I've been messing around this first hibernate from last few days, after solving going through multiple SO threads
org.hibernate.MappingNotFoundException: resource: *hbm.xml not found
org.hibernate.MappingNotFoundException
org.hibernate.MappingNotFoundException: resource: *hbm.xml not found
i am still unable to figure out how to deal with this
here is my 'hibernate.cfg.xml'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">cijagani</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.1.101/test</property>
<property name="hibernate.connection.username">cijagani</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="/login.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
->Along with this login.hbm.xml
<?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 Jun 28, 2014 6:39:37 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="login" table="LOGIN">
<id name="username" type="java.lang.String">
<column name="USERNAME" />
<generator class="assigned" />
</id>
<property name="password" type="java.lang.String">
<column name="PASSWORD" />
</property>
</class>
</hibernate-mapping>
->along with log output
INFO: HHH000221: Reading mappings from resource: /login.hbm.xml
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: /login.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:738)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2195)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2167)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2147)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2100)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2015)
at StoreData.main(StoreData.java:14)
->StoreData.java is as below
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class StoreData {
public static void main(String[] args) {
//creating configuration object
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
//creating seession factory object
SessionFactory factory= new AnnotationConfiguration().configure().buildSessionFactory();
//creating session object
Session session=factory.openSession();
//creating transaction object
Transaction t=session.beginTransaction();
login e1=new login();
e1.setUsername("vi");
e1.setPassword("8434");
session.persist(e1);//persisting the object
session.getTransaction().commit();//transaction is committed
session.close();
System.out.println("successfully saved");
}
}
any help would be appreciated.
this is the basic steps that need to be followed while developing hibernate application ( am telling you from the basic level)
Steps To Create Hibernate Prog
Create Project
Create package inside the project (This is what I think You have missed (it is mandatory) - Ex: package in.myproject.test)
Create POJO Classes, configuration and hbm files
And at last, write your Java application
Remember
Creating package is must
cfg.configure("hibernate.cfg.xml"); -- we supply configure method, the configuration file name when we give our own name to the configuration file ( Ex - "myConfiguration.cfg.xml" (or) "myConfig.xml" etc).
Even if you supply the default configuration file name i.e "hibernate.cfg.xml", still it is fine (but not recomended)
simply there was error in this
<mapping resource="/login.hbm.xml"></mapping>
i just converted it to this
<mapping resource="login.hbm.xml"></mapping>
and it worked,besides this there were minor errors in field name property of login.hbm.xml which i sorted out by simply changing the names as they were on mysql table name.

What can I do with "Could not parse configuration" error from Hibernate?

I am following Java Hibernate tutorial example from YouTube. Everything looks great until I try to run code which is supposed to create table Employee on Apache Derby server. I tried to use SQL server (2008) first and I was getting the same error.
Could not parse configuration: hibernate.cfg.xml and there is also timeout error. I appreciate any help. Thanks.
Here is the error I get:
17:28:51,574 INFO Version:15 - Hibernate Annotations 3.4.0.GA
17:28:51,587 INFO Environment:560 - Hibernate 3.3.2.GA
17:28:51,590 INFO Environment:593 - hibernate.properties not found
17:28:51,594 INFO Environment:771 - Bytecode provider name : javassist
17:28:51,597 INFO Environment:652 - using JDK 1.4 java.sql.Timestamp handling
17:28:51,648 INFO Version:14 - Hibernate Commons Annotations 3.1.0.GA
17:28:51,655 INFO Configuration:1474 - configuring from resource: hibernate.cfg.xml
17:28:51,655 INFO Configuration:1451 - Configuration resource: hibernate.cfg.xml
17:28:51,702 DEBUG DTDEntityResolver:64 - trying to resolve system-id [http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd]
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1035)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1017)
at com.hibernate.chapter1.TestEmployee.main(TestEmployee.java:14)
Caused by: org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1532)
... 5 more
Here is my hibernate.cfg.xml file:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="connection.url">jdbc:derby://localhost:1527/HibernateDb;create=true</property>
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">2</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- Enable Hibernate's current session context -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
And, here is the code I am running:
package com.hibernate.chapter1;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class TestEmployee {
public static void main(String[] args) {
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass(Employee.class);
config.configure("hibernate.cfg.xml");
new SchemaExport(config).create(true, true);
}
}
What did I do wrong?
This means the hibernate.dtd cannot be resolved - its resolution is attempted on the server. The dtd is contained in the jars files - see here and here for how to resolve it.

Categories

Resources