ORM - creating an object with associations

One object related to many others:

Example: one Post

is part of one Blog

, has a list of relationships with, Tags

and more ...

Then I have one web form with dropdowns, each populated from a custom request to get only the id and name of that object, I don't get ALL of the object because it might have large size values ​​(don't ask me why, but it can happen) and also because I don't need other attributes to create the dropdown.

Now when I try to create this new Post

one I need to do something like this:

Post post = new Post();
post.Blog = blog;
.....

      

Before ORM I could use SQL queries to create an object and only pass the blog id , but now I need to pass the blog object. This means I will need to fetch it from the database just to create a post, and this post needs object tags and other things as well. I think this is an extra operation. Why do I need all other objects to create something else?

There is a similar question here , but without an accepted answer.

I will also not cache objects from dropdowns for the reasons I explained earlier.

I would like to create this Post

messaging only Id

Blog

. Is there a way to do this?

...

Note : using NHibernate, but I think this is a general ORM question.

+2


source to share


4 answers


You can use projection classes. Getting only the information you need, not the complete DB record.

One example:



HqlBasedQuery query = new HqlBasedQuery(typeof(Post),
        @"
        SELECT tags.Id, tags.Name
        FROM Post post
        INNER JOIN post.Tags tags
        WHERE post.Id = ?
        ORDER BY tags.Name
        ", postId);
        var results = from object[] summary in (ArrayList)ActiveRecordMediator.ExecuteQuery(query)
                      select new YourProjectionClass
                      {
                          Id = (int)summary[0],
                          Name= (string)summary[1],
                      };
        return results.ToList<YourProjectionClass>();

      

+1


source


In the ORM, you can do what you want:
create a post using only a partially filled blog.

This is very common code:

    Post post = new Post();
    post.setBlog(new Blog(123));

      



If used many times, there is an obvious way to make this code smaller. I leave this to you as an exercise; -)


Note that this causes other problems. For example, when you work with a block, is it fully initialized?

0


source


Right: if the ORM supports lazy fetching (hibernate), sess.load (class, id) doesn't actually generate the SQL query, but returns an uninitialized proxy.

0


source


For Hibernate (not NHibernate, but it should be similar) I believe you can usually use HQL queries to load objects:

select id from Blog where ...

      

You can load this into a Blog object and it will work transparently. If you then access any of the properties that you haven't loaded, it will raise a request for the rest of its content (or a LazyInitializationException if you pass the object outside of its session).

Collections and associations are usually lazy loaded as well. The situation for simple parameter types is a little more complicated and I don't think there is an easy way to do it. If you find yourself displaying huge chunks of data (i.e. CLOBs or BLOBs), you probably want to cheat a bit and map it using an association.

In any case, just because you have an ORM framework doing the conversion for you does not mean that you are freed from all the small details about how to load objects. In fact, getting the data access layer is one of the most difficult tasks. Especially when you are working with a legacy database that is not suitable for Hibernate, stick to its simple principles.

0


source







All Articles