mardi 20 septembre 2022

Json schema to Java entity class

I am a java client developer. We have a requirement of generating java entity class from json. Although Json to pojo is possible through multiple libraries, our requirement is to Json to JPA entity class.

Can we use dynamic proxy to generate this

Json:

{

  "properties" : {
    "id" : {
      "type" : "string"
    },
    "lock" : {
      "type" : "string"
    },
    "version" : {
      "type" : "integer"
    }
  }
}

Java entity class


package com.testclient;

import java.util.Objects;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;



@Entity
@Table(name = "DUMMYMODEL")
public class DummyModel implements Model<String> {
    @Id
    private String id;

    @Version
    private Long version;

    @JsonIgnore
    @Transient
    private String lock;

    public DummyModel() {
    }

    public DummyModel(String id) {
        this.id = id;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public Long getVersion() {
        return version;
    }

    @Override
    public String getLock() {
        return lock;
    }

    @Override
    public void setVersion(Long version) {
        this.version = version;
    }

    @Override
    public void setLock(String lock) {
        this.lock = lock;
    }

  
    @Override
    public String toString() {
        return "DummyModel [id=" + id + ", version=" + version + ", lock=" + lock + "]";
    }
}

Am aware of https://www.jsonschema2pojo.org/ which generates pojo.





Aucun commentaire:

Enregistrer un commentaire