apache / apache/netbeans

Suggestion to Implement CRUD Methods in JPA Controller Templates for NetBeans 21

Open
#7,349 11 comments 0 reactions 0 assignees View on GitHub
kind:feature needs:triage
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Description

Hello NetBeans Team,

I've been working with NetBeans IDE, specifically transitioning between version 19 and version 21, and I noticed a significant change in the way JPA Controller classes are generated. In NetBeans 19, the JPA Controller template included CRUD methods (create, read, update, delete) which are essential for basic database operations. However, in NetBeans 21, these methods seem to be missing in the generated code.

This change has impacted the ease of use and the efficiency of starting new projects with JPA, as developers now need to manually add these CRUD methods, which was previously automated.

Example of generated JPA Controller in NetBeans 19:

package persistencia;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import logica.Usuario;
import persistencia.exceptions.NonexistentEntityException;

public class UsuarioJpaController implements Serializable {

public UsuarioJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;

public EntityManager getEntityManager() {
return emf.createEntityManager();
}

public void create(Usuario usuario) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(usuario);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}

public void edit(Usuario usuario) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
usuario = em.merge(usuario);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
int id = usuario.getId();
if (findUsuario(id) == null) {
throw new NonexistentEntityException("The usuario with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}

public void destroy(int id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Usuario usuario;
try {
usuario = em.getReference(Usuario.class, id);
usuario.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The usuario with id " + id + " no longer exists.", enfe);
}
em.remove(usuario);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}

public List findUsuarioEntities() {
return findUsuarioEntities(true, -1, -1);
}

public List findUsuarioEntities(int maxResults, int firstResult) {
return findUsuarioEntities(false, maxResults, firstResult);
}

private List findUsuarioEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Usuario.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}

public Usuario findUsuario(int id) {
EntityManager em = getEntityManager();
try {
return em.find(Usuario.class, id);
} finally {
em.close();
}
}

public int getUsuarioCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root rt = cq.from(Usuario.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}

}

Generated Jpa Controller code in NetBeans 21:

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package persistencia;

import java.io.Serializable;

/**
*
* @author marcelrubin
*/
public class UsuarioJpaController implements Serializable {

}

Could the team consider re-implementing the CRUD methods in the JPA Controller templates for NetBeans 21? This feature was very useful in accelerating development and improving the out-of-the-box functionality of the IDE.

Thank you for considering this suggestion. I believe it would enhance the productivity of many developers who rely on NetBeans for Java EE and JPA projects.

Best regards,

Marcel

### Use case/motivation

_No response_

### Related issues

_No response_

### Are you willing to submit a pull request?

No

Contributor guide

Open the contributing guide

Research direction

Start by locating the NetBeans entry point that generates the JPA Controller and compare its current output with the NetBeans 19 example shown in the issue. Restore generation of the create, edit, destroy, find, and count methods, then verify that a generated UsuarioJpaController includes the requested CRUD behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.