This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 241650

Summary: use entity manager should generate EntityManagerFactory attribute
Product: javaee Reporter: catweasle <catweasle>
Component: PersistenceAssignee: Sergey Petrov <sj-nb>
Status: RESOLVED WONTFIX    
Severity: normal    
Priority: P3    
Version: 7.4   
Hardware: PC   
OS: All   
Issue Type: ENHANCEMENT Exception Reporter:

Description catweasle 2014-02-10 16:09:09 UTC
Pressing ALT+INSERT in a java SE class generates a method

	public static void persist(Object object) {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("APU");
		EntityManager em = emf.createEntityManager();
                ...}

Calling this method twice will cause a new factory to be created for each persist.
With a policy create-and-drop the second persist will effectively destroy the data from the first persist.
As you need to create this factory only once I suggest to generate this code as:

private  static EntityManagerFactory emf = Persistence.createEntityManagerFactory("APU");

	public static void persist(Object object) {
		EntityManager em = emf.createEntityManager();
                ...}
Comment 1 Sergey Petrov 2014-02-10 20:39:21 UTC
yes, it's not the best approach, but static field isn't good too, in my opinion, and anyway lifecycle of EntityManager isn't handled and depends on user requirenment  and is likely will require more work. this methd is more like a draft user can rely on.
moreover "several invocation", I see no much reason to support it better as all methds do the same if I got it right.
Comment 2 catweasle 2014-02-10 22:59:42 UTC
Fair enough, if you don't like the EntityManagerFactory to be static, make it non static, at least it would survive over different persist(Object o) calls.
I do not have a problem with EntityManager being created for each persist.
Several invocations example:
persist(new User("John","Deer"));
Persist(new Car("Firebird","V6");
...
Comment 3 Martin Balin 2016-07-07 08:52:53 UTC
This old bug may not be relevant anymore. If you can still reproduce it in 8.2 development builds please reopen this issue.

Thanks for your cooperation,
NetBeans IDE 8.2 Release Boss