Map columns of separate tables in a single object and vice versa

1
  

Situation 1

I have the following very simple tables:

.......................

And I have the following classes:

Client:

@Entity
public class Cliente implements Serializable {

    private static final long serialVersionUID = 7402770378598465859L;

    private Short id;
    private String nome;

    @Column(name="data_nascimento")
    private LocalDate dataNascimento;

    private Contato contato;

    //Constructor
    // get e sets
}

Contact:

@Entity
public class Contato implements Serializable {

    private static final long serialVersionUID = -5040500676803061821L;

    private String telefone;
    private String celular;

    //Constructor
    // get e sets
}

Employee:

@Entity
public class Funcionario implements Serializable{

    private static final long serialVersionUID = -5718684144972644433L;

    private Short id;
    private String nome;
    private String rg;
    private String cpf;
    private boolean ativo;
    private Contato contato;

    //Constructor
    // get e sets
}
  

Situation 2

I have the following very simple tables:

AndIhavethefollowingclasses:

Client:

@EntitypublicclassClienteimplementsSerializable{privatestaticfinallongserialVersionUID=7402770378598465859L;privateShortid;privateStringnome;@Column(name="data_nascimento")
    private LocalDate dataNascimento;

    private String telefone;
    private String celular;

    // Constructor
    // get e sets
}

Employee:

@Entity
public class Funcionario implements Serializable {

    private static final long serialVersionUID = -5718684144972644433L;

    private Short id;    
    private String nome;
    private String rg;
    private String cpf;
    private boolean ativo;
    private String telefone;
    private String celular;

    // Constructor
    // get e sets
}
  

Issues

1 - In "Situation 1", we have 2 tables and 3 classes, in which the phone and cell properties of the Contact class are represented as columns in the Employee and Client tables in the database, how is the mapping of this situation using JPA?

2 - In "Situation 2", we have 3 tables and 2 classes, in which the columns of the "Contact" table of the database are represented in the application as properties of the Employee and Client , how do I map using JPA in this situation?

3 - In the Client class we have the property dataNascimento that stores an object of type java.time.LocalDate . Does the JPA (2.1) and / or Hibernate (4.3.5.Final) provide some annotation or some other means for mapping this object? If not, is there any other alternative but to reuse java.util.Date ?

4 - This is a curious question about SQL, does the BIT attribute currently have any differences from TINYINT (1)?

    
asked by anonymous 03.07.2014 / 21:50

1 answer

1

Responding to situation 1 and 2:

What you need to solve situation 1 is in situation 2 and vice versa, ie in situation 1 you have 2 tables and situation code 2 is annotated for 2 entities. There is also the possibility, in situation 1, of creating the Contact class without being annotated for the JPA and having others inherit it. In situation 2, the attributes belong to the Contacts table, so they can not be annotated in other entities. In this case, do the following (workaround for situation 2):

  • Create the Contact entity;
  • Replace the phone and cell attributes of the classes with a Contact variable;
  • Make the note using @OneToOne as it is in the relationship.

    @Entity
    public class Contato implements Serializable {
    
        private static final long serialVersionUID = -5040500676803061821L;
    
        private String telefone;
        private String celular;
    
        //Constructor
        // get e sets
    }
    
    @Entity
    public class Cliente implements Serializable {
    
        private static final long serialVersionUID = 7402770378598465859L;
    
        private Short id;
        private String nome;
    
        @Column(name="data_nascimento")
        @Temporal(DATE)
        private Calendar dataNascimento;
    
        @OneToOne(fetch=FetchType.LAZY)
        private Contato contato;
    
        //Constructor
        // get e sets
    }
    
    @Entity
    public class Funcionario implements Serializable{
    
        private static final long serialVersionUID = -5718684144972644433L;
    
        private Short id;
        private String nome;
        private String rg;
        private String cpf;
        private boolean ativo;
    
        @OneToOne(fetch=FetchType.LAZY)
        private Contato contato;
    
        //Constructor
        // get e sets
    }
    
  • Replying to 3, see that I changed the type of the date of birth to Calendar and put the annotation.

    About the BIT in relation to TINYINT (1) is that the BIT by default in some databases is recognized as a Boolean field, that is, it will always accept 0 or 1 or FALSE or TRUE. Since TINYINT stores integer values greater than 1 or less than 0, it depends on how it was defined and even defining it so TINYINT (1) does not change its behavior. So if you are going to work with Boolean values, use BIT.

    Note: If the relationship is 1 to 1, remove the Contact table from the database and place the attributes in the Client and Employee. It will ease your side and avoid overuse of Join. : D

    Updated:

    If you want to keep the table separate and use its attributes in entities, you can use the @Embeddable annotation.

    Oracle Javadoc Embeddable

        
    04.07.2014 / 05:13