Package Database

Class Hibernate


  • public class Hibernate
    extends java.lang.Object
    Class that is used to connect to the database
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static javax.persistence.EntityManager em  
      private static javax.persistence.EntityManagerFactory entityManagerFactory  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Hibernate()
      Private constructor to hinder creation of utility class
    • Method Summary

      Modifier and Type Method Description
      static void deleteUser​(java.lang.String username)
      Delete user.
      static javax.persistence.EntityManager getEm()
      Gets entity manager.
      static javax.persistence.EntityManagerFactory getEntityManagerFactory()
      Gets entity manager factory.
      private static java.util.Map<java.lang.String,​java.lang.String> getProperties()
      Sets up the password, username and url to the database
      static java.lang.String getSalt​(java.lang.String username)
      Gets salt of user.
      static User getUser​(java.lang.String username)
      Gets user.
      static boolean login​(java.lang.String username, java.lang.String hash)
      Login user
      static boolean registerUser​(java.lang.String username, java.lang.String hash, java.lang.String salt)
      Register a new user in the database
      static void setupDatabase()
      Method that sets up the database for the application.
      static void updateUser​(User user)
      Update user.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • entityManagerFactory

        private static javax.persistence.EntityManagerFactory entityManagerFactory
      • em

        private static javax.persistence.EntityManager em
    • Constructor Detail

      • Hibernate

        private Hibernate()
        Private constructor to hinder creation of utility class
    • Method Detail

      • getEm

        public static javax.persistence.EntityManager getEm()
        Gets entity manager. Creates a new one if the connection is not open anymore or the entity manager is null
        Returns:
        the entity manager
      • getEntityManagerFactory

        public static javax.persistence.EntityManagerFactory getEntityManagerFactory()
        Gets entity manager factory. Creates a new one if the connection is not open anymore or the entity manager factory is null
        Returns:
        the entity manager factory
      • getProperties

        private static java.util.Map<java.lang.String,​java.lang.String> getProperties()
        Sets up the password, username and url to the database
        Returns:
        a map with the password, username and url
      • registerUser

        public static boolean registerUser​(java.lang.String username,
                                           java.lang.String hash,
                                           java.lang.String salt)
        Register a new user in the database
        Parameters:
        username - the username
        hash - the hash
        salt - the salt
        Returns:
        if the registration was successful
      • updateUser

        public static void updateUser​(User user)
        Update user.
        Parameters:
        user - the user to update.
      • getSalt

        public static java.lang.String getSalt​(java.lang.String username)
                                        throws javax.persistence.NoResultException
        Gets salt of user.
        Parameters:
        username - username of user.
        Returns:
        the salt.
        Throws:
        javax.persistence.NoResultException - if the user was not found.
      • getUser

        public static User getUser​(java.lang.String username)
        Gets user.
        Parameters:
        username - username of user.
        Returns:
        the user.
      • login

        public static boolean login​(java.lang.String username,
                                    java.lang.String hash)
        Login user
        Parameters:
        username - the username.
        hash - the hash.
        Returns:
        if login was successful.
      • deleteUser

        public static void deleteUser​(java.lang.String username)
        Delete user.
        Parameters:
        username - the username.
      • setupDatabase

        public static void setupDatabase()
        Method that sets up the database for the application.