title: How to generate Swagger codegen Java models as JPA Entities
body: <p>I am using Swagger codegen to create Java models to be used in a Spring REST server, and would like to know how to get Swagger to declare each model as a JPA entity.</p>
  
  <p>I generate the code with the <code>swagger-codegen-maven-plugin</code> as follows:</p>
  
  <pre><code>&lt;plugin&gt;
      &lt;groupId&gt;io.swagger&lt;/groupId&gt;
      &lt;artifactId&gt;swagger-codegen-maven-plugin&lt;/artifactId&gt;
      &lt;version&gt;2.4.0&lt;/version&gt;
      &lt;executions&gt;
          &lt;execution&gt;
              &lt;goals&gt;
                  &lt;goal&gt;generate&lt;/goal&gt;
              &lt;/goals&gt;
              &lt;configuration&gt;
                  &lt;inputSpec&gt;${project.basedir}/src/main/openApi/Rack.json&lt;/inputSpec&gt;
                  &lt;language&gt;spring&lt;/language&gt;
                  &lt;groupId&gt;com.me&lt;/groupId&gt;
                  &lt;artifactId&gt;rest-server&lt;/artifactId&gt;
                  &lt;apiPackage&gt;com.me.rest.api&lt;/apiPackage&gt;
                  &lt;modelPackage&gt;com.me.rest.model&lt;/modelPackage&gt;
                  &lt;invokerPackage&gt;com.me.rest.invoker&lt;/invokerPackage&gt;
                  &lt;configOptions&gt;
                      &lt;sourceFolder&gt;src/gen/java/main&lt;/sourceFolder&gt;
                      &lt;java8&gt;true&lt;/java8&gt;
                      &lt;dateLibrary&gt;java8&lt;/dateLibrary&gt;
                  &lt;/configOptions&gt;
              &lt;/configuration&gt;
          &lt;/execution&gt;
      &lt;/executions&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>As I have it now, this is the abbreviated java code that gets generated:</p>
  
  <pre><code>@Validated
  @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "...")
  
  public class Rack   {
    @JsonProperty("id")
    private Long id = null;
  
    @JsonProperty("name")
    private String name = null;
  
    ...
  }
  </code></pre>
  
  <p>How do I get Swagger to add the @Entity and @Id JPA annotations, as follows?</p>
  
  <pre><code>import javax.persistence.Entity;
  import javax.persistence.Id;
  
  @Entity
  @Validated
  @javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "...")
  
  public class Rack   {
    @Id
    @JsonProperty("id")
    private Long id = null;
  
    @JsonProperty("name")
    private String name = null;
  
    ...
  }
  </code></pre>
  
  <p>This way, all I would have to do to get Spring to automatically expose these generated classes as REST APIs, would be to add the following to my <code>pom.xml</code>:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-data-rest&lt;/artifactId&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Then I could create the JPA repositories with Spring-Data, as follows:</p>
  
  <pre><code>public interface RackRepository extends CrudRepository&lt;Rack, Long&gt; {
  }
  </code></pre>