R – using hql in NHibernate

hqlnhibernate

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";

Best Answer

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
Related Topic