1
Situation 1
I have the following tables, very simple:
.......................
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
}
Official:
@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 tables, very simple:
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 String telefone;
private String celular;
// Constructor
// get e sets
}
Official:
@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 properties telephone and cellular class Contact are represented as columns in tables Official and Client in the database, how this situation is mapped 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 class properties Official and Client, how do I map using JPA in this situation?
3 - In the Customer class we have the property dataNascization which stores an object of the type java.time.LocalDate
. To JPA(2.1) and/or Hibernate(4.3.5.Final) provides some annotation or other means for mapping this object? If not, there is some other alternative than to re-use the java.util.Date
?
4 - This is a question of curiosity about SQL, the type attribute BIT currently has some difference from TINYINT(1)?
First of all, thank you for answering me. The question I asked is why I first created the Customer and Employee classes, however, these two classes had many similar attributes besides cell and phone (I only put these two attributes to simplify), so I first created the Contact class, putting the similar attributes on contact in this class to mitigate attribute redundancy, I had created the Address class, which also contains redundant attributes of the Employee and Client classes, but I did not ask the question to simplify it.
– klaytonbrito
As in your observation, it is unnecessary to create an extra table for Contacts and the associations for this are 1-1, which could as you said yourself, occur performance loss. But in my case real, i still have the Address class, which has 7 redundant attributes of the Client, Employee and Request (address to send the request to) gathered there. In your opinion, in this my scenario, you think I better mitigate redundancy and create tables like Contact and Address(relationships for these classes are 1-1), or leave the tables with many redundant attributes?
– klaytonbrito
Man, the advantage of not creating a Contact and Address table and not mapping them is that JPA will not do the automatic Joins, that is, increases the performance of the Framework. On counter-start, you will have many repeated attributes in your entities. I don’t know what to recommend to you, because both have their advantages and disadvantages.
– Ascension
One thing I forgot to mention, in the case of the date, you changed to Calendar which is not wrong, but as I read some things about the library
java.time
of Java 8 and because it has a number of advantages I wanted to use it in my project and wanted to know if there was an annotation that I could use to map thejava.time.LocalDate
, and not thejava.util.Date
orjava.util.Calendar
previous versions of Java.– klaytonbrito
I’ll be honest, I don’t work with Java 8 yet, but if I use it, they say Joda’s here to stay, but I don’t know about it. Another thing, talking about your separate entity, take a look at the @Embeddable annotation and how to work with it.
– Ascension
Dude, it was something like this I was looking for, the @Embeddable annotation makes it possible for me to separate the columns into distinct objects, like the Client table, and separate this into Client class and Address class, maintaining the cohesion of which Address class is intrinsic to the Client class.
– klaytonbrito