Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Inheritance problem

2009-06-14
2013-04-16
  • Denis Ramos
    Denis Ramos
    2009-06-14

    I've tried the two inheritance approaches provided by ammentos, but i couldn't get things working in any one.

    Please, let me know if i've made some mistake!

    # FIRST approach

    SUPER CLASS
    @PersistentEntity(sourceDomain = "T_USUARIO", primaryKey = "id_usuario")
    public class Usuario {
        @PersistentField(fieldName="id_usuario", automatic=true, automaticType=AutomaticType.FRAMEWORK)
        private long id;
        ...
    }

    SUBCLASS
    @PersistentEntity(sourceDomain = "T_PRELETOR", primaryKey = "id_usuario")
    public class Preletor extends Usuario {
        @PersistentField(fieldName="id_usuario")
        private long id;
        ...
    }

    I got the following error:

    Caused by: java.sql.SQLException: Violation of unique constraint PK_T_PRELETOR: duplicate value(s) for column(s) ID_USUARIO in statement [insert into T_PRELETOR (id_trabalho,id_usuario,instituicao ) values ( ?,?,? )]

    I'm working with HSQLDB 1.8.0.10!!

    # SECOND approach

    SUPER CLASS still with the same code.

    SUBCLASS - superKey APPROACH

    @PersistentEntity(sourceDomain = "T_PRELETOR", primaryKey = "id_preletor", superKey="id_usuario")
    public class Preletor extends Usuario {
        @PersistentField(fieldName="id_preletor", automatic=true, automaticType=AutomaticType.FRAMEWORK)
        private long idPreletor;

        @PersistentField(fieldName="id_usuario")
        private long idUsuario;
        …
    }

    ** If i add this constraint to the table:

    ALTER TABLE T_PRELETOR ADD CONSTRAINT fk_id_usuario_T_PRELETOR_T_USUARIO FOREIGN KEY(id_usuario) REFERENCES T_USUARIO(id_usuario);

    I got this error:

    Caused by: java.sql.SQLException: Integrity constraint violation - no parent FK_ID_USUARIO_T_PRELETOR_T_USUARIO table: T_USUARIO in statement [insert into T_PRELETOR (id_preletor,id_trabalho,id_usuario,instituicao ) values ( ?,?,?,? )]

    ** Whithout the above constraint i got NullPointerException when i try to read an attribute of superclass (Usuario) from subclass (Preletor).

    Ammentos recorded T_USUARIO with all columns correct and generated auto-increment value to the ID_USUARIO.

    PROBLEM: Ammentos recorded T_PRELETOR correct, except by the FK ID_USUARIO which received 0 as value.

    Please, help me solve this problem!

    Entity Preletor has an one-to-one relationship with entity Trabalho. And Usuario has too an one-to-one relationship with entity Pessoa.

    ***** This is my unit est code:

    Pessoa pessoa = new Pessoa("Maria", sobreNome, "19 8111-1111",
                        "19 9001-5522");

    Usuario usuario = new Usuario("senha", email, pessoa);

    Preletor preletor = new Preletor("UFMG", null, usuario);

    Ammentos.openTransaction();

    Ammentos.save(pessoa);
    Ammentos.save(preletor);
               
    Ammentos.commitTransaction();
    =======================

    PROBLEM: Ammentos recorded T_PRELETOR correct, except by the FK ID_USUARIO which received 0 as value.

    I even tried using IDENTITY to generate PK values, but the stack trace reported that function is not supported besides HSQLDB 1.8.* has it.

    Best regards,
    Denis

     
    • ddpole
      ddpole
      2009-06-15

      Hi Denis,

      Could you send me by email your classes and a script to create the tables in order to let me reproduce your problem?

      Davide

       
    • Denis Ramos
      Denis Ramos
      2009-06-15

      Hi Davide,

      i sent an e-mail to your e-mail account from sourceforge telling how you can download the source code.

      Thanks a lot!

      Denis

       
    • ddpole
      ddpole
      2009-06-16

      Hi Denis.

      First try: synchronize parent's and current class' pkey with a persistence handler at onSave, like this:

      // into Preletor class

      @OnSave(When = BEFORE)
      private void synchronizeKeys(){
        this.id = super.getId();
      }

      NB: next version will perform this operation automatically ;)

      Please let me know if it is ok

      Regards
      Davide

       
    • Denis Ramos
      Denis Ramos
      2009-06-16

      Thanks Davide, it works pretty and if it could be done by the framework it would be better!

      Best regards
      Denis