tags:

views:

1680

answers:

1

I have two tables:

-- Table: medibv.btdbn

-- DROP TABLE medibv.btdbn;

CREATE TABLE medibv.btdbn
(
  mabn varchar(8) NOT NULL,
  hoten text,
  ngaysinh timestamp,
  namsinh varchar(4),
  phai numeric(1) DEFAULT 0,
  mann varchar(2),
  madantoc varchar(2),
  sonha varchar(15),
  thon text,
  cholam text,
  matt varchar(3),
  maqu varchar(5),
  maphuongxa varchar(7),
  userid numeric(5) DEFAULT 0,
  ngayud timestamp DEFAULT now(),
  hotenkdau text,
  nam text,
  image bytea,
  barcode bytea,
  CONSTRAINT pk_btdbn PRIMARY KEY (mabn) USING INDEX TABLESPACE medi_index,
  CONSTRAINT fk_btdbn_btddt FOREIGN KEY (madantoc)
      REFERENCES medibv.btddt (madantoc) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE SET NULL,
  CONSTRAINT fk_btdbn_btdnn_bv FOREIGN KEY (mann)
      REFERENCES medibv.btdnn_bv (mann) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE SET NULL,
  CONSTRAINT fk_btdbn_btdpxa FOREIGN KEY (maphuongxa)
      REFERENCES medibv.btdpxa (maphuongxa) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE SET NULL,
  CONSTRAINT fk_btdbn_btdquan FOREIGN KEY (maqu)
      REFERENCES medibv.btdquan (maqu) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE SET NULL,
  CONSTRAINT fk_btdbn_btdtt FOREIGN KEY (matt)
      REFERENCES medibv.btdtt (matt) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE SET NULL
) 
WITH OIDS;
ALTER TABLE medibv.btdbn OWNER TO medisoft;

and

-- Table: medibv.benhandt

-- DROP TABLE medibv.benhandt;

CREATE TABLE medibv.benhandt
(
  mabn varchar(8),
  mavaovien numeric(18) DEFAULT 0,
  maql numeric(18) NOT NULL DEFAULT 0,
  makp varchar(2),
  ngay timestamp,
  dentu numeric(1) DEFAULT 0,
  nhantu numeric(1) DEFAULT 0,
  lanthu numeric(3) DEFAULT 0,
  madoituong numeric(2) DEFAULT 0,
  chandoan text,
  maicd varchar(9),
  mabs varchar(4),
  sovaovien varchar(10),
  loaiba numeric(3) DEFAULT 0,
  userid numeric(5) DEFAULT 0,
  ngayud timestamp DEFAULT now(),
  cschandoan text,
  CONSTRAINT pk_benahndt PRIMARY KEY (maql),
  CONSTRAINT fk_benhandt_btdkp_bv FOREIGN KEY (makp)
      REFERENCES medibv.btdkp_bv (makp) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE SET NULL,
  CONSTRAINT fk_benhandt_doituong FOREIGN KEY (madoituong)
      REFERENCES medibv.doituong (madoituong) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE SET NULL
) 
WITH OIDS;
ALTER TABLE medibv.benhandt OWNER TO medisoft;

I used NHibernate with C# and I created two classes: Btdbn and Benhandt

i want to get a Ilist like sql below:

hql= "select tdbn.mabn, badt.mavaovien " +
    "from btdbn tdbn " +
    "     inner join benhandt badt on tdbn.mabn = badt.mabn";
+1  A: 

HQL works on classes, not on tables. So, you have to write a HQL query in where you query the classes that are mapped to those 2 tables. If you want to return data by the hql query that cannot be represented by one of your classes (entity classes that are mapped by NHibernate), then you'll have to create a DTO class. This class has properties that will contain the values retrieved by the query. To be able to do this, you'll have to 'import' that class.

Then, you can do something like this:

select new MyDTO(tdbn.mabn, badt.mava) from btdn tdbn inner join tdbn.mabn
Frederik Gheysels
is there any way if i don't want to create DTD alss
i don't think so.
Frederik Gheysels
thank Frederik Gheysels but i don't want to create MyDTO class, is there any way
you will need some kind of 'container' class which will hold the results that are returned from the DB.You can try it without dto, but I don't know if it will work.
Frederik Gheysels
Frederik is correct. HQL is an object-oriented query language so you must provide it with objects (a DTO class, in this case). Your only other option is a raw SQL query with session.CreateSQLQuery.
Stuart Childs
hi Frederik Gheysels i've created a class named rptBaocaotuvong to contain the value retrieved by th query like this:IQuery l = session.CreateQuery("select new rptBaocaotuvong(bn.Mabn, bn.Hoten, bn.Ngaysinh, ba.Mavaovien) from Btdbn bn, Benhandt ba where ba.Mabn=bn.Mabn");IList lt = l.List();it thew an exception: class not found: rptBaocaotuvong [select new rptBaocaotuvong(bn.Mabn, bn.Hoten, bn.Ngaysinh, ba.Mavaovien) from Baocaotuvong.Btdbn bn, Baocaotuvong.Benhandt ba where ba.Mabn=bn.Mabn]Please help!