-1
I have two tables:
Table person:
CREATE TABLE T_AM_OME_PESSOA
(
cd_pessoa NUMBER (8) NOT NULL ,
nm_pessoa VARCHAR2 (60) NOT NULL
) ;
ALTER TABLE T_AM_OME_PESSOA ADD CONSTRAINT PK_AM_PESSOA PRIMARY KEY ( cd_pessoa ) ;
and table forum:
CREATE TABLE T_AM_OME_FORUM
(
cd_forum NUMBER (8) NOT NULL ,
ds_forum VARCHAR2 (256) NOT NULL
) ;
ALTER TABLE T_AM_OME_FORUM ADD CONSTRAINT PK_AM_FORUM PRIMARY KEY ( cd_forum ) ;
and relationships
ALTER TABLE T_AM_OME_FORUM ADD CONSTRAINT FK_AM_PESSOA_FORUM FOREIGN KEY ( cd_forum ) REFERENCES T_AM_OME_PESSOA ( cd_pessoa ) ;
and sequence
-- Sequencia tabela T_AM_OME_PESSOA
CREATE SEQUENCE SQ_AM_OME_PESSOA
INCREMENT BY 1
START WITH 1
MAXVALUE 99999999
NOCACHE
NOCYCLE;
where in the person implementation will be an abstract class.. currently via bank, to register a forum, I first register any name in the PERSON table and use the value generated from the sequence as the primary key for cd_forum. however, I would like to register forum directly, making record both in the table person and in the table forum simultaneously.
My DAO
public void gravar(Forum f, Connection conexao) throws Exception {
String sql1 = "insert into T_AM_OME_PESSOA" + "(CD_PESSOA, NM_PESSOA) values (SQ_AM_OME_PESSOA.NEXTVAL,?)";
PreparedStatement estrutura = conexao.prepareStatement(sql1);
estrutura.setString(1, f.getNm_pessoa());
estrutura.execute();
estrutura.close();
String sql2 = "insert into T_AM_OME_FORUM" + "(CD_FORUM, DS_FORUM) values(?,?)";
PreparedStatement estrutura2 = conexao.prepareStatement(sql2);
estrutura2.setInt(1, f.getCd_pessoa());
estrutura2.setString(2, f.getDs_forum());
estrutura2.execute();
estrutura2.close();
test class
f.setNm_pessoa(JOptionPane.showInputDialog("Nome do forum "));
f.setCd_forum(f.getCd_pessoa());
f.setDs_forum(JOptionPane.showInputDialog("Descricao do forum "));
ForumBo.grava(f,con);
this giving
java.sql.Sqlintegrityconstraintviolationexception: ORA-02291: integrity restriction (SYSTEM.FK_AM_PESSOA_FORUM) violated - unmarked motherkey
any suggestions for this INSERT simultaneously in PERSON AND FORUM? Obs.: the class Forum extends Person
It works perfectly. Thank you!
– Mariana de Amorim Delfino
For nothing... Arrange!!!
– Claudio
Mariana, Just one question: your modeling is including a FK between two Pks... this is "quite unusual"... You even need to have two tables with the same value being PK???
– Claudio
I believe that for this table it is not really relevant to have the same value for PK. I recently used 2 tables that had a m:n relationship (many-to-many) and in normalization, I did not use associative entity but used the same value as PK in a table like FK in another table to form a composite primary key and was very important to ensure data uniqueness
– Mariana de Amorim Delfino
Another idea also for the proposed question (later I thought of other alternatives), was to redeem this value via bank through a sub-concession. Worked.
– Mariana de Amorim Delfino