Hibernate - Anotações + Chave estrageira + Chave composta

2 respostas
S

Estou tentando migrar uma aplicação para hibernate e estou com problemas com as chaves estrangeiras.
Tenho 3 tabelas (conforme o script abaixo), mas resumidamente:
Tabela caixas (pk = cai_numero)
Tabela lotes (pk = lot_numero + lot_serie + (fk)cai_numero)
Tabela notas (pk = not_sequencia + (fk)lot_numero) + (fk)lot_serie + (fk)cai_numero)
Ou seja, uma caixa contem vários lotes (1:N) e um lote contém várias notas (1:N).

CREATE TABLE caixas
(
  cai_numero integer NOT NULL,
  cai_cd integer,
  cai_data_envio character varying(10),
  cai_data_inicio character varying(10),
  cai_data_final character varying(10),
  CONSTRAINT pk_cai_numero PRIMARY KEY (cai_numero)
)

CREATE TABLE lotes
(
  cai_numero integer NOT NULL,
  lot_numero integer NOT NULL,
  lot_serie integer NOT NULL,
  lot_data_hora character varying(20) NOT NULL,
  CONSTRAINT pk_lotes PRIMARY KEY (lot_numero, lot_serie),
  CONSTRAINT fk_caixas FOREIGN KEY (cai_numero)
      REFERENCES caixas (cai_numero) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

CREATE TABLE notas
(
  not_id integer NOT NULL,
  cai_numero integer NOT NULL,
  lot_numero integer NOT NULL,
  lot_serie integer NOT NULL,
  not_sequencia integer NOT NULL,
  not_numero integer NOT NULL,
  not_valor double precision NOT NULL,
  not_cfop integer NOT NULL,
  not_data character varying(10) NOT NULL,
  not_uf_emit character varying(2) NOT NULL,
  not_ie_emit character varying(9),
  not_cnpj_emit character varying(14) NOT NULL,
  not_tipo_emit integer NOT NULL,
  not_uf_dest character varying(2) NOT NULL,
  not_ie_dest character varying(9) NOT NULL,
  not_cnpj_dest character varying(14) NOT NULL,
  not_tipo_dest integer NOT NULL,
  not_base_icms double precision NOT NULL,
  not_valor_icms double precision NOT NULL,
  not_base_icms_subst double precision NOT NULL,
  not_valor_icms_subst double precision NOT NULL,
  CONSTRAINT pk_notas PRIMARY KEY (not_sequencia),
  CONSTRAINT fk_caixas FOREIGN KEY (cai_numero)
      REFERENCES caixas (cai_numero) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_lotes FOREIGN KEY (lot_numero, lot_serie)
      REFERENCES lotes (lot_numero, lot_serie) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

Minhas classes ficaram:

@Entity
@Table(name = "caixas")
@SuppressWarnings("serial")
public class Caixa implements Serializable {

	@Id
	@Column(name="cai_numero", nullable=false, insertable=true,	updatable=true)
	private int numero; //numero da caixa
...
@Entity
@Table(name = "lotes")
@SuppressWarnings("serial")
public class Lote implements Serializable {
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="cai_numero", insertable=true, updatable=true)
	@Fetch(FetchMode.JOIN)
	//@Cascade(CascadeType.SAVE_UPDATE)
	private Caixa caixa; //caixa que o lote pertence
	
	@Id
	private LotePk lotePk;
...
@Embeddable
@SuppressWarnings("serial")
public class LotePk implements Serializable {

	@Column(name = "lot_numero")	
	private int numero;

	@Column(name = "lot_serie")
	private int serie;
...
@Entity
@Table(name = "notas")
@SuppressWarnings("serial")
public class Nota implements Serializable {
	
	@GeneratedValue(strategy = GenerationType.SEQUENCE)
	@Column(name = "not_id")
	private int id; //identificador unico da nota no banco
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="cai_numero", insertable=true, updatable=true)
	@Fetch(FetchMode.JOIN)
	//@Cascade(CascadeType.SAVE_UPDATE)
	private Caixa caixa; //caixa a qual pertence a nota
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({@JoinColumn(name="lot_numero", insertable=true, updatable=true),
				@JoinColumn(name="lot_serie", insertable=true, updatable=true)})
	@Fetch(FetchMode.JOIN)
	//@Cascade(CascadeType.SAVE_UPDATE)
	private Lote lote; //lote o qual pertence a nota
	
	@Id
	@Column(name = "not_sequencia",	nullable=false,	insertable=true, updatable=true)	
	private int sequencia; //sequencia da nota dentro do lote
...

Antes de eu explicar os problemas que estou enfrentando, alguém poderia me dizer se as anotações estão corretas? Pelo que pesquisei não tem como eu trabalhar com chave composta sem criar uma outra classe (neste caso, LotePk).

[]'s

2 Respostas

S

Resolvi o problema com as anotações. Alterei algumas definições que estavam erradas no script SQL e no atributo lotePk da classe Lote alterei a anotação @Id para @EmbeddedId.
Vamos ver o que dá daqui pra frente.
[]'s

juniorsatanas

scheide posta o final aqui mano, atítulo de curiosidade !

Criado 2 de fevereiro de 2009
Ultima resposta 22 de fev. de 2010
Respostas 2
Participantes 2