1
I need help with my code, I have two tables: -CDF (official register)
CREATE TABLE public.cdf(
ativo character varying(1) COLLATE pg_catalog."default",
reg integer NOT NULL,
nome character varying(30) COLLATE pg_catalog."default" NOT NULL,
senha character varying(33) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT cdf_pkey PRIMARY KEY (reg)
)
-RGPT (Point Record) with 2 foreign CDF keys: reg
-> registration number and nome
CREATE TABLE public.rgpt(
id_rgpt integer NOT NULL DEFAULT nextval('rgpt_id_rgpt_seq'::regclass),
reg integer NOT NULL,
nome character varying(30) COLLATE pg_catalog."default" NOT NULL,
entrada character varying(5) COLLATE pg_catalog."default",
saida character varying(5) COLLATE pg_catalog."default",
CONSTRAINT rgpt03_pkey1 PRIMARY KEY (id_rgpt),
CONSTRAINT rgpt_fk FOREIGN KEY (reg)
REFERENCES public.cdf (reg) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE NO ACTION,
CONSTRAINT rgpt_fk1 FOREIGN KEY (nome)
REFERENCES public.cdf (nome) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE NO ACTION
)
In my entities generated by Hibernate my foreign RGPT keys were like this: Java:
public class Rgpt:
private int idRgpt;
private Cdf cdfByReg;
private Cdf cdfByNome;
private String entrada;
private String saida;
public Cdf getCdfByReg() {
return this.cdfByReg;
}
public void setCdfByReg(Cdf cdfByReg) {
this.cdfByReg = cdfByReg;
}
public Cdf getCdfByNome() {
return this.cdfByNome;
}
public void setCdfByNome(Cdf cdfByNome) {
this.cdfByNome = cdfByNome;
}
And the XML:
<class name="DAO.entity.Rgpt" table="rgpt" schema="public" optimistic- lock="version">
<id name="idRgpt" type="int">
<column name="id_rgpt" />
<generator class="assigned" />
</id>
<many-to-one name="cdfByReg" class="DAO.entity.Cdf" fetch="select">
<column name="reg" not-null="true" />
</many-to-one>
<many-to-one name="cdfByNome" class="DAO.entity.Cdf" fetch="select">
<column name="nome" not-null="true" />
</many-to-one>
The problem is when I go to do the HQL consultation and it gives a very strange error: Consultation:
final Session session = HibernateUtil.getSessionFactory().openSession();
String query = "from Rgpt as r where r.cdfByReg.reg= 54 and DATE(r.data) = '" + d.getDiaMesAno(data) + "'";
Query q = session.createQuery(query);
Rgpt rgpt = (Rgpt) q.uniqueResult();
ERROR:
Hibernate: select rgpt0_.id_rgpt as id_rgpt1_1_, rgpt0_.reg as reg2_1_, rgpt0_.name as nome3_1_, rgpt0_.em as em4_1_, rgpt0_.Ma as ema5_1_, rgpt0_.Sm as sm6_1_, rgpt0_.Sma as sma7_1_, rgpt0_.et as et8_1_, rgpt0_.Ta as eta9_1_, rgpt0_.st as st10_1_, rgpt0_. sta11_1_, rgpt0_.eadd as eadd12_1_, rgpt0_.Sadd assadd13_1_, rgpt0_.just as just14_1_, rgpt0_.data as data15_1_, rgpt0_.without the sem16_1_, rgpt0_.tdia as tdia17_1_, rgpt0_.ttaj as ttaj18_1_, rgpt0_.dif as dif19_1_, rgpt0_.vt as vt20_1_, rgpt0_.bn as bn21_1_, rgpt0_.grat_integral as grat_in22_1_, rgpt0_.grat_half as grat_me23_1_, rgpt0_.Vr asvr24_1_, rgpt0_.va_integral as va_inte25_1_, rgpt0_.va_half asva_meta26_1_ from public.rgpt rgpt0_ Where (rgpt0_.reg=? )and(DATE(rgpt0_.data)='23/03/2017' ) Mar 23, 2017 4:54:44 PM org.hibernate.engine.jdbc.spi.Sqlexceptionhelper logExceptions WARN: SQL Error: 0, Sqlstate: 22003 Mar 23, 2017 4:54:44 PM org.hibernate.engine.jdbc.spi.Sqlexceptionhelper logExceptions ERROR: Invalid value for int type : ESTER
Somehow he’s getting the nome
(varchar) in place of reg
(int), but the references at first seem correct.
Can anyone tell me if there’s something wrong with my consultation or my code? Grateful!
What is the code/mapping of the entity
Cdf
?– Dherik