NHibernate: Display IList Property

I have the following tables Order , Transaction , Payment . The Order class has some properties:

 public virtual Guid Id { get; set; } public virtual DateTime Created { get; set; } ... 

and I added two more:

 public virtual IList<Transaction> Transactions { get; set; } public virtual IList<Payment> Payments { get; set; } 

How to save Transactions and Payments lists (relationships) in a database?

+4
source share
4 answers

He came up with a working solution for me. In the mapping file, I added:

 <bag name="Transactions" lazy="true" table="Transaction" > <key column="OrderId"/> <one-to-many class="Transaction"/> </bag> <bag name="Payments" lazy="true" table="Payment"> <key column="OrderId"/> <one-to-many class="Payment"/> </bag> 
0
source

See collection matching in the reference documentation. There are several ways to map an IList property.

A bag is an unordered, non-indexed collection that may contain the same item several times.

 <bag name="Transactions" lazy="true" table="Transaction" > <key column="OrderId"/> <one-to-many class="Transaction"/> </bag> 

A set is an unordered, unindexed set containing unique elements.

 <set name="Transactions" lazy="true" table="Transaction" > <key column="OrderId"/> <one-to-many class="Transaction"/> </set> 

A list is an ordered and indexed collection that can contain the same item multiple times.

 <list name="Transactions" lazy="true" table="Transaction" > <key column="OrderId"/> <index column="ordering"/> <one-to-many class="Transaction"/> </list> 

NHibernate can return unordered collections in sorted order using the sort attribute.

No discussion of the collection in which we are assembled mentions a cascade . This allows you to perform operations on the parent for collection objects.

  • It usually doesn't make sense to include a cascade in the associations <many-to-one> or <many-to-many> . The cascade is often useful for the associations <one-to-one> and <one-to-many> .
  • If the lifetime of the child is limited by the life of the parent, make it a lifecycle object by specifying cascade = "all-delete-orphan".
  • Otherwise, you may not need a cascade. But if you think that you will often work with a parent and children together in the same transaction, and you want to keep some typing for yourself, consider using cascade = "persist, merge, save-update"
+7
source

If you manage a database, one of the joys of using NHib is that it will generate a database for you, including the foreign keys needed to support your lists. Use the following code:

  try { var schema = new SchemaExport(cfg); schema.Drop(false, true); schema.Create(true, true); } catch (Exception ex) { Console.WriteLine(@"Schema Export Error Message: {0}", ex); } 

where cfg is your NHibernate configuration object.

Let us know if this solves your problem or another question arises that you ask.

Berryl

+1
source

I'm not sure what you are asking. Assuming Transactions and Payments already mapped in nhibernate, you need to add the cascade option for association mappings for Order . In fluent nhibernate, something like this:

 References(x => x.Payments) .Cascade.All ... 

This will make sure that when adding transactions / payments to the order, they are saved.

change

Regardless of whether you use free nhibernate, cascading is the option that (I think) you need. The nhibernate documentation for mapping collections is here .

0
source

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


All Articles