spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
3.01k stars 1.42k forks source link

Bad column order in spring boot 3.1 #3044

Closed Guarmanda closed 1 year ago

Guarmanda commented 1 year ago

The ordering of columns changes between 3.0 and 3.1. In 3.1, spring data jpa generates columns in alphabetical order, including the ID column, so that my ID column is at the middle of the table

One of the problematic entities: `@Entity @Getter @Setter @NoArgsConstructor public class Personne extends Historique {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idPersonne;
private String nom;
private String prenom;
private LocalDate derniereConnexion;
private String quadrigramme;

@ManyToMany(fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
@JoinTable(name = "statut_personne", joinColumns = @JoinColumn(name = "idPersonne"), inverseJoinColumns = @JoinColumn(name = "idStatut"))
private List<Statut> statut;

@OneToMany(mappedBy = "personne")
private List<Planification> planifications;

@OneToOne(mappedBy = "personne", cascade = CascadeType.REMOVE)
private Utilisateur utilisateur;

@OneToMany(mappedBy = "personne")
private List<SquadPersonne> squadPersonnes;

@OneToMany(mappedBy = "personne")
private List<Competence> competences;

@OneToMany(mappedBy = "personne")
private List<ClientPersonne> clientPersonnes;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "personne_operation", joinColumns = @JoinColumn(name = "idPersonne"), inverseJoinColumns = @JoinColumn(name = "idOperation"))
private Set<Operation> operations;

@ManyToOne
@JoinColumn(name = "idAgence")
private Agence agence;

@OneToMany(mappedBy = "personne", cascade = CascadeType.REMOVE)
private List<PersonneTechnologie> personneTechnologies;

public Statut getStatut() {
    String app = JwtUtil.getApplication();
    if (app != null && app.equals("gco")) {
        return statut.stream().filter(Statut::isGco).findFirst().orElse(null);
    } else {
        return statut.stream().filter(s -> !s.isGco()).findFirst().orElse(null);
    }
}

public void setStatut(Statut statut) {
    String app = JwtUtil.getApplication();
    if (this.statut == null) {
        this.statut = new ArrayList<>();
    }
    if (!this.statut.isEmpty()) {
        if (app.equals("gco")) {
            // remove first gco statut
            this.statut.removeIf(Statut::isGco);
        } else {
            // remove first non gco statut
            this.statut.removeIf(s -> !s.isGco());
        }
    }
    this.statut.add(statut);
}

public Personne(String nom, String prenom) {
    super();
    this.nom = nom;
    this.prenom = prenom;
}

public PersonneDto toDto() {
    Long idUser = (this.utilisateur == null) ? null : this.utilisateur.getIdUtilisateur();
    Long idStatut = (this.getStatut() == null) ? null : this.getStatut().getIdStatut();
    Long idAgence = (this.agence == null) ? null : this.agence.getIdAgence();
    return new PersonneDto(this.idPersonne, this.nom, this.prenom, idStatut,
            idAgence,
            idUser, this.quadrigramme, this.getDateFin(),
            this.getDateDebut(),
            this.getDerniereConnexion());
}

}`

When creating a database with 3.0: image

When creating a database with 3.1: image

mp911de commented 1 year ago

Not sure what you refer to. Spring Data JPA doesn't create any tables, it is your underlying persistence provider that is typically of doing so.