Amigos, me ajudem…
Fiz tudo direitinho, olha aí…
No meu banco de dados eu criei:
CREATE TABLE tablea(
id integer NOT NULL DEFAULT nextval('tablea_seq'::regclass),
CONSTRAINT tablea_pkey PRIMARY KEY (id)
)
CREATE TABLE tableb(
id integer NOT NULL,
CONSTRAINT tableb_pkey PRIMARY KEY (id),
CONSTRAINT tableb_fkey FOREIGN KEY (id)
REFERENCES tablea (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
No meu código criei as duas tabelas (TableA e TableB):
[code]@Entity (name=“TableA”)
@SequenceGenerator(name = “tablea_seq”, sequenceName = “tablea_seq”)
@Table (schema=“public”, name=“tablea”)
public class TableA {
@Id
@GeneratedValue(
strategy = GenerationType.AUTO,
generator = "tablea_seq"
)
@Column (name="id")
private Integer id;
@OneToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@PrimaryKeyJoinColumn
private TableB tableB;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public TableB getTableB() {
return tableB;
}
public void setTableB(TableB tableB) {
this.tableB = tableB;
}
}[/code]
[code]@Entity (name=“TableB”)
@Table (schema=“public”, name=“tableb”)
public class TableB {
@GenericGenerator(name = "gerador", strategy = "foreign",
parameters = @Parameter(name = "property", value = "tableA"))
@Id
@GeneratedValue(generator = "gerador")
@Column(name = "id")
private Integer id;
@OneToOne(mappedBy = "tableB", fetch = FetchType.LAZY)
private TableA tableA;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public TableA getTableA() {
return tableA;
}
public void setTableA(TableA tableA) {
this.tableA = tableA;
}
}[/code]
…e o teste:
TableA tableA = new TableA();
TableB tableB = new TableB();
tableA.setTableB(tableB);
tableB.setTableA(tableA);
dao.incluir(tableA);
…e tudo o que tenho é a exceção abaixo:
01:12:47,890 ERROR JDBCExceptionReporter:234 - ERROR: insert or update on table "tableb" violates foreign key constraint "tableb_fkey"
Detail: Key (id)=(5) is not present in table "tablea".
Então, está gerando a sequencia (como vê acima = 5), mas parece estar violando a foreignkey da TableB. Detalhe: as duas tabelas estão em branco (sem registros). A cada nova tentativa é gerado uma sequencia, mas sem sucesso na inclusão dos registros. Dá a entender que primeiro tem de gravar na tableA, para depois gravar na tableB, mas não está ocorrendo…
Me ajudem aí, estou no sufoco com isto, precisando resolver. Passei o dia pesquisando isto e a única solução que existe é esta, mas não está funcionando…
Obrigado a todos.