• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Native SQL in Mapping File For a Property

 
jon jackson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read through the Hibernate Introduction chapter today (ok - I skimmed throught it) but couldnt find any answers to my questions there, nor in the FAQ. Im a newcomer to Hibernate and trying to finish off what someone started
I have a mapping file as below for a User (a bit stripped down for clarity).




User.hbm.xml
----------------


<class name="com.myApp.user.User" table="USER">

<id name="id" type="java.lang.Long">
<column name="ID" />
<generator class="native" />
</id>

<many-to-one name="domain" class="com.myApp.person.Domain" fetch="join">
<column name="DOMAIN_ID" not-null="true" />
</many-to-one>

<property name="emailAddress" type="java.lang.String">
<column name="EMAIL_ADDRESS" length="80" not-null="true" />
</property>

<property name="password" type="java.lang.String">
<column name="PASSWORD" length="25" not-null="true" />
</property>

<set name="userAddresses" inverse="true" cascade="all">
<key>
<column name="USER_ID" not-null="true" />
</key>
<one-to-many class="com.myApp.user.UserAddress" />
</set>

PROBLEM
--------
My problem lies with the password field. I have an existing database of users who had their passwords encrypted using the mySql encode() function. So basically I wish to write a bit of SQL to run MYsQL'S decode() function for the password fetching.

Is there a way of instructing hibernate to run the native MySQL function for the password field, or do I have to write the whole SELECT in the mapping file?



The DAO is making use of a Criteria implementation:

get() method
---------------

Criteria criteria = getSession().createCriteria(type);
criteria.add(Restrictions.eq(MappingConstants.EMAIL_ADDRESS, emailAddress));
createCriteria(criteria, MappingConstants.DOMAIN, MappingConstants.CODE, domainCode);
List<T> dataObjList = criteria.list();



Any suggestions as to where to find the information I need is greatly appreciated from this newcomer.


Jon
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using a column field like

http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-column

look at the "default" attribute? That might work, but how do you go the other way?

Oh, the other thing you always have the option to do is to overwrite the default select, insert, and delete statements for an Entity class. So here is the real way to do what you want to do

http://www.hibernate.org/hib_docs/v3/reference/en/html/querysql.html#querysql-cud

<sql-insert>
<sql-update>

And right below that in the next section is for changing the Select statement.

Good Luck

Mark
 
jon jackson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi mark


Thanks for your posting - this is proving quite a difficult task for what I had originally assumed would be a common requirement with an easy implementation.

The more hibernate forums Im reading the more I read comments about people being disappointed with it.

Will let you know how I resolve this one.

Thanks for your time.
Jon
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic