Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,16 @@ private void generateEntityRepository(String _package, Entity entity, File outpu
Map<String, Object> dataModel = createEntityDataModel(model, entity, _package, domainLayer, repositoryLayer);
dataModel.put("model", model);
dataModel.put("package", repositoryPackage);
dataModel.put("cdi", true);
dataModel.put("named", false);
dataModel.put("AbstractRepository", "Abstract" + firstUpper(repositoryLayer));
dataModel.put("AbstractRepository_FQN", _package + "." + repositoryLayer + "." + "Abstract" + firstUpper(repositoryLayer));

processTemplateToFile(cfg, "EntityRepository.java.ftl", dataModel, outputDir, dataModel.get("EntityRepository") + ".java");
if (model.getJakartaVersion() > 10) {
processTemplateToFile(cfg, "EntityDataRepository.java.ftl", dataModel, outputDir, dataModel.get("EntityRepository") + ".java");
} else {
dataModel.put("cdi", true);
dataModel.put("named", false);
dataModel.put("AbstractRepository", "Abstract" + firstUpper(repositoryLayer));
dataModel.put("AbstractRepository_FQN", _package + "." + repositoryLayer + "." + "Abstract" + firstUpper(repositoryLayer));
processTemplateToFile(cfg, "EntityRepository.java.ftl", dataModel, outputDir, dataModel.get("EntityRepository") + ".java");
}
} catch (IOException | TemplateException e) {
e.printStackTrace();
}
Expand All @@ -325,7 +329,9 @@ private void generateRestBase(Map<String, Object> dataModel, String _package, Fi
}

private void generateRepositoryBase(Map<String, Object> dataModel, String _package, File outputDir) {

if (model.getJakartaVersion() > 10) {
return; // No AbstractRepository needed for Jakarta Data
}
dataModel.put("package", _package + "." + repositoryLayer);
dataModel.put("cdi", true);
dataModel.put("AbstractRepository", "Abstract" + firstUpper(repositoryLayer));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<#--
Copyright 2024 the original author or authors from the Jeddict project (https://jeddict.github.io/).
Copyright 2024-2026 the original author or authors from the Jeddict project (https://jeddict.github.io/).

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
Expand Down Expand Up @@ -39,25 +39,37 @@ public class ${beanClass} implements Serializable {
}

public List<${EntityClass}> getAll${EntityClassPlural}() {
<#if model.jakartaVersion gt 10>
return ${entityRepository}.findAll().toList();
<#else>
return ${entityRepository}.findAll();
</#if>
}

public String create() {

return null;
}
public String save() {
<#if model.jakartaVersion gt 10>
${entityRepository}.save(${entityInstance});
<#else>
if (${entityInstance}.${pkGetter}() == null) {
${entityRepository}.create(${entityInstance});
} else {
${entityRepository}.edit(${entityInstance});
}
</#if>
${entityInstance} = new ${EntityClass}(); // reset
return null;
}

public String remove(${pkType} ${pkName}) {
<#if model.jakartaVersion gt 10>
${entityRepository}.deleteById(${pkName});
<#else>
${entityRepository}.remove(${entityRepository}.find(${pkName}));
</#if>
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ public class ${entityConverterClass} implements Converter<${instanceType}> {
if (value == null || value.isEmpty()) {
return null;
}
<#if model.jakartaVersion gt 10>
return ${entityRepository}.findById(${pkType}.valueOf(value)).orElse(null);
<#else>
return ${entityRepository}.find(${pkType}.valueOf(value));
</#if>
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<#--
Copyright (c) 2026 Payara Foundation and/or its affiliates. All rights reserved.

The contents of this file are subject to the terms of either the GNU
General Public License Version 2 only ("GPL") or the Common Development
and Distribution License("CDDL") (collectively, the "License"). You
may not use this file except in compliance with the License. You can
obtain a copy of the License at
https://github.com/payara/Payara/blob/master/LICENSE.txt
See the License for the specific
language governing permissions and limitations under the License.

When distributing the software, include this License Header Notice in each
file and include the License file at glassfish/legal/LICENSE.txt.

GPL Classpath Exception:
The Payara Foundation designates this particular file as subject to the "Classpath"
exception as provided by the Payara Foundation in the GPL Version 2 section of the License
file that accompanied this code.

Modifications:
If applicable, add the following below the License Header, with the fields
enclosed by brackets [] replaced by your own identifying information:
"Portions Copyright [year] [name of copyright owner]"

Contributor(s):
If you wish your version of this file to be governed by only the CDDL or
only the GPL Version 2, indicate your decision by adding "[Contributor]
elects to include this software in this distribution under the [CDDL or GPL
Version 2] license." If you don't indicate a single choice of license, a
recipient has the option to distribute your version of this file under
either the CDDL, the GPL Version 2 or to extend the choice of license to
its licensees as provided above. However, if you add GPL Version 2 code
and therefore, elected the GPL Version 2 license, then the option applies
only if the new code is made subject to such option by the copyright
holder.
-->
package ${package};

import ${EntityClass_FQN};
import ${model.importPrefix}.data.repository.CrudRepository;
import ${model.importPrefix}.data.repository.Repository;

@Repository
public interface ${EntityRepository} extends CrudRepository<${EntityClass}, ${EntityPKClass}> {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<#--
Copyright 2024 the original author or authors from the Jeddict project (https://jeddict.github.io/).
Copyright 2024-2026 the original author or authors from the Jeddict project (https://jeddict.github.io/).

Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy of
Expand Down Expand Up @@ -99,15 +99,23 @@ public class ${controllerClass} {
<#if attribute.multi>
<#else>
if (${instanceName}.get${attribute.getTitleCaseName()}() != null && ${instanceName}.get${attribute.getTitleCaseName()}().get${model.getEntity(attribute.type).getPrimaryKeyFirstUpperName()}() != null) {
<#if model.jakartaVersion gt 10>
${instanceName}.set${attribute.getTitleCaseName()}(${attribute.name}${EntityRepositorySuffix}.findById(${instanceName}.get${attribute.getTitleCaseName()}().get${model.getEntity(attribute.type).getPrimaryKeyFirstUpperName()}()).orElse(null));
<#else>
${instanceName}.set${attribute.getTitleCaseName()}(${attribute.name}${EntityRepositorySuffix}.find(${instanceName}.get${attribute.getTitleCaseName()}().get${model.getEntity(attribute.type).getPrimaryKeyFirstUpperName()}()));
</#if>
} else {
${instanceName}.set${attribute.getTitleCaseName()}(null);
}
</#if>
<#else>
</#if>
</#list>
<#if model.jakartaVersion gt 10>
${entityRepository}.save(${instanceName});
<#else>
${entityRepository}.create(${instanceName});
</#if>
return HeaderUtil.createEntityCreationAlert(Response.created(new URI("/${applicationPath}/api/${entityApiUrl}/" + ${instanceName}.${pkGetter}())),
ENTITY_NAME, <#if isPKPrimitive>String.valueOf(${instanceName}.${pkGetter}())<#elseif pkType == "String">${instanceName}.${pkGetter}()<#else>${instanceName}.${pkGetter}().toString()</#if>)
.entity(${instanceName}).build();
Expand Down Expand Up @@ -137,15 +145,23 @@ public class ${controllerClass} {
<#if attribute.multi>
<#else>
if (${instanceName}.get${attribute.getTitleCaseName()}() != null && ${instanceName}.get${attribute.getTitleCaseName()}().get${model.getEntity(attribute.type).getPrimaryKeyFirstUpperName()}() != null) {
<#if model.jakartaVersion gt 10>
${instanceName}.set${attribute.getTitleCaseName()}(${attribute.name}${EntityRepositorySuffix}.findById(${instanceName}.get${attribute.getTitleCaseName()}().get${model.getEntity(attribute.type).getPrimaryKeyFirstUpperName()}()).orElse(null));
<#else>
${instanceName}.set${attribute.getTitleCaseName()}(${attribute.name}${EntityRepositorySuffix}.find(${instanceName}.get${attribute.getTitleCaseName()}().get${model.getEntity(attribute.type).getPrimaryKeyFirstUpperName()}()));
</#if>
} else {
${instanceName}.set${attribute.getTitleCaseName()}(null);
}
</#if>
<#else>
</#if>
</#list>
<#if model.jakartaVersion gt 10>
${entityRepository}.save(${instanceName});
<#else>
${entityRepository}.edit(${instanceName});
</#if>
return HeaderUtil.createEntityUpdateAlert(Response.ok(), ENTITY_NAME, <#if isPKPrimitive>String.valueOf(${instanceName}.${pkGetter}())<#else>${instanceName}.${pkGetter}().toString()</#if>)
.entity(${instanceName}).build();
}
Expand All @@ -167,7 +183,11 @@ public class ${controllerClass} {
<#if pagination == "no">
public List<${instanceType}> getAll${EntityClassPlural}() {
LOG.log(Level.FINE, "REST request to get all ${EntityClassPlural}");
<#if model.jakartaVersion gt 10>
List<${EntityClass}> ${entityInstancePlural} = ${entityRepository}.findAll().toList();
<#else>
List<${EntityClass}> ${entityInstancePlural} = ${entityRepository}.findAll();
</#if>
return ${entityInstancePlural};
}
<#else>
Expand Down Expand Up @@ -195,10 +215,16 @@ public class ${controllerClass} {
@Produces(MediaType.APPLICATION_JSON)
public Response get${EntityClass}(@PathParam("${pkName}") ${pkType} ${pkName}) {
LOG.log(Level.FINE, "REST request to get ${EntityClass} : {}", ${pkName});
<#if model.jakartaVersion gt 10>
return ${entityRepository}.findById(${pkName})
.map(res -> Response.status(Response.Status.OK).entity(res).build())
.orElse(Response.status(Response.Status.NOT_FOUND).build());
<#else>
${instanceType} ${instanceName} = ${entityRepository}.find(${pkName});
return Optional.ofNullable(${instanceName})
.map(res -> Response.status(Response.Status.OK).entity(${instanceName}).build())
.orElse(Response.status(Response.Status.NOT_FOUND).build());
</#if>
}

/**
Expand All @@ -215,8 +241,12 @@ public class ${controllerClass} {
@Path("/{${pkName}}")
public Response remove${EntityClass}(@PathParam("${pkName}") ${pkType} ${pkName}) {
LOG.log(Level.FINE, "REST request to delete ${EntityClass} : {}", ${pkName});
<#if model.jakartaVersion gt 10>
${entityRepository}.deleteById(${pkName});
<#else>
${entityRepository}.remove(${entityRepository}.find(${pkName}));
</#if>
return HeaderUtil.createEntityDeletionAlert(Response.ok(), ENTITY_NAME, <#if isPKPrimitive>String.valueOf(${pkName})<#else>${pkName}.toString()</#if>).build();
}

}
}