0

I do not like the order of the columns in the database h2. I use Auditing to create tables.

Example code

package ru.omega.vaccination.model.dto.audit;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.time.Instant;

@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(
    value = {"createdAt", "updatedAt"},
    allowGetters = true
)
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public abstract class DateAudit implements Serializable {

  @CreatedDate
  @Column(nullable = true, updatable = false)
  private Instant createdAt;

  @LastModifiedDate
  @Column(nullable = true)
  private Instant updatedAt;

}

And

@Entity(name = "people")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class People extends DateAudit  {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
    @Column
    private String lastname;
    @Column
    private String firstname;
    @Column
    private String patronymic;

    ...
}

In DB i see enter image description here

I would like to move the "CREATED_AT" "UPDATED_AT" columns to the end of the table. I can't find a way to implement this. I would be glad to see code examples to solve such a problem.

WBLord
  • 874
  • 6
  • 29
  • How you have created this table? – Gaurav Jeswani Aug 26 '21 at 07:47
  • 4
    Why do you think the order of the columns are important? – Simon Martinelli Aug 26 '21 at 07:48
  • 2
    You should never rely on the order of columns in a table. If you need a specific order then choose it when doing queries. What do you really want to achieve here? – Gaël J Aug 26 '21 at 07:54
  • @GauravJeswani By hibernate – WBLord Aug 27 '21 at 08:47
  • @SimonMartinelli Requires you to check the database manually (many times). These columns are not of much value when browsing the database, but they get in the way. In the real project, I have about 10 columns, which should be removed at the end, for more convenient work. – WBLord Aug 27 '21 at 08:47
  • But as @GaëlJ said you will execute a SELECT and use only columns that you need and there you define the order – Simon Martinelli Aug 27 '21 at 08:49
  • I'm considering it as an option. I want it to be in the right order from the beginning. The order is not critical, I would like to get it right.) – WBLord Aug 27 '21 at 08:55

0 Answers0