NHibernate - join without matching

Is it possible to combine two classes without a specific comparison between them (using the API criteria)?

I have to join the two classes and get the data from both, but I cannot match them. I only know the SomeID foreign key in the first class and the primary key identifier in the second.

How to create criteria for joining them? Is this possible without display?

Please help, I really need it, but I'm stuck.: /

PS

I know “any” mapping, but I have 10 fields like SomeID. Creating any mappings for 10 fields just to create joins is redundant. If there is no other resolution, I will do it, but I do not want to.

+3
source share
2 answers

I do not know the criteria version, but in HQL you can do it like this:

select customer, order from Customer customer, Order order 
    where order.CustomerID = customer.Id
    and customer.Id = 42

Then the result set will be a list of the object [], where the client will be repeated with the number of orders that he placed (provided that, of course, there is one client for many orders).

Please note that the result will be empty if there are no orders.

+6
source

If you do not want or cannot define association properties in your objects (for example, in modular applications that support dynamic loading of plugins), you can still create “fake” associations using the Fluent api.

. Orchard, "ContentItemAlteration". Orchard ContentItem ContentPart, . ContentPart , , (, , ) ContentItem .

, Orchard:

/// <summary>
    /// Add a "fake" column to the automapping record so that the column can be
    /// referenced when building joins accross content item record tables.
    /// <typeparam name="TItemRecord">Either ContentItemRecord or ContentItemVersionRecord</typeparam>
    /// <typeparam name="TPartRecord">A part record (deriving from TItemRecord)</typeparam>
    /// </summary>
    class Alteration<TItemRecord, TPartRecord> : IAlteration<TItemRecord> {
        public void Override(AutoMapping<TItemRecord> mapping) {

            // public TPartRecord TPartRecord {get;set;}
            var name = typeof(TPartRecord).Name;
            var dynamicMethod = new DynamicMethod(name, typeof(TPartRecord), null, typeof(TItemRecord));
            var syntheticMethod = new SyntheticMethodInfo(dynamicMethod, typeof(TItemRecord));
            var syntheticProperty = new SyntheticPropertyInfo(syntheticMethod);

            // record => record.TPartRecord
            var parameter = Expression.Parameter(typeof(TItemRecord), "record");
            var syntheticExpression = (Expression<Func<TItemRecord, TPartRecord>>)Expression.Lambda(
                typeof(Func<TItemRecord, TPartRecord>),
                Expression.Property(parameter, syntheticProperty),
                parameter);

            mapping.References(syntheticExpression)
                .Access.NoOp()
                .Column("Id")
                .ForeignKey("none") // prevent foreign key constraint from ContentItem(Version)Record to TPartRecord
                .Unique()
                .Not.Insert()
                .Not.Update()
                .Cascade.All();
        }
    }

"part" ContentItem, . , , "ProductPart", "ProductPartRecord", ContentItem "ProductPartRecord".

, , HasMany , Fluent, .

+1

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


All Articles