-->
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.  [ 4 posts ] 
Author Message
 Post subject: select this + ',' + csv + ',' + string from table
PostPosted: Mon Jul 10, 2006 2:21 pm 
Newbie

Joined: Wed Mar 22, 2006 6:30 am
Posts: 9
can you do this type of concatenation in a select statement using hql/sql? using session.createSQLQuery() for example?

i can't figure out how to get a hibernate query to run and return a list of strings, which don't map to any tables/hbm/classes.... ....in my example, the tables are all mapped, but the select statement is trying to create a new string which does not exist in the database....

this idiom of native sql would be great to use in my current program. if hibernate does not support it, ok...

tom


Hibernate version:
2.1


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 10, 2006 3:46 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
How about this:

Code:
    public String getString() {
        Session session = getHibernateTemplate().getSessionFactory().openSession();
        SQLQuery query = session.createSQLQuery("select 'This ' || 'is ' || 'a ' || 'string.' as mystring from dual");
        query.addScalar("mystring", Hibernate.STRING);
        return (String) query.uniqueResult();
    }


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 10, 2006 5:03 pm 
Newbie

Joined: Wed Mar 22, 2006 6:30 am
Posts: 9
wow... that would too cool if i could make it work...

i am stuck with hib 2 - no SQLQuery object, and session.createSQLQuery looks like this:

Query createSQLQuery(String sql, String[] returnAliases, Class[] returnClasses)

and

Query createSQLQuery(String sql, String returnAlias, Class returnClass)


if i invoke the function like this:
Query query = session.createSQLQuery("select 'This ' || 'is ' || 'a ' || 'string.' as mystring from dual", "csv", String.class);


i get this error:
[junit] net.sf.hibernate.MappingException: No persister for: java.lang.String
[junit] at net.sf.hibernate.impl.SessionFactoryImpl.getPersister(SessionFactoryImpl.java:344)
[junit] at net.sf.hibernate.impl.SessionImpl.getClassPersister(SessionImpl.java:2656)
[junit] at net.sf.hibernate.impl.SessionImpl.getSQLLoadable(SessionImpl.java:3775)
[junit] at net.sf.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:3754)
[junit] at net.sf.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:52)
[junit] at net.sf.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:527)
[junit] at com.sabrix.reports.managers.AuditExtractManager.getAuditExtractCsv(AuditExtractManager.java:212)
[junit] at com.sabrix.reports.managers.TestAuditExtractManager.testGetAuditExtractCsv(TestAuditExtractManager.java:232)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[junit] at java.lang.reflect.Method.invoke(Method.java:324)
[junit] at junit.framework.TestCase.runTest(TestCase.java:154)
[junit] at junit.framework.TestCase.runBare(TestCase.java:127)
[junit] at junit.framework.TestResult$1.protect(TestResult.java:106)
[junit] at junit.framework.TestResult.runProtected(TestResult.java:124)
[junit] at junit.framework.TestResult.run(TestResult.java:109)
[junit] at junit.framework.TestCase.run(TestCase.java:118)
[junit] at junit.framework.TestSuite.runTest(TestSuite.java:208)
[junit] at junit.framework.TestSuite.run(TestSuite.java:203)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:297)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeInVM(JUnitTask.java:1072)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:682)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1434)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:632)
[junit] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
[junit] at org.apache.tools.ant.Task.perform(Task.java:364)
[junit] at org.apache.tools.ant.Target.execute(Target.java:341)
[junit] at org.apache.tools.ant.Target.performTasks(Target.java:369)
[junit] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
[junit] at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
[junit] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
[junit] at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
[junit] at org.apache.tools.ant.Main.runBuild(Main.java:668)
[junit] at org.apache.tools.ant.Main.startAnt(Main.java:187)
[junit] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
[junit] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)


fyi there is no addScalar() on Query object as of hib 2...
//query.addScalar("mystring", Hibernate.STRING);


thank you so - so much,

tom


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 10, 2006 5:41 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
That's not good at all. I'm definitely not an expert, but you might be down to using

Code:
session.connection();


and issuing straight JDBC...

Good Luck!


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

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.