NHibernate - Lazy-Loading primitive type

I use NHibernate to load some objects into my ASP.NET MVC application.

For example, the view is submitted by the user, and I want to display the username (but only the username) of the user, for example:

<%= Html.Encode(item.User.UserName) %>

When I load a dispatch using NHibernate, the user is loaded from the database, which means that the actual SQL query (to load the user information) will be generated and executed only when the specified line of code is called (which is what I want).

The problem is that the SQL query also selects other user information, such as password, email, etc. This information is obviously not needed and is discarded.

The SQL query is as follows:

SELECT id, username, password, email FROM User WHERE Id = 1;

I conclude that NHibernate only uses lazy references to other objects that map to tables in my database. This is not like lazy loading of basic, primitive types like strings, ints, etc.

Can I do it? When the above line is selected, I would like the SQL query to look something like this:

SELECT username FROM User WHERE Id = 1;

Is it possible? How?

+3
source share
2 answers

Is there a reason why you do not want to download the complete object? Except in rare cases, there is no real difference in performance.

, , , , . , User : User UserProfile ( ..), UserProfile .

+1

resultTransformer , ( , DTO, )

http://docs.jboss.org/hibernate/stable/core/reference/en/html/querysql.html ( Hibernate, NHibernate, ), 16.1.5

0

Source: https://habr.com/ru/post/1729111/


All Articles