Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Native SQL in Mapping File For a Property  RSS feed

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).


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

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

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

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

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

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

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.

Posts: 17344
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using a column field like

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


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

Good Luck

jon jackson
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.
You got style baby! More than this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!