Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Saturday, January 22, 2022

how to make Criteria in hibernate and Java






 step1: open dilsecodie

step 2 : check https://dilsecodie.com/sourcecode/topic/Java/tutorial/Criteria

step 3 : can change class name for any entity to get result .

step 4 : This only work Hibernate Criteria

step 5: Can be used in any serviceImp class 

For hibernate, cover all the advanced concepts as well, including transaction management, session handling, isolation, caching. The many to one, one to many mappings, 

how to make joins in entities, how to write criteria for join queries etc

Java Persistence API, that is, JPA is a specification whereas Hibernate is its implementation.


JPA provides the abstraction over the specific ORM. It provides the APIs that are implemented by the ORM. It's like an interface. You write an implementation of it. Then use the interface references to keep the objects of its implementation(s).


The advantage of using it is that you can replace the ORM in your application because-


You would be using the JPA interfaces to wire the entity manager in your repository classes.

To write the queries you'd be using JPQL or the JPA Criteria.

Thus making your code independent of the underlying ORM


Criteria c = this.sessionFactory.getCurrentSession().createCriteria(Test.class)

     .add(Restrictions.eq("do_name", doName)).

add(Restrictions.eq("test_name",TestName));

return c.list();


There is no such thing as Spring JPA. JPA is the Java Persistence API and it is only a specification. 

Spring Data JPA makes it easy to implement JPA-based repositories and you can use them along with an ORM, such as Hibernate, or not

Session–Cache or collection of loaded objects relating to a single unit of work


XDoclet Annotations used to support Attribute Oriented Programming


Defines HQL for expressing queries to the database


Supports Entity Relationships through mapping files and annotations in JavaDoc


Provides a Persistence Manager API exposed via the Session, Query, Criteria, and Transaction API


Provides callback support through lifecycle, interceptor, and validatable interfaces


Entity Relationships are unidirectional. Bidirectional relationships are implemented by two unidirectional relationships.



The exceptions of JDBC are checked. We must place them in try catch blocks. It is a burden to programmar.


After completion of the source code of the project, If a table structure is altered then we need to modify all the queries wherever they are placed in project. It is a time consuming process.


In JDBC persistance logic we use SQL commands suppose if database is changed then SQL commands also need to be changed means JDBC persistance logic is database dependent.


The primary responsibility of hibernate is transferring the data between Java and database in the form of objects. It is a light weight ORM and DAO layer framework. Hibernate is non invasive which does not force programmar to extend any class or implement any interface. Here in hibernate we use HQL(hibernate query language). It is database independent which reduces burden on developers. HQL queries are converted to database-specific commands by hibernating at runtime.


There are many features like caching, lazy loading, Criteria, Locking, Filters, connection pooling are available in Hibernate.

Hibernate: wherever lots of Edit update, Delete is required we always prefer to hibernate basically every data is coming from Java Object and even though it is very simple to save data. just like set value into Bean class and call save(POJORef); that will save the data.

Some of the important points remember while doing a practical

Always follow coding guidelines

always catch Exceptions.

write log statement for checking purposes.



How to convert Jsonarray from ArrayList






 step 1 : open dilsecodie

 step  2: using java Convert Jsonarray to ArrayList 

 step 3: Pass ArrayList  To this method gets Jsonarray in result.

 step 4 : Jsonarray can be used to return responses from API


JSONArray can contain: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object; i.e. it is mixed types, making it hard to just dump the elements into a List of some type and sort that, 

then pass through the list dumping sorted items back into the JSON array. 

the only certain way to get a common type of each element from the 

JSONArray is using the Object get() method

of course then all you have is Object objects and won't be able to do any meaningful sorting on them without revisiting the serialization issue.


Assuming your JSONArray contains homogeneously structured values, 

you could iterate through the JSONArray, calling one of the typed get() methods on each one, 

dumping them into a List type, then sorting on that.


You can simply implement the Interface List and do generics for the type and implement your own class that does the same as ArrayList.


Since you use an array as storage, this should be straight-forward and easy to do.


public class MyArrayList<Type> implements List { 


}


Array lists are objects in Java, 

which means that they have special features that primitive arrays do not.

 It’s a wrapper class, which means that under the hood, 

there is primitive array wrapped with the functionality of lists, 

which gives the class very powerful methods



 public static ArrayList convertJSONArrayToArrayList(JSONObject jsonObject){

JSONArray arrayOfReq = jsonObject.getJSONArray(NetworkConstant.OBJECT_DATA) ;

ArrayList lstJSONArray =  new ArrayList();

for(int i = 0; i < arrayOfReq.size() ; i++){

            Object objectOnVec = (Object) arrayOfReq.get(i);

           


            if(objectOnVec instanceof JSONObject)

            lstJSONArray.add(convertFromJsonObject((JSONObject)objectOnVec));

             else

            lstJSONArray.add(objectOnVec);

      }

   return lstJSONArray;

}



 public static JSONObject convertListToJSONObject(>

ArrayList requiredListObject) {

JSONObject jsonObjectLst = new JSONObject();

jsonObjectLst.put("DATA",4);

JSONArray jsonArray = new JSONArray();

for (int i = 0; i < requiredListObject.size(); i++) {

Object objectVal = requiredListObject.get(i);

if (objectVal instanceof JSONStructure || objectVal instanceof HashMap

|| objectVal instanceof ArrayList) {

jsonArray.add(convertDSToJSONObject(objectVal));

} else {

if(objectVal instanceof JSONObject) {

jsonArray.add(objectVal);

} else if(objectVal instanceof String || objectVal instanceof Integer) { 

jsonArray.add(objectVal);

} else {

jsonArray.add(objectVal);

}

}

}

jsonObjectLst.put("JSONDATA", jsonArray);

return jsonObjectLst;

}



The important points about Java ArrayList class are:


Java ArrayList class can contain duplicate elements.

Java ArrayList class maintains insertion order.

Java ArrayList class is non synchronized.

Java ArrayList allows random access because array works at the index basis.

In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.

 

Hierarchy of ArrayList class


As shown in above diagram, Java ArrayList class extends AbstractList class which implements List interface. The List interface extends Collection and Iterable interfaces in hierarchical order.


ArrayList class declaration


Let's see the declaration for java.util.ArrayList class.


public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

Constructors of Java ArrayList

ArrayList()

It is used to build an empty array list.

ArrayList(Collection c)

ArrayList supports dynamic arrays that can grow as needed. In Java, standard arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. But, sometimes, you may not know until run time precisely how large an array you need. To handle this situation, the Collections Framework defines ArrayList. In essence, an ArrayList is a variable-length array of object references. That is, an ArrayList can dynamically increase or decrease in size. Array lists are created with an initial size. When this size is exceeded.