These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Calling sql script from hibernate3 does not work.
PostPosted: Fri Sep 11, 2009 6:22 am 
Newbie

Joined: Fri Sep 11, 2009 5:18 am
Posts: 1
Hello,

I have a sql function uGetSeq() which returns an incremental alphanumeric value. I want to call it from my web application which is based on Hibernate3 and Struts.

I created a NamedQueries.hbm.xml and I put it into the place where all other hbm.xml files are located too.
NamedQueries.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="de.laliluna.library" >
<sql-query name="selectPaymentRefId" callable="true">
<return alias="ref" class="Payment">
<return-property name="referenceNo" column="REF_NO"/>
</return>
{ ? = call uGetSeq() }
</sql-query>
</hibernate-mapping>

Then I configured my hibernate.config.xml accordingly:

<?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>
<!-- mysql configuration -->
<property name="connection.url">jdbc:mysql://localhost/LibraryWeb</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<property name="connection.autocommit">false</property>
<property name="show_sql">true</property>

<!-- mapping files -->
<mapping resource="de/laliluna/library/Invoice.hbm.xml" />
<mapping resource="de/laliluna/library/InvoiceItem.hbm.xml" />
<mapping resource="de/laliluna/library/Payment.hbm.xml" />
<mapping resource="de/laliluna/library/User.hbm.xml" />
<mapping resource="de/laliluna/library/Account.hbm.xml" />
<mapping resource="de/laliluna/library/NamedQueries.hbm.xml" />
</session-factory>
</hibernate-configuration>

Then I called the sql script within application code (please see below for the whole code segment):

String str = (session.createSQLQuery("selectPaymentRefId")).toString();

But it does not work, in the corresponding GUI field, "SQLQueryImpl(selectPaymentRefId)"
is displayed and not a "T000001" is displayed as expected. Do you have any suggestions for me?
Thanks.

public void savePayment(Payment paymentValue) {
/* a Hibernate session */
Session session = null;
/* we always need a transaction */
Transaction tx = null;

/* get session of the current thread */
session = HibernateSessionFactory.currentSession();

int userId=getCurrentUserId();
User user = (User) session.get(User.class, userId);
Payment payment;

tx = session.beginTransaction();
if (paymentValue.getId() == null ||paymentValue.getId().intValue() == 0) {
// create payment
paymentValue.setUserId(userId);
paymentValue.setUser(user);

String str = (session.createSQLQuery("selectPaymentRefId")).toString();

paymentValue.setReferenceNo(str);

session.save(paymentValue);

}

tx.commit();
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.