Tôi mới dùng NHibernate và tôi đang cố gắng tìm hiểu cách truy vấn dữ liệu của mình.Truy vấn với NHibernate
Dưới đây là cấu hình xml. Chỉ có công thức được hiển thị.
Tôi muốn có thể truy vấn công thức nấu ăn theo đối tượng từ các từ khóa được nhập và cũng là thành phần từ tên thành phần.
Vì vậy, bạn có thể nhập "rượu pasta" chẳng hạn.
Đây là những gì tôi đã thử nhưng cho tôi lỗi.
hql = "from Recipe r " +
"left join r.Images " +
"inner join r.User " +
"inner join r.Ingredients i " +
"where i.IngredientName Like '%pasta%' OR i.IngredientName Like '%wine%' OR r.RecipeTitle Like '%pasta' OR r.RecipeTitle Like '%wine%'";
Tôi cũng muốn tải các bộ sưu tập.
Tôi có đang truy vấn đúng không ?? Tôi cần có khả năng tạo chuỗi truy vấn từ tiêu chí tìm kiếm của mình. Điều này sẽ dễ dàng hình thành tôi trong SQL.
Malcolm
<class name="Recipe" table="Recipes" xmlns="urn:nhibernate-mapping-2.2">
<id name="RecipeID" type="Int32" column="RecipeID">
<generator class="identity" />
</id>
<property name="RecipeTitle" type="String">
<column name="RecipeTitle" />
</property>
<property name="Completed" type="Boolean">
<column name="Completed" />
</property>
<property name="ModifiedOn" type="DateTime">
<column name="ModifiedOn" />
</property>
<property name="Rating" type="Double">
<column name="Rating" />
</property>
<property name="PrepTime" type="Int32">
<column name="PrepTime" />
</property>
<property name="CookTime" type="Int32">
<column name="CookTime" />
</property>
<property name="Method" type="String">
<column name="Method" />
</property>
<bag name="Images" inverse="true" cascade="all">
<key column="RecipeID" />
<one-to-many class="OurRecipes.Domain.RecipeImage, OurRecipes.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
<many-to-one name="Category" column="CategoryID" />
<bag name="Comments" inverse="true" cascade="all">
<key column="RecipeID" />
<one-to-many class="OurRecipes.Domain.Comment, OurRecipes.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
<many-to-one name="User" column="EnteredByID" />
<bag name="Ingredients" inverse="true" cascade="all">
<key column="RecipeID" />
<one-to-many class="OurRecipes.Domain.Ingredient, OurRecipes.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
</class>
Để giải quyết vấn đề này, bạn có thể lấy DistinctRootEntityResultTransformer –
Tại sao bạn sử dụng FetchMode.Join thay vì FetchMode.Eager cho những tải này nếu bạn thực sự có ý định sử dụng các đối tượng con? –
Tôi không thấy nó hoạt động như thế nào khi bạn đã mã hóa từ khóa.Làm thế nào bạn sẽ truy vấn nếu tôi đã cho bạn một chuỗi các từ tách ra bởi không gian ??? – Malcolm