apache / netbeans

Apache NetBeans
https://netbeans.apache.org/
Apache License 2.0
2.59k stars 833 forks source link

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

Open marcelperezrubin opened 2 months ago

marcelperezrubin commented 2 months ago

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<Usuario> findUsuarioEntities() {
    return findUsuarioEntities(true, -1, -1);
}

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

private List<Usuario> 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<Usuario> 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:

/*

import java.io.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

matthiasblaesing commented 2 months ago

It would be helpful if you could test the current release candidate for NB22:

https://ci-builds.apache.org/job/Netbeans/job/netbeans-TLP/job/release220/8/artifact/dist/

There were fixes for the javax/jakarta transition.

If you don't find this fixed, please provide steps to reproduce.

marcelperezrubin commented 2 months ago

Hi Matthias thank you so much for your email,

I really appreciate it, I would like to try it, do you have it for Mac?

Kind regards

El vie, 3 may 2024 a las 18:06, Matthias Bläsing @.***>) escribió:

It would be helpful if you could test the current release candidate for NB22:

https://ci-builds.apache.org/job/Netbeans/job/netbeans-TLP/job/release220/8/artifact/dist/

There were fixes for the javax/jakarta transition.

If you don't find this fixed, please provide steps to reproduce.

— Reply to this email directly, view it on GitHub https://github.com/apache/netbeans/issues/7349#issuecomment-2093307172, or unsubscribe https://github.com/notifications/unsubscribe-auth/A4OUTZWTDWPSOMLQ55SXWMLZAOYXXAVCNFSM6AAAAABHEIZ47GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJTGMYDOMJXGI . You are receiving this because you authored the thread.Message ID: @.***>

matthiasblaesing commented 2 months ago

mac OS is kind of hard to build for, so for RCs there are no notarized builds available. In the folder netbeans the binary zip is available. On other OSes, it is as easy as unzipping the file, maybe setting the netbeans_jdkhome in etc/netbeans.conf and running bin/netbeans, bin/netbeans.exe or bin/netbeans64.exe. You might need to fiddle with file attributes after unzipping.

matthiasblaesing commented 2 months ago

The binary zip is [netbeans-22-rc2-bin.zip](https://ci-builds.apache.org/job/Netbeans/job/netbeans-TLP/job/release220/8/artifact/dist/netbeans/netbeans-22-rc2-bin.zip)

neilcsmith-net commented 2 months ago

@matthiasblaesing there is normally a notarized macOS installer for one of the RCs. It's an important test. Unfortunately this is delayed until RC3 as per the problem at https://github.com/apache/netbeans/pull/7268#issuecomment-2090730648

The correct link for RCs is #7281 The Jenkins link should not be shared outside the dev@ list.

marcelperezrubin commented 1 month ago

Is it possible to transfer or copy the "JPA Controller Classes from Entity Classes" template from NetBeans 19 to NetBeans 21? If so, could you provide guidance on how to achieve this?

Thank you for your assistance.

matthiasblaesing commented 1 month ago

@marcelperezrubin as already stated: This might be fixed in NB22. So please state how to reproduce the problem, then I'm willing to have a look. Additionally it might be worth testing NB22 RC4, as already stated the issue might already be fixed.

marcelperezrubin commented 1 month ago

Fixed! Finally, I found a solution

Summary of Steps to Resolve Empty JPA Controllers in NetBeans 21 on MacBook m1

To resolve this, I followed these steps:

  1. Update Jakarta EE Version:

    • Updated the jakartaee property to version 9.1 or higher. This is crucial because Jakarta EE 8 still uses the javax namespace, whereas Jakarta EE 9 and above use the jakarta namespace. Ensuring the correct namespace compatibility was essential for the proper generation of JPA controllers.
  2. Add JPA Dependencies:

    • Explicitly added dependencies for the Jakarta Persistence API (JPA) to the project. This step was necessary to ensure that all required classes and methods for JPA were available in the project's classpath.
  3. Add JAX-RS Dependencies:

    • Added dependencies for Jakarta RESTful Web Services (JAX-RS). While not directly related to JPA controllers, this ensures the project includes comprehensive support for RESTful services, which might be interdependent with other parts of the project.
  4. Include maven-war-plugin:

    • Included the maven-war-plugin in the project if it wasn't already present. This plugin is crucial for packaging the application correctly, ensuring that all dependencies and resources are included during the build process, which can prevent issues related to incomplete builds.

These steps ensured that the project configuration was aligned with the requirements of Jakarta EE 9.1 and above, resolving the issue of empty JPA controllers by making sure all necessary dependencies and configurations were correctly set up.

I hope this summary help !

I am also looking forward to trying NB22, thanks for your work :)

Kind Regards

El El dom, 19 may 2024 a las 15:20, Matthias Bläsing < @.***> escribió:

@marcelperezrubin https://github.com/marcelperezrubin as already stated: This might be fixed in NB22. So please state how to reproduce the problem, then I'm willing to have a look. Additionally it might be worth testing NB22 RC4, as already stated the issue might already be fixed.

— Reply to this email directly, view it on GitHub https://github.com/apache/netbeans/issues/7349#issuecomment-2119236879, or unsubscribe https://github.com/notifications/unsubscribe-auth/A4OUTZSY54LAUSAYW6Q5KH3ZDCRJHAVCNFSM6AAAAABHEIZ47GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJZGIZTMOBXHE . You are receiving this because you were mentioned.Message ID: @.***>