jpa use id name in repositories

master
Niclas Thobaben 2021-09-21 12:49:09 +02:00
parent b63d4b9268
commit ffd864dc92
3 changed files with 98 additions and 1 deletions

View File

@ -64,6 +64,8 @@ module.exports = {
model.table = jpa.table || model.plural
model.indexes = jpa.indexes
model.pkey = jpa.pkey
model.pkeyPascalCase = utils.pascalcase(jpa.pkey)
return model
@ -79,6 +81,8 @@ module.exports = {
repo.record_type = model.name;
repo.type = jpa.repository;
repo.pkey = jpa.pkey;
repo.pkeyPascalCase = utils.pascalcase(repo.pkey)
repo.pkey_type = model.fields.find(field => field.isPkey).type
repo.name = `${utils.pascalcase(model.name)}Repository`

View File

@ -0,0 +1,91 @@
/**
* Auto-generated from apibuilder.io service specification.
* apidoc-version : {{service.apidoc.version}}
* organisation : {{service.organization.key}}
* service-version : {{service.version}}
*
* Documentation at:
* <a href="https://app.apibuilder.io/{{service.organization.key}}/{{service.name}}/{{service.version}}">https://app.apibuilder.io/{{service.organization.key}}/{{service.name}}/{{service.version}}</a>
*
**/
package {{package}};
import javax.persistence.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.UpdateTimestamp;
import lombok.Data;
{{#imports}}
import {{{.}}};
{{/imports}}
import java.util.Collections;
import java.util.stream.Collectors;
/**
* {{description}}
* See <a href="https://app.apibuilder.io/{{service.organization.key}}/{{service.name}}/{{service.version}}#model-{{model.name}}">https://app.apibuilder.io/{{service.organization.key}}/{{service.name}}/{{service.version}}#model-{{model.name}}</a>
*
**/
@Entity
@Data
@Table(
name = "{{table}}",
indexes = {
{{#indexes}}
@Index(columnList = "{{.}}"),
{{/indexes}}
}
)
public class {{name}} {
{{#fields}}
{{#description}}/** {{.}} **/{{/description}}{{#isPkey}}
@Id{{#id_generator}}
@GeneratedValue({{#name}}generator = "{{.}}"{{/name}}){{#strategy}}
@GenericGenerator(name = "{{name}}", strategy = "{{.}}"){{/strategy}}{{#id_type}}
@Type(type = "{{.}}"){{/id_type}}{{/id_generator}}{{/isPkey}}{{#type.isCollection}}
@ManyToMany{{/type.isCollection}}
{{^oneToOne}}{{^isRecord}}{{^isCollection}}
@Column(
nullable = {{nullable}},
unique = {{unique}},
columnDefinition = "{{columnType}}"
){{/isRecord}}{{/isCollection}}{{/oneToOne}}{{#isEnum}}
@Enumerated(EnumType.STRING){{/isEnum}}{{#updateTimestamp}}
@UpdateTimestamp{{/updateTimestamp}}{{#creationTimestamp}}
@CreationTimestamp{{/creationTimestamp}}{{#oneToMany}}
@OneToMany{{/oneToMany}}{{#oneToOne}}
@OneToOne{{/oneToOne}}{{#isRecord}}{{^isCollection}}
@ManyToOne{{/isCollection}}{{/isRecord}}
private {{{type.name}}} {{name}};
{{/fields}}
public {{modelType}} toModel() {
{{modelType}} {{modelName}} = new {{modelType}}();
{{#fields}}
{{#isRecord}}{{#type.isCollection}}
{{modelName}}.set{{namePascalCase}}(
this.{{name}} != null
? this.{{name}}.stream()
.map({{type.baseType}}::toModel)
.collect(Collectors.toList())
: Collections.emptyList()
);
{{/type.isCollection}}{{^type.isCollection}}
{{modelName}}.set{{namePascalCase}}(this.{{name}} != null ? this.{{name}}.toModel() : null);
{{/type.isCollection}}
{{/isRecord}}
{{^isRecord}}
{{modelName}}.set{{namePascalCase}}(this.{{name}});
{{/isRecord}}
{{/fields}}
return {{modelName}};
}
}

View File

@ -26,7 +26,9 @@ import java.util.Optional;
@Repository
public interface {{record_type}}Repository extends JpaRepository<{{record_type}}, {{pkey_type.name}}> {
List<{{record_type}}> findAllByIdIn(List<{{pkey_type.name}}> ids);
List<{{record_type}}> findAllBy{{pkeyPascalCase}}In(List<{{pkey_type.name}}> ids);
{{record_type}} findBy{{pkeyPascalCase}}(@NonNull {{pkey_type.name}} {{pkey}});
{{#indexes}}
List<{{record_type}}> findAllBy{{namePascalCase}}(@NonNull {{type.name}} {{name}});