I'm creating a rest app using Springboot, JPA and as Mysql database. It works fine but it has something strange when I enter the database.
I used the Workbench to create the relational entity diagram above the schema generated by JPA, but it did not migrate any relationships between tables, funny that all fields that should be foreign keys were generated, but are not mapped as keys .
When I test the application runs perfectly, I think that whoever is maintaining the integrity of the relationships in the database is the application itself before sending the data.
How do you make relationships also migrate to DBMS?
Below is the DB configuration in the application:
#1 - Configurando o servidor banco de dados
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/dbname?useSSL=false
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
For example, below are the City and State tables that were created in the database through JPA.
City:
@Entity
public class Cidade implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String descricao;
private Estado estado;
private Date dt_insert;
private Date dt_update;
public Cidade () {
}
public Cidade(Long id, String descricao, Estado estado) {
super();
this.id = id;
this.descricao = descricao;
this.estado = estado;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
@ManyToOne
public Estado getEstado() {
return estado;
}
public void setEstado(Estado estado) {
this.estado = estado;
}
public Date getDt_insert() {
return dt_insert;
}
public void setDt_insert(Date dt_insert) {
this.dt_insert = dt_insert;
}
public Date getDt_update() {
return dt_update;
}
public void setDt_update(Date dt_update) {
this.dt_update = dt_update;
}
@PrePersist
public void prePersist() {
Date atual = new Date();
this.dt_insert = atual;
this.dt_update = atual;
}
@PreUpdate
public void preUpdate() {
this.dt_update= new Date();
}
}
Status:
@Entity
public class Estado implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String sigla;
private String descricao;
private Date dt_insert;
private Date dt_update;
public Estado() {
}
public Estado(Long id, String sigla, String descricao) {
super();
this.id = id;
this.sigla = sigla;
this.descricao = descricao;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSigla() {
return sigla;
}
public void setSigla(String sigla) {
this.sigla = sigla;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public Date getDt_insert() {
return dt_insert;
}
public void setDt_insert(Date dt_insert) {
this.dt_insert = dt_insert;
}
public Date getDt_update() {
return dt_update;
}
public void setDt_update(Date dt_update) {
this.dt_update = dt_update;
}
@PrePersist
public void prePersist() {
Date atual = new Date();
this.dt_insert = atual;
this.dt_update = atual;
}
@PreUpdate
public void preUpdate() {
this.dt_update= new Date();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Estado other = (Estado) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
As you can see, a relationship with the state class was mapped in the city class, but unidirectional. In the image of the tables there is no line that shows the type of relationship between the two.