JavierElBene
JavierElBene

Reputation: 43

How define and setting parameters in Hibernate Query Language (HQL)?

I need to know what i have to write inside the text of an Hibernate Query Language query to define a parameter and then how i assign a value to this parameter. For example, look at this code:

nhSession
.CreateQuery("INSERT INTO historiccard SELECT * FROM card WHERE game_id={0}")
.SetParameter(0, lobby.getPK());

What i have to write after game_id=?. After that, is that right the way in that i'm assigning the value to the parameter?

Upvotes: 0

Views: 1177

Answers (1)

Rippo
Rippo

Reputation: 22424

One way:-

nhSession
.CreateQuery("INSERT INTO historiccard SELECT * FROM card WHERE game_id=:id")
.SetInt32("id", lobby.getPK());

You don't need to add single quotes etc for strings, dates, etc, just use a :varname

You can assign values using

.SetDateTime("varname", DateTime.Now)
.SetInt64("varname", 12345678)
.SetString("varname", "rippo")

You can also chain these together.

Upvotes: 1

Related Questions