diff --git a/starter-generator/src/main/java/fish/payara/starter/application/generator/CRUDAppGenerator.java b/starter-generator/src/main/java/fish/payara/starter/application/generator/CRUDAppGenerator.java index aaed0f19..e8170485 100644 --- a/starter-generator/src/main/java/fish/payara/starter/application/generator/CRUDAppGenerator.java +++ b/starter-generator/src/main/java/fish/payara/starter/application/generator/CRUDAppGenerator.java @@ -304,12 +304,16 @@ private void generateEntityRepository(String _package, Entity entity, File outpu Map 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(); } @@ -325,7 +329,9 @@ private void generateRestBase(Map dataModel, String _package, Fi } private void generateRepositoryBase(Map 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)); diff --git a/starter-generator/src/main/resources/template/jsf/EntityBean.java.ftl b/starter-generator/src/main/resources/template/jsf/EntityBean.java.ftl index e2f296a1..d40effe5 100644 --- a/starter-generator/src/main/resources/template/jsf/EntityBean.java.ftl +++ b/starter-generator/src/main/resources/template/jsf/EntityBean.java.ftl @@ -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 @@ -39,7 +39,11 @@ public class ${beanClass} implements Serializable { } public List<${EntityClass}> getAll${EntityClassPlural}() { +<#if model.jakartaVersion gt 10> + return ${entityRepository}.findAll().toList(); +<#else> return ${entityRepository}.findAll(); + } public String create() { @@ -47,17 +51,25 @@ public class ${beanClass} implements Serializable { 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}); } + ${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})); + return null; } diff --git a/starter-generator/src/main/resources/template/jsf/EntityConverter.java.ftl b/starter-generator/src/main/resources/template/jsf/EntityConverter.java.ftl index 697f16fb..58606a77 100644 --- a/starter-generator/src/main/resources/template/jsf/EntityConverter.java.ftl +++ b/starter-generator/src/main/resources/template/jsf/EntityConverter.java.ftl @@ -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)); + } @Override diff --git a/starter-generator/src/main/resources/template/repository/EntityDataRepository.java.ftl b/starter-generator/src/main/resources/template/repository/EntityDataRepository.java.ftl new file mode 100644 index 00000000..4eb3ffdb --- /dev/null +++ b/starter-generator/src/main/resources/template/repository/EntityDataRepository.java.ftl @@ -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}> { + +} diff --git a/starter-generator/src/main/resources/template/rest/EntityController.java.ftl b/starter-generator/src/main/resources/template/rest/EntityController.java.ftl index 3487a573..51999057 100644 --- a/starter-generator/src/main/resources/template/rest/EntityController.java.ftl +++ b/starter-generator/src/main/resources/template/rest/EntityController.java.ftl @@ -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 @@ -99,7 +99,11 @@ 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()}())); + } else { ${instanceName}.set${attribute.getTitleCaseName()}(null); } @@ -107,7 +111,11 @@ public class ${controllerClass} { <#else> +<#if model.jakartaVersion gt 10> + ${entityRepository}.save(${instanceName}); +<#else> ${entityRepository}.create(${instanceName}); + 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()) .entity(${instanceName}).build(); @@ -137,7 +145,11 @@ 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()}())); + } else { ${instanceName}.set${attribute.getTitleCaseName()}(null); } @@ -145,7 +157,11 @@ public class ${controllerClass} { <#else> +<#if model.jakartaVersion gt 10> + ${entityRepository}.save(${instanceName}); +<#else> ${entityRepository}.edit(${instanceName}); + return HeaderUtil.createEntityUpdateAlert(Response.ok(), ENTITY_NAME, <#if isPKPrimitive>String.valueOf(${instanceName}.${pkGetter}())<#else>${instanceName}.${pkGetter}().toString()) .entity(${instanceName}).build(); } @@ -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(); + return ${entityInstancePlural}; } <#else> @@ -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()); + } /** @@ -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})); + return HeaderUtil.createEntityDeletionAlert(Response.ok(), ENTITY_NAME, <#if isPKPrimitive>String.valueOf(${pkName})<#else>${pkName}.toString()).build(); } -} +} \ No newline at end of file