ranjani ra

Greenhorn
+ Follow
since Feb 13, 2013
ranjani likes ...
Hibernate Oracle Java
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by ranjani ra

How to validate the syntax of a SQL query without executing it using JDBC?

I know about the 'set noeexec on' in Sybase, but I want to use a similar functionality in JDBC call.

The command 'set noexec on' in Sybase works if used in a env like Rapid SQL. However I want to use it with Prepared statements.

[code=java]String sqlValue = "select * from table_name";
Connection conn = ConnectionPool.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sqlValue);
boolean queryValidityResult = pstmt.execute();[/code]
Is it right to use the 'set noexec on' like this?

[code=java]String sqlValue = "set noexec on " + "select * from table_name";
Connection conn = ConnectionPool.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sqlValue);
boolean queryValidityResult = pstmt.execute();[/code]
Or is there any setter property on PreparedStatement which would help?

Or is there any other way by which I can just validate my SQL instead of executing it?

Any ideas?
Hi all,

I have a sample hibernate application using annotations and it runs fine. To call stored procedures, my pl says it s better to use <classname>.hbm.xml instead of annotations. However when I try to use the xml file for mapping column names to pojo elements, I am getting an exception:
Could not parse mapping document from resource Dsl_member.hbm.xml
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Dsl_member.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:602)
at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:957)
at org.hibernate.cfg.AnnotationConfiguration.parseMappingElement(AnnotationConfiguration.java:629)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1589)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1568)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:1047)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:64)
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:1462)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1017)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1448)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1011)
at com.TestConnectCCR.main(TestConnectCCR.java:23)
Caused by: org.hibernate.MappingException: Could not parse mapping document in input stream
at org.hibernate.cfg.AnnotationConfiguration.addInputStream(AnnotationConfiguration.java:783)
at org.hibernate.cfg.AnnotationConfiguration.addInputStream(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:599)
... 15 more
Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.AnnotationConfiguration.addInputStream(AnnotationConfiguration.java:773)
... 17 more
java.lang.NullPointerException
at com.TestConnectCCR.main(TestConnectCCR.java:50)

Table: CREATE TABLE DSL_MEMBER(ID INT NOT NULL, FIRST_NAME VARCHAR2(20), LAST_NAME VARCHAR2(20), STATUS VARCHAR2(2), SALARY INT); ID is the primary key.

Given below is the POJO Dsl_member with annotated table and column names
package com;

import java.io.Serializable;
import java.util.Date;

public class Dsl_member implements Serializable{

private int id;
private String firstName;
private String lastName;
private String status;
private int salary;
//getters and setters, cosntructors

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="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<!-- url, userid and pwd mappings for my db -->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<mapping class="com.Dsl_member"></mapping>
</session-factory>
</hibernate-configuration>

Dsl_member.hbm.xml file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="Dsl_member" table="DSL_MEMBER">
<meta attribute="class-description">
This class contains the dsl_member detail.
</meta>
<id name="id" type="int" column="ID">
</id>
<property name="firstName" column="FIRST_NAME" type="String"/>
<property name="lastName" column="LAST_NAME" type="String"/>
<property name="status" column="STATUS" type="String"/>
<property name="salary" column="SALARY" type="int"/>
</class>
</hibernate-mapping>

Test class:
Session session = null;
try{
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Transaction tx = null;
tx = session.beginTransaction();

//Create new instance of Contact and set

List<Dsl_member> userList=new ArrayList<Dsl_member>();
//select values from DSL_MEMBER table
userList=session.createQuery("from Dsl_member").list();
for(Dsl_member userEntity:userList){
//Print the values
}



Hi all,

I am new to hibernate. I need to call a stored procedure from hibernate. But I am unable to call the stored proc. Given below is the code.

Table: CREATE TABLE DSL_MEMBER(ID INT NOT NULL, FIRST_NAME VARCHAR2(20), LAST_NAME VARCHAR2(20), STATUS VARCHAR2(2), SALARY INT); ID is the primary key.

Given below is the POJO Dsl_member with annotated table and column names
package com;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.NamedNativeQuery;

@Entity
@Table(name="DSL_MEMBER")
public class Dsl_member implements Serializable{

@Id
@Column(name="ID")
private int id;

@Column(name="FIRST_NAME")
private String firstName;

@Column(name="LAST_NAME")
private String lastName;

@Column(name="STATUS")
private String status;

@Column(name="SALARY")
private int salary;
//getters and setters, cosntructors

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="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<!-- url, userid and pwd mappings for my db -->
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">false</property>
<mapping class="com.Dsl_member"></mapping>
</session-factory>
</hibernate-configuration>

Stored proc that I have to access:
create or replace procedure DSL_SELECTALL_MEMBER( p_cursor out SYS_REFCURSOR) as
begin
open p_cursor for select mem.first_name, mem.last_name, mem.salary from dsl_member mem;
end;

Test class:
In JDBC, I know that I can call this proc as below:
CallableStatement cs = conn.prepareCall("{call dsl_selectall_member(?)}");
cs.registerOutParameter(1,OracleTypes.CURSOR);
rs = cs.executeQuery();

But in hibernate, is there an equivalent of cs.registeroutparamater??

When I try to call the above proc as
Query query = session.createSQLQuery("call dsl_selectall_member(?)").addEntity(Dsl_member.class).setParameter("?",OracleTypes.CURSOR);
List result = query.list();

When I try calling like above, I get the below exception:
could not locate named parameter [?]
org.hibernate.QueryParameterException: could not locate named parameter [?]

Is there any other way to call a stored proc from hibernate?