Springboot - JPA - does not migrate relationships in the entity and relationship diagram

Asked

Viewed 597 times

1

I am creating a Rest application using Springboot, JPA and as a Mysql database. It works well, but it has something strange when I enter the database.

I used Workbench to create the relational entity diagram on top of the schema generated by JPA, but did not migrate any relationship between the tables, the funny thing is 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 relationships in the database is the application itself before sending the data.

How to make relationships 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 follows the City and State tables that were created in the database through JPA.

inserir a descrição da imagem aqui Below follow the two classes:

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();
    }
}

State:

@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, the city class has mapped out a relationship with the state class, but one-way. The table image does not show the line showing the type of relationship between the two.

  • Include a print of status quo from your database and also some classes that demonstrate how your relationship is being done.

  • Are you sure it’s not a problem in the tool you used? You checked the table creation scripts to see if the constraints were created?

  • JPA relationships only create constants when mandatory, if you only add Onetomany it will not create a constant and index to a Foreign key, it is even declaring a column and adding a non-compulsory relationship

  • Weslley Barbosa, all right, how can I define it as mandatory? Also because it is part of a business requirement to inform that necessarily the city has to be related to the state. I have now used the @Notnull annotation in the city class in the field that refers to the state, so I require it to have a value. But FK has not yet been created. How to make it mandatory?

  • @Leonardolima, by the way? Yes I checked the scripts, he is not creating the FK he create a column as if it were any other, but does not map it as Foreign Key.

1 answer

0


To be able to implement the referential integrity in the Mysql database it was necessary for JPA to create the tables using the Innodb engine, to do this I changed the dialect within the application.properties and had the tables generated. It worked, the relationships in DB were generated.

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect

Browser other questions tagged

You are not signed in. Login or sign up in order to post.