Thursday, 2 July 2020

How to get Database Change Notification ?

I am not writing the whole blog here .
Found a good document on oracle .Please refer below .

https://docs.oracle.com/cd/E11882_01/java.112/e16548/dbchgnf.htm#JJDBC28820


Note: As per my analysis this feature is not working/deprecated in 12.1 c oracle version and again it is available in 12.2 c oracle .

Its a good feature to get notified automatically if there is any changes in Table .

How to get notification if Table/Entity perform any operation ?

The below example will show ,if there is any operation in a entity then how we will get notification in JPA+Hibernate .

Note: If the operation is in only application not in backend operation (like update,delete,add query in oracle developer directly)

While defining Entity class need to add below annotation .

 @EntityListeners(MyEntityListener.class)

We will discuss about  MyEntityListener.class also.
Example  of Entity class :

@Entity
@EntityListeners(MyEntityListener.class)
public class MyEntity {
  @Id
  @GeneratedValue
  private int id;
  private String msg;

  public MyEntity() { }

  public MyEntity(String msg) {
      this.msg = msg;
  }
    .............
}


Listner class:
In this class we will get notification if there is any operation in entity .

Example:
public class MyEntityListener {

  @PrePersist
  void onPrePersist(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPrePersist(): " + myEntity);
  }

  @PostPersist
  void onPostPersist(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPostPersist(): " + myEntity);
  }

  @PostLoad
  void onPostLoad(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPostLoad(): " + myEntity);
  }

  @PreUpdate
  void onPreUpdate(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPreUpdate(): " + myEntity);
  }

  @PostUpdate
  void onPostUpdate(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPostUpdate(): " + myEntity);
  }

  @PreRemove
  void onPreRemove(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPreRemove(): " + myEntity);
  }

  @PostRemove
  void onPostRemove(MyEntity myEntity) {
      System.out.println("MyEntityListener.onPostRemove(): " + myEntity);
  }
}


The above annotation will take responsible for the notification .

If any operation will be there in the mentioned table in  your application then we will get notification
.
Note:We can use other annotation in Entity class as well.

Friday, 18 May 2018

Object States in Hibernate – Transient,Persistent and Detached

1. Transient Object State:

An object which is not associated with hibernate session and does not represent a row in the database is considered as transient. It will be garbage collected if no other object refers to it.

An object that is created for the first time using the new() operator is in transient state. When the object is in transient sate then it will not contain any identifier (primary key value). You have to use save, persist or saveOrUpdate methods to persist the transient object.

Employee emp=new Employee();
emp.setName("Chinmay");
// here emp object is in a transient state .

2. Persistent Object State:

An object that is associated with the hibernate session is called as Persistent object. When the object is in persistent state, then it represent one row of the database and consists of an identifier value.You can make a transient instance persistent by associating it with a Session.
Long id = (Long) session.save(emp);
// emp object is now in a persistent state

3. Detached Object State:

Object which is just removed from hibernate session is called as detached object.The sate of the detached object is called as detached state.
When the object is in detached sate then it contain identity but you can’t do persistence operation with that identity.
Any changes made to the detached objects are not saved to the database. The detached object can be reattached to the new session and save to the database using update, saveOrUpdate and merge methods.

session.close();
//object in detached state

Example :
========= 
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import com.jwt.hibernate.Student;

public class ObjectStatesDemo {
    public static void main(String[] args) {
         // Transient object state
        Student student = new Student();
        student.setId(101);
        student.setName("Chinmay");
        student.setRoll("10");
        student.setDegree("MCA");
        student.setPhone("9999");
        // Transient object state
        Session session = new AnnotationConfiguration().configure()
                .buildSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        // Persistent object state
        session.save(student);
        t.commit();
        // Persistent object state
        session.close();
        // Detached object state

    }
}


Wednesday, 4 April 2018

How to sort Object class custom property in java ?

public static void main(String[] args) {
        List<Object> list = new ArrayList();
        list.add(new Object[]{new Object[]{"code", "123.456"}, new Object[]{"size", 200},new Object[]{"location", "Indonesia"}});
        list.add(new Object[]{new Object[]{"code", "321.987"}, new Object[]{"size", 250},new Object[]{"location", "Indonesia"}});
        list.add(new Object[]{new Object[]{"code", "456.987"}, new Object[]{"size", 220},new Object[]{"location", "Thailand"}});
        list.add(new Object[]{new Object[]{"code", "147.258"}, new Object[]{"size", 320},new Object[]{"location", "Thailand"}});

        list.add(new Object[]{new Object[]{"code", "852.741"},new Object[]{"size", 80},new Object[]{"location", "Malaysia"}});
        list.add(new Object[]{new Object[]{"code", "456.789"}, new Object[]{"size", 320},new Object[]{"location", "Indonesia"}});
        list.add(new Object[]{new Object[]{"code", "654.321"}, new Object[]{"size", 300},new Object[]{"location", "Indonesia"}});
        System.out.println("BEFORE SORTING:");
        for (Object obj : list) {
            System.out.println("Location:" + (((Object[]) ((Object[]) obj)[2]))[1] + ",Size=" + (((Object[]) ((Object[]) obj)[1]))[1]
                    + ",Code" + (((Object[]) ((Object[]) obj)[0]))[1]);
        }
        Collections.sort(list, new MyComp());
        System.out.println("AFTER SORTING:");
        for (Object obj : list) {
            System.out.println("Location:" + (((Object[]) ((Object[]) obj)[2]))[1] + ",Size=" + (((Object[]) ((Object[]) obj)[1]))[1]
                    + ",Code" + (((Object[]) ((Object[]) obj)[0]))[1]);
        }
    }

    static class MyComp implements Comparator {

        @Override
        public int compare(Object o1, Object o2) {
            if (o1 instanceof Object[] && o2 instanceof Object[]) {
                if (((Object[]) o1).length == 3 && ((Object[]) o2).length == 3) {
                    return ((String) (((Object[]) ((Object[]) o1)[2]))[1]).compareTo((String) (((Object[]) ((Object[]) o2)[2]))[1]);
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        }
    }

Wednesday, 27 September 2017

How to implement custom ArrayList in java?

package Inter;

import java.util.Arrays;
import java.util.Iterator;

/**
 *
 * @author chinmaykumar.t
 */
public class CustomArrayList {

    private int size = 0;
    Object[] objArr = null;

    public CustomArrayList() {
        objArr = new Object[10];
    }

    public Object get(int index) {
        if (index < size) {
            return objArr[index];
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }
    }

    public void add(Object obj) {
        if (objArr.length - size <= 5) {
            increaseListSize();
        }
        objArr[size++] = obj;
    }

    public Object remove(int index) {
        if (index < size) {
            Object object = objArr[index];
            objArr[index] = null;
            int temp = index;
            while (temp < size) {
                objArr[temp] = objArr[temp + 1];
                temp++;
            }
            size--;
            return object;
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }

    }

    public void increaseListSize() {
        objArr = Arrays.copyOf(objArr, objArr.length * 2);
        System.out.println("\nNew length: " + objArr.length);

    }

    public int size() {
        return size;
    }
   
    public static void main(String[] args) {
        CustomArrayList arrayList=new  CustomArrayList();
        arrayList.add("chinmay");
        arrayList.add("kumar");
        arrayList.add("tripathy");
        System.out.println(""+arrayList.toString());
        System.out.println(""+arrayList.get(0));
        System.out.println(""+arrayList.remove(1));
        System.out.println(""+arrayList.size());
         for(int i=0;i<arrayList.size();i++){
            System.out.print(arrayList.get(i)+" ");
        }
    }
}

Monday, 4 September 2017

Use of data types Money and Smallmoney in RDBMS(Sql Server)

Money and Smallmoney: -

These are the data types to store the monetary or currency values in the data type. Money data type stores the 8 bytes and Smallmoney data type stores the 4 bytes. These data types are capable to store the ten-thousandth of the monetary unit. For example:- cents and dollars etc. 6) Int, bigint, smallint and tinyint:- These data types can also stores the exact values of the number. int data type is the primary integer data type in the SQL Server. We can use the bigint data type in place of the int and small int if the records are too much in the given table. But if the data is keep increasing in the table then we can change the data type of the monetary column into bigint from tinyint, smallint and int. The use of the different data type from the int family is used as per the requirements which are different all the time. For Metadata/ Base tables may be able to use the tinyint and smallint but for the transaction tables we can go for the int and bigint data types.

Wednesday, 26 July 2017

How to know the running monitor in Cron job ?


How to  know the running monitor in Cron job and edit the monitor path and how to save ?


=>Login and type below command:
crontab -e

=>For comment one monitor
Press Esc then type "i" means Insert mode.
Add # before the monitor
e.g
#*/5 * * * *  '/apps/472/monitor/notificationmonitor.sh' > /apps/472/monitor/notificationcron.txt

=>To  save the changes
press ESC  then type  :wq!   

*Here "wq!" means write and quit.

=> Simply to quit from current window without changes
press Esc and type :q!