How use EntityManagerFactory just one time? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How use EntityManagerFactory just one time?

When i run app my login form opens and after insert data click ok to open my main form, so i open EntityManagerFactory two time. First in login form and second in main form. How can open just one time?

20th Oct 2019, 4:40 PM
hamid
hamid - avatar
3 Answers
+ 2
try implement singleton design pattern to your EntityManagerFactory. singleton limit a class to only have a single instance, but you can call/use that instance almost everywhere as long as you have the access to that class
20th Oct 2019, 5:18 PM
Taste
Taste - avatar
0
thank you taste can you write that ?
26th Oct 2019, 10:43 AM
hamid
hamid - avatar
0
its been a while since i code in java so make an ajustment yourself if anything is wrong. make the constructor private. it you dont have one create default constructor in private. this will make sure that no object can be created again from that class. next follow my code below private static ClassName instance; public static ClassName(){ if(instance == null) instance = new ClassName(); return instance; } just change ClassName to your class. to use it just call ClassName.get();
26th Oct 2019, 10:53 AM
Taste
Taste - avatar