-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: how to execute SQLFunction ?
PostPosted: Thu Apr 06, 2006 6:35 am 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
I want to persist an object in database. So using save in session would work. But the problem is that I want to set one of the attribute of the object based on the result of execution of an SQL function.

Remember that Hibernate wraps sql functions of a DB as class org.hibernate.dialect.function.SQLFunction and sets it into the java.util.Map in Dialect class of a database.

I can use this map and get sqlfunction instance of the inbuilt function, but can i execute this sqlfunction and set the attribute in my object with the return value of sqlfunction ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 12:41 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That sounds way over the top. Can't you use use the sql-insert and sql-update elements of the class element to customize how hibernate saves your objects? Something like
Code:
<class name="X" ...>
  <id .../>
  <property name="Prop1" .../>
  <property name="Prop2" .../>
  <property name="Derived" insert="false" update="false"/>
  ...

  <sql-insert>insert into table (id, col1, col2, col3) values (?, ?, ?, udfYourFunc())</sql-insert>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 12:49 am 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
do <sql-update/insert> take native database sql ?

I could have but the object has lot of properties and collections inside it and uses dynamic update/insert, so writing them inside <sql-update/insert> might not be right ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 12:59 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Yes, it's native SQL. It can be clunky. But if the function only needs to be run at insert-time, then it's not too bad, seeing as there's no such thing as dynamic insert. If the column needs to be derived on every update too, then perhaps a trigger is what you need?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 1:05 am 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
Let me tell you the problem straight forward...

I want to insert the 'current timestamp' into a 'column in my table' but this timestamp has to be the time at database server (like the one returned by SYSDATE / CURRENT_TIMESTAMP at database server.

But whenever hibernate is binding a value to that parameter it gets the value from the m/c where hibernate code is running (which could be a middleware). And this isnt acceptable, so getting a value from an sql function (which is database independent CURRENT_TIMESTAMP) binding that value to the parameter would be most correct option. But I am not able to fetch this current timestamp from server m/c !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 1:08 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Most DBMSs have a column type that does this. The hibernate <version> tag gives you access to it. If you change your mapping to include that tag, you should get what you want. It's described in section 5.1.7 of the ref docs.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 2:26 am 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
Thanx ... though i didnt try <version tag> (Hib ref 3.1.9)... <timestamp ... source="db" /> (Hib ref 3.1.9) worked fine .. Just one more thing I needed was the generated attribute.
I need generated = "insert" .. but that isnt supported by <version> or <timestamp> only by <property > ... but eclipse install on my m/c is giving error for 'generated' attribute for <property> (Hib ref 3.1.9). Says 'generated' attribute isnt declared !

I am using :
Code:
        <property name="effectiveTime" column="EFFECTIVE_TIME" type="dbtimestamp" generated="insert" /> 


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 09, 2006 5:39 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Then you must have an older version of the dtd somewhere, as the "generated" attribute is fairly new (3.1 and later, iirc). The 4th parameter to the doctype element at the top of your mapping files must be exactly "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd". It's strange, but hibernate doesn't use the system parameter, just the public one. Anyway, you need that exact string, then hibernate looks in your jar/classpath: make sure that there are no hibernate-mapping-3.0.dtd files anywhere on your filesystem, no old jars lying around, and that the dtd file in your hibernate3.jar contains this line:
Code:
<!ATTLIST property generated (never|insert|always) "never">
If you have all of that and it still doesn't work, then you'll have to do some serious investigation, because it should work.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 1:18 am 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
Code:
<!DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration.                   -->



I am using these plugins for hibernate mapping generation...

Code:
D:\e\eclipse\plugins\org.hibernate.eclipse_3.1.0.beta4
D:\e\eclipse\plugins\com.genuitec.myeclipse.hibernate_4.0.0

D:\e\eclipse\plugins\com.genuitec.eclipse.hibernate_4.0.0            ---> This is where the files of plugin are stored. I have check the hibernate3.jar file in this directory and hibernate-mapping-3.0.dtd file has 'generated' attribute for <property>.



Classpath has hibernate3.jar file from the lib folder of my app. That also has correct hibernate-mapping-3.0.dtd file.

BUT one ibm plugin folder has 3 dtd's ->
D:\e\eclipse\plugins\com.ibm.webtooling.system.dtds_14.0.0\dtds
(hibernate-mapping-1.1.dtd,hibernate-mapping-2.0.dtd,hibernate-mapping-3.0.dtd)
This plugin is also loaded at startup.

But I guess that only the dtd from hibernate3.jar file in my classpath should be loaded !

Any Help !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 1:33 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The DTD is loaded using Class.getResourceAsStream (iirc). So what matters is the classpath order. The class loader just starts at the beginning and goes through each path in your classpath, looking for the DTD. If the first one it finds is in the jar, then well and good, but if the first one it finds is (e.g.) in your WEB_INF/classes directory, then that's the one it loads.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 2:15 am 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
My Classpath order :
Apart from this hibernate3.jar is much above the other jars in classpath and I have put hibernate-mapping-3.0.dtd in config folder also (which is also in classpath) ....


Code:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
   <classpathentry kind="src" path="src"/>
   <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
   <classpathentry exported="true" kind="lib" path="config"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/JdbcDrivers/mysql/mysql-connector-java-3.1.6-bin.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/JdbcDrivers/oracle/classes12.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/hibernate3.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/pdm.jar"/>
   <!--<classpathentry kind="con" path="com.omondo.uml.CONTAINER"/>-->
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/activation.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/asm.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/asm-attrs.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/c3p0-0.8.5.2.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/cglib-2.1.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/cleanimports.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/common.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/commons-collections.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/commons-collections-2.1.1.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/commons-dbcp.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/commons-logging-1.0.4.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/commons-pool.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/concurrent-1.3.2.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/concurrent-1.3.3.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/connector.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/dom4j-1.6.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/eg.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/ehcache-1.1.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/fcesupport.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/gui.jar"/>

   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/hl7.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/iengine.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/jaas.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/jacc-1_0-fr.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/jaxen-1.1-beta-4.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/jconn2.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/jdbc2_0-stdext.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/jgroups-2.2.7.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/jmxremote.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/jmxremote_optional.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/jmxri.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/jmxtools.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/jta.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/junit.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/log4j-1.2.7.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/log4j-1.2.9.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/logging.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/mail.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/mx4j-tools.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/ojdbc14.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/oscache-2.1.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/postgresql-8.0-311.jdbc2.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/postgresql-8.0-311.jdbc3.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/proxool-0.8.3.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/rcm.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/rumba.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/sapdbc-7.4.03.17.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/swarmcache-1.0rc2.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/velocity-1.4.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/wrapper.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/xerces-2.6.2.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/xml-apis.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/xmlrpc-1.2-b1.jar"/>
   <classpathentry kind="lib" path="conf"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/antlr-2.7.6rc1.jar"/>
   <classpathentry kind="lib" path="C:/guidepost_2.0/eclipse/workspace/CCG/lib/ant-antlr-1.6.5.jar"/>
   <classpathentry kind="output" path="bin"/>
</classpath>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 9:30 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I don't know what that file is. Is it something to do with eclipse, maybe? Whatever it is, the system classpath (in your environment variables) will still be used in addition to whatever classpath you set up elsewhere, so you might want to go through that too, looking for old DTDs.

I see that you have some classpathentry items referencing new and old versions of the same jar (log4j, concurrent). It won't be causing this error, but it's not right, and may cause other problems later. You should remove the old entries.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 12:42 am 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
Yes it is Eclipse 3.1 (guidepost 2.0)
The file is the ".classpath" file that exists in the folder where the project is created along with the ".project" file.


one ibm plugin folder has 3 dtd's ->
D:\e\eclipse\plugins\com.ibm.webtooling.system.dtds_14.0.0\dtds
(hibernate-mapping-1.1.dtd,hibernate-mapping-2.0.dtd,hibernate-mapping-3.0.dtd)
This plugin is also loaded at startup.

But I dont understand why older DTD's would be loaded, as I have specified in my XML doctype that it references 3.0 dtd !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 12:59 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The 3.0 DTD has changed a lot since the original release. Including the generated="???" attribute: 3.0.5 doesn't have this. It's not the 2.0 or 1.1 mapping that's ruining things for you, it's older versions of the 3.0 one.

I think they only bother changing the name when backwards compatibility is lost. Adding new allowable attributes doesn't prevent old mapping files from working, so they don't renumber the DTDs.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 2:13 am 
Regular
Regular

Joined: Wed Mar 08, 2006 2:07 am
Posts: 50
Location: Bangalore
Thanx 'tenwit'

3.0.5 was the problem. Actually the hibernate hbm validator for myeclipse plugin was loooking for a 3.0 mapping file and when I compared this 3.0 mapping file with the one in hibernate 3.1.2 jar, Then i saw glaring dissimilarities. Lots of attributes were added in the new mapping file ! I referenced to it and now it works fine ...

Thankx for your time.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.