Search code examples
javadatabaseentitypersistence

Java Entity insertion DB change column type


I inherit from a java code with a persistent class named "RELEASE" (javax.persistence dependencies)

@Entity
@Table(name = "\"RELEASE\"")
@Converter(name = "booleanConverter", converterClass = BooleanConverter.class)
public class Release implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(updatable = false, unique = true, nullable = false, precision = 22)
    private Long id;

    @Convert("booleanConverter")
    @Column(nullable = false, length = 5)
    private Boolean active;

    @Column(nullable = false, length = 125)
    private String name;

    // uni-directional many-to-one association to Service
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SERVICE_ID", nullable = false)
    private Service service;

And getter and setter associated, I have to change the type of the Id column (Long to String) because now I make an API request and get a json format response {key1: "response1, key2 : "response2" , ...} with string. The new type of number that I get are like "CHG0070132".

How can I change the type without losing my old data?


Solution

  • You need to change in 2 Parts

    1. The DB , you need to manually change the column type of the table in your DB Check here
    2. Change the getter Setter and the variable datatype from long to String