Rakesh
Rakesh

Reputation: 45

Why do we need to write a native sql query in a hbm.xml rather than in a string of java file?

Lets say for example I have the following query (simple one but I know we use native sql queries only when we have complex ones which cannot be written as named queries)

String query = "select top 1 id from comp_job_processing_cycle order by start_date desc"

Now I was told that we are supposed to write these native sql queries in somefile.hbm.xml file rather than the way I have written in a java file. Why is that?

Upvotes: 0

Views: 263

Answers (1)

Suresh Atta
Suresh Atta

Reputation: 122006

My reasons are

  • Global access to that query. If you write inline query, you may need to copy paste.
  • Your code looks beautiful. Those big big queries in source code looks ugly.
  • Maintenance will be easy. If you scatter your queries everywhere in code, later it's hard to maintain them.

And I usually place respective queries in respective bean mapping file.

Upvotes: 1

Related Questions