1

I'm using JavaFX+Hibernate to create a desktop app,however after the user logged in with his/her username, I want the username to be assigned to a global variable similar to PHP $_SESSION variable that can be accessible from anywhere.

this is my code for the HibernateUtil.java class

package models;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    public static SessionFactory buildSessionFactory(){

     try{
        // Create the SessionFactory from hibernate.cfg.xml
         System.out.println("creating SessionFactory using HibernateUtil.java");
         SessionFactory f = new Configuration().configure().buildSessionFactory();
         System.out.println("session created successfully");
         return f;
     } catch (Throwable ex){
        // Make sure you log the exception, as it might be swallowed
         System.err.println("Initial SessionFactory creation failed." + ex);
         throw new ExceptionInInitializerError(ex);  

     }

    }

 public static SessionFactory getSessionFactory() {
        return sessionFactory;
 }

}

3 Answers 3

1

Java's equivalent of a "global variable" may be a public accessible static variable. You could create one in your code as

 public class Globals {
     public static String userName;
 }

And the username can be set and read from Globals.userName.

Note: Using a private field and get and set methods would be more clean....

Sign up to request clarification or add additional context in comments.

2 Comments

This is bad practice, and probably shouldn't be suggested here, even with the caveat you provide.
I take this answer as Yes because it's it answers the alternative to $_SESSION in PHP in Java ,but I will use either dependency injection or create my own method with private variables .
1

Don't use "global data". Instead, just arrange to pass the username (and other data) to whoever needs access to it. (In other words, use dependency injection, in some form.)

One (of many) problems with global data is that you restrict yourself to only ever having one value for that variable. Suppose in a few months your boss comes to your office and says something like "Our clients love the application you wrote, but many of them have multiple accounts that they want to access at the same time. Can you adapt the app so that they can open multiple tabs and log into each account in a different tab?". (This exact situation might not apply to you, but the general principle does.) If you have made the username a single global variable in your application, it will be virtually impossible to make this change without rewriting arbitrarily large portions of the code.

See, e.g. http://wiki.c2.com/?GlobalVariablesAreBad, https://softwareengineering.stackexchange.com/questions/148108/why-is-global-state-so-evil, and many others that you can search for, for other reasons not to do this.

4 Comments

thanks James,yes I understand you ,but in my case it's a desktop App not a web server app,so the "global variable data" will be stored in the client computer RAM ,so global should be fine in my case right ? please advice
@OsamaAl-Banna Absolutely not. The advice on global data applies to software design in general. The hypothetical scenario I suggested was intended to refer to a desktop application. Don't use global data.
what about creating my own method to store the username and the username variable will be private ? I think the app will be still can handle multiple user right ?
Where will the method be defined, and how will other parts of your application access it? That is a good idea (actually, you are talking about defining a model, in MVC terms), but you still have the same problem you started with: you have to provide the object defining those methods to other parts of your application (use dependency injection...).
1

Hibernate Session is absolutely different from what you think.

As long as you have a desktop application you have to care what you need to keep in memory. You can create your own kind of Context or "Session" if you'd like name it that way and keep there what you need.

Analog for PHP $_SESSION exists in J2EE Web application with Servlet API

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.