apache / apache/netbeans

Wizard for inserting the code to use an EntityManager does not generate good code

Open
#4,839 0 comments 0 reactions 0 assignees View on GitHub
Java EE/Jakarta EE kind:bug
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Apache NetBeans version

Apache NetBeans 16 release candidate

### What happened

In an EJB, Alt+Insert > Use Entity Manager... generate these lines:
```java
@PersistenceContext(unitName = "xxxxx")
private EntityManager em;
@javax.annotation.Resource
private javax.transaction.UserTransaction utx;

public void persist(Object object) {
try {
utx.begin();
em.persist(object);
utx.commit();
} catch (Exception e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
throw new RuntimeException(e);
}
}
```
The bad namespace javax is used if the project is a Jakarta EE 9.1 project. Overall, it is not the good code when the transactions are managed by the EJB (value by default for an EJB; it would be the good code if the EJB were annotated with `@TransactionManagement(TransactionManagemenType.BEAN)`)
The good code:
```java
@PersistenceContext(unitName = "xxxxx")
private EntityManager em;

public void persist(Object object) {
em.persist(object);
}
```

### How to reproduce

Create a Maven Web application project.
Write a persistence.xml file.
Create an EJB.
In an EJB, Alt+Insert > Use Entity Manager..

### Did this work correctly in an earlier version?

Apache NetBeans 12.6 or earlier

### Operating System

Windows 11 Family 64-bit, version 21H2

### JDK

11

### Apache NetBeans packaging

Apache NetBeans provided installer

### Anything else

I don't remember in which earlier version the good code was generated.

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

No

### Code of Conduct

Yes

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue in a Maven Web application with persistence.xml and an EJB, using Alt+Insert > Use Entity Manager. Inspect the wizard's generated code and how it identifies Jakarta EE 9.1 projects and EJB transaction management. Done means the generated imports and transaction handling match the project and EJB configuration, including the shown container-managed case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.