2010-07-07 3 views

Répondre

6

Vous avez besoin d'un critère Restrictions.isNotEmpty():

List<A> r = s.createCriteria(A.class) 
    .add(Restrictions.or(
     Restrictions.isNotEmpty("a1"), 
     Restrictions.isNotEmpty("a2"))).list(); 
+0

C'était facile ... c'est facile ... merci! –

+0

Wow, je ne le savais pas. Ce n'est probablement pas disponible dans NHibernate. –

0

Vous avez besoin d'un sous-requête, en utilisant DetachedCriteria:

Il y a un example in Ayende's Blog. Je n'ai pas actuellement le temps de travailler dessus.

-1

Je pense que cet exemple vous aidera. Il est écrit en t-sql, mais devrait être facile à suivre quelle que soit la plate-forme que vous utilisez.

/*Create data structures*/ 
CREATE TABLE Parent ( 
    ParentId  INT   NOT NULL PRIMARY KEY 
    , ParentName VARCHAR(50) NOT NULL) 

CREATE TABLE ChildA ( 
    ChildAId  INT   NOT NULL PRIMARY KEY 
    , ParentId INT   NOT NULL CONSTRAINT FK_ChildA_Parent FOREIGN KEY REFERENCES Parent(ParentId) 
    , ChildAName VARCHAR(50) NOT NULL) 

CREATE TABLE ChildB ( 
    ChildBId  INT   NOT NULL PRIMARY KEY 
    , ParentId INT   NOT NULL CONSTRAINT FK_ChildB_Parent FOREIGN KEY REFERENCES Parent(ParentId) 
    , ChildBName VARCHAR(50) NOT NULL) 

/* Insert four parents */ 
INSERT INTO Parent VALUES (1,'A') 
INSERT INTO Parent VALUES (2,'B') 
INSERT INTO Parent VALUES (3,'C') 
INSERT INTO Parent VALUES (4,'D') 

/* Insert two children for A */ 
INSERT INTO ChildA VALUES (1,1,'a') 
INSERT INTO ChildB VALUES (1,1,'a') 

/* Insert one child for B */ 
INSERT INTO ChildA VALUES (2,2,'b') 

/* Insert one child for C */ 
INSERT INTO ChildB VALUES (2,3,'c') 

/* This select stmt returns A with children in both child tables, B with a child in ChildA, and C with a child in ChildB, but no D. */ 
SELECT * 
FROM Parent p 
WHERE EXISTS (select 1 from ChildA a where p.ParentId = a.ParentId) 
OR  EXISTS (select 1 from ChildB b where p.ParentId = b.ParentId) 
Questions connexes