|
I'm using NHibernate 1.2.0 GA and SqlServer2005 driver.
I have the following tables:
CREATE TABLE Orders
(
orderId int IDENTITY (1, 1) NOT NULL CONSTRAINT PK_Order PRIMARY KEY,
total decimal(10, 2) NOT NULL
)
CREATE TABLE OrderItems
(
orderItemId int IDENTITY(1, 1) NOT NULL CONSTRAINT PK_OrderItem PRIMARY KEY,
orderId int NOT NULL CONSTRAINT FK_OrderOrderItem FOREIGN KEY REFERENCES Orders(orderId),
total decimal(10, 2) NOT NULL
)
Mapped as:
<!-- Orders -->
<class name="MyOrder" table="Orders">
<id name="Id" type="Int32" column="orderId" unsaved-value="0">
<generator class="native" />
</id>
<property name="Total" column="total" type="Decimal" not-null="true" />
<bag name="OrderItems" inverse="true" cascade="save-update" fetch="subselect">
<key column="orderId" />
<one-to-many class="OrderItem" />
</bag>
</class>
<!-- OrderItems -->
<class name="OrderItem" table="OrderItems">
<id name="Id" type="Int32" column="orderItemId" unsaved-value="0">
<generator class="native" />
</id>
<many-to-one name="Order" column="orderId" class="MyOrder" not-null="true" />
<property name="Total" column="total" type="Decimal" not-null="true" />
</class>
I have the following data in the tables:
INSERT INTO Orders(total) VALUES(1)
INSERT INTO Orders(total) VALUES(2)
INSERT INTO Orders(total) VALUES(3)
INSERT INTO Orders(total) VALUES(4)
INSERT INTO Orders(total) VALUES(5)
INSERT INTO Orders(total) VALUES(6)
INSERT INTO Orders(total) VALUES(7)
INSERT INTO Orders(total) VALUES(8)
INSERT INTO Orders(total) VALUES(9)
INSERT INTO Orders(total) VALUES(10)
INSERT INTO Orders(total) VALUES(11)
INSERT INTO OrderItems(orderId, total) VALUES(1, 1)
INSERT INTO OrderItems(orderId, total) VALUES(1, 1)
INSERT INTO OrderItems(orderId, total) VALUES(1, 1)
INSERT INTO OrderItems(orderId, total) VALUES(2, 1)
INSERT INTO OrderItems(orderId, total) VALUES(2, 1)
INSERT INTO OrderItems(orderId, total) VALUES(2, 1)
INSERT INTO OrderItems(orderId, total) VALUES(3, 1)
INSERT INTO OrderItems(orderId, total) VALUES(3, 1)
INSERT INTO OrderItems(orderId, total) VALUES(3, 1)
INSERT INTO OrderItems(orderId, total) VALUES(4, 1)
INSERT INTO OrderItems(orderId, total) VALUES(4, 1)
INSERT INTO OrderItems(orderId, total) VALUES(4, 1)
INSERT INTO OrderItems(orderId, total) VALUES(5, 1)
INSERT INTO OrderItems(orderId, total) VALUES(6, 1)
INSERT INTO OrderItems(orderId, total) VALUES(7, 1)
INSERT INTO OrderItems(orderId, total) VALUES(8, 1)
INSERT INTO OrderItems(orderId, total) VALUES(9, 1)
INSERT INTO OrderItems(orderId, total) VALUES(10, 1)
Basically the data inserted are 11 orders and their associated order items, where the first 4 orders have 3 line items.
I have created a method to return all the orders and orderitems using pagination:
ISession session = SessionFactory.OpenSession();
ICriteria criteria = session.CreateCriteria(typeof(MyOrder));
criteria.SetFirstResult(0);
criteria.SetMaxResults(10);
criteria.SetFetchMode("OrderItems", FetchMode.Eager);
criteria.AddOrder(new Order("Id", true));
IList<MyOrder> orders = criteria.List<MyOrder>();
session.Close();
If you run the above code the orders list returned has 10 items in it. However, the first 3 orders are the order with orderId=1, then the next 3 orders are the order with orderId=2, then the next 3 orders are the order with orderId=3, and the last order is the order with orderId=4, and it only has one orderItem (instead of the 3 inserted).
So it seems that the paged criteria didn't work as expected (I was expecting to get the first ten orders with their associated order items).
Am I missing something or is this a bug?
|