Blog

Spring Hibernate Jars

MySQL Connection Jars

http://www.java2s.com/Code/Jar/c/Downloadcommysqljdbc515jar.htm

http://www.java2s.com/Code/Jar/m/Downloadmysqlconnectorjava5123binjar.htm

Bcrypt Password Jars

http://www.java2s.com/Code/Jar/s/Downloadspringsecuritycrypto310RELEASEjar.htm

Why String is Immutable in Java

Lets break it into some parts

String s1 = “hello”;
This Statement creates string containing hello and occupy space in memory i.e. in Constant String Pool and and assigned it to reference object s1

String s2 = s1;
This statement assigns the same string hello to new reference s2

         _______
        |       |
s1 ---->| hello |<----- s2
        |_______|

Both references are pointing to the same string so output the same value as follows.

out.println(s1); // o/p: hello
out.println(s2); // o/p: hello
Though String is immutable, assignment can be possible so the s1 will now refer to new value stack.

s1 = “stack”;

         _________
        |         |
s1 ---->| stack   |
        |_________|

But what about s2 object which is pointing to hello it will be as it is.

         __________
        |          |
s2 ---->| hello    |
        |__________|

out.println(s1); // o/p: stack
out.println(s2); // o/p: hello
Since String is immutable Java Virtual Machine won’t allow us to modify string s1 by its method. It will create all new String object in pool as follows.

s1.concat(” overflow”);

                 ___________________
                |                   |
s1.concat ----> | stack overflow    |
                |___________________|

out.println(s1); // o/p: stack
out.println(s2); // o/p: hello
out.println(s1.concat); // o/p: stack overflow
Note if String would be mutable then the output would have been

out.println(s1); // o/p: stack overflow
Now you might be surprised why String has such methods like concat() to modify. Following snippet will clear your confusion.

s1 = s1.concat(” overflow”);
Here we are assigning modified value of string back to s1 reference.

         ___________________
        |                   |
s1 ---->| stack overflow    |
        |___________________|

out.println(s1); // o/p: stack overflow
out.println(s2); // o/p: hello
That’s why Java decided String to be a final class Otherwise anyone can modify and change the value of string. Hope this will help little bit.

Spring Web MVC Hello World

 

Git Clone Link: https://gitlab.com/shaileshsonare/spring_helloworld.git

Step 1. Create new Project

Step 2. Name the Project and select location

 

Step 3. Select Server and Settings

 

Step 4. Select Framework to include in Project’s library and include JSTL (Java Server Pages Tag Library)

 

Step 5. Project Created Successfully

Step 6. Check Welcome file. This file will display on browser if we enter only Project name in url.

e.g. http://localhost:8080/ProjectName/

Step 7. This section will decide which url pattern will be consider as Spring Requests and which url is not a part of Spring Framework.

Here all *.htm (URL ending with .htm from browser) will be consider as Spring request.

 

Step 8. You can run normal jsp pages also from browser which will not be part of Spring framework.

 

Step 9. Create new jsp file as

Right Click on WebPages >> New >> Jsp File

 

Step 10. Run on Browser it will run as old traditional jsp project file.

 

Hibernate CRUD with MySQL Database – Java

 

To Update Record Using Hibernate Session:

Configuration cfg = new Configuration().configure("hibernate.cfg.xml"); 

StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()); 

SessionFactory factory = cfg.buildSessionFactory(ssrb.build()); Session session = factory.openSession(); 

Transaction t = session.beginTransaction(); 

Users u = (Users) session.load(Users.class, 6); // load record having id 6 into u object

u.setLastName("Nitnavre"); // set column value which needs to update

System.out.println(session.save(u)); // fire update query using save function of hibernate

t.commit(); // commit the update changes to save state in database

System.out.println("Record updated successfully...");

 

Ref Link:

  1. https://www.journaldev.com/3481/hibernate-session-merge-vs-update-save-saveorupdate-persist-example

Creating Hibernate Session with Database

  1. model.dao.interfaces.EmployeeDAOInterface.java

 

package model.dao.interfaces;

import java.util.List;
import model.Employee;

/**
 *
 * @author Shailesh Sonare
 */
public interface EmployeeDAOInterface {
    public List getAllEmplyees();
}

 

2. model.dao.EmployeeDAO.java

 

package model.dao;

import java.util.Iterator;
import java.util.List;
import model.Employee;
import model.dao.interfaces.EmployeeDAOInterface;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Shailesh Sonare
 */
public class EmployeeDAO implements EmployeeDAOInterface {

    @Override
    public List getAllEmplyees() {
        Configuration cnf = new Configuration();
        cnf.configure("hibernate.cfg.xml");
        
        StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cnf.getProperties());
        
        SessionFactory sf = cnf.buildSessionFactory(ssrb.build());
        Session session = sf.openSession();
        
        String hql = "FROM Employee where id = 2";
        
        List list = session.createQuery(hql).list();
        
        for(Iterator iterator = list.iterator(); iterator.hasNext();) {
            Employee e = (Employee)iterator.next();
            
            System.out.println(e.getName());
        }
        
        return list;
    }
    
}