I have verified that the object is not getting cached when using a Session.CreateCriteria
Here is the mapping file of the class Task:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="dbo" default-cascade="save-update">
<class name="CID.IPath.NewBusinessLayer.Task, CID.IPath.NewBusinessLayer" table="Task">
<cache usage="read-write" />
<id name="Id" type="Int32" column="TaskId">
<generator class="identity" />
</id>
<bag name="roles" table="role_task_association" inverse="true" lazy="true">
<cache usage="read-write" />
<key column="task_id" />
<many-to-many class="CID.IPath.NewBusinessLayer.claimuserrole, CID.IPath.NewBusinessLayer" column="role_id" />
</bag>
<property name="TaskDescription" column="TaskDescription" type="String" />
<property name="TaskName" column="TaskName" type="String" />
</class>
</hibernate-mapping>
Here is some test code I wrote in ASP.Net:
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim task As Task = task.Get(GetType(Task), 4)
Label1.Text = task.TaskDescription
End Sub
When I hit Refresh a couple of times, I only see one SQL statement executed in the SQL profiler:
Code:
exec sp_executesql N'SELECT task0_.TaskId as TaskId0_, task0_.TaskName as TaskName0_, task0_.TaskDescription as TaskDesc2_0_ FROM dbo.Task task0_ WHERE task0_.TaskId=@p0', N'@p0 int', @p0 = 4
Whereas if I change the code to something like this:
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim task As Task = task.Get(GetType(Task), "TaskName", "Claim Editing")
Label1.Text = task.TaskDescription
End Sub
which underneath is using Session.CreateCriteria, I see the following statement executed every single time I hit Refresh:
Code:
exec sp_executesql N'SELECT this.TaskId as TaskId0_, this.TaskName as TaskName0_, this.TaskDescription as TaskDesc2_0_ FROM dbo.Task this WHERE this.TaskName = @p0', N'@p0 nvarchar(4000)', @p0 = N'Claim Editing'
What do you think?