Hi all!
I experience a problem when using HasMany-associations with the latest
AR 2.0. To illustrate the problem, I created a simple Customer and
Order class, where each Customer has n Orders and each Order belongs
to 1 Customer. My classes look like that:
[ActiveRecord("Customers")]
public class Customer : ActiveRecordBase<Customer>
{
[PrimaryKey(PrimaryKeyType.Native, Access =
PropertyAccess.Property)]
public long Id { get; set; }
[Property(NotNull = true)]
public string Name { get; set; }
[HasMany(typeof(Order),
Table = "Orders",
ColumnKey = "CustomerId",
Cascade = ManyRelationCascadeEnum.All,
RelationType = RelationType.Bag,
Access = PropertyAccess.Property,
Inverse = true)]
public IList<Order> Orders { get; set; }
}
[ActiveRecord("Orders")]
public class Order : ActiveRecordBase<Order>
{
[PrimaryKey(PrimaryKeyType.Native, Access =
PropertyAccess.Property)]
public long Id { get; set; }
[Property(NotNull = true)]
public DateTime Date { get; set; }
}
I create a new customer using these lines of code:
var c = new Customer();
c.Name = "ALFKI";
c.Orders = new List<Order>();
c.Orders.Add(new Order { Date = DateTime.UtcNow });
c.SaveAndFlush();
The SQL commands that are generated are (tested with SQLite):
NHibernate: INSERT INTO Customers (Name) VALUES (@p0); select
last_insert_rowid();@p0 = 'ALFKI'
NHibernate: INSERT INTO Orders (Date) VALUES (@p0); select
last_insert_rowid();@p0 = 16.10.2009 00:00:00
As you can see, the INSERT statement for the Orders table does not set
the foreign key for the customer. If I set the 'Inverse' attribute of
the Orders collection to 'false', the following SQL is generated:
NHibernate: INSERT INTO Customers (Name) VALUES (@p0); select
last_insert_rowid();@p0 = 'ALFKI'
NHibernate: INSERT INTO Orders (Date) VALUES (@p0); select
last_insert_rowid();@p0 = 16.10.2009 00:00:00
NHibernate: UPDATE Orders SET CustomerId = @p0 WHERE Id = @p1;@p0 = 1,
@p1 = 1
Now the foreign key is set at least, although this is not a desirable
behaviour as it 1) leads to a superfluous SQL statement and 2) the
foreign key in the Orders table should not allow NULLs. How could I
solve this issue? Is it a bug?
Best Regards
Markus
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---