How to get java source files from jar file

  • Jar : Java Archive is a group of .class files.
  • We can create a jar file using following commands
  • jar -cvf  example.jar test.class
  • jar -cvf example.jar *.*
  • To unzip from jar file we need to use following command.
  • jar -xvf  instanceofjava.jar



How to get source code from jar file using java De-compilers

how to get source code from jar file in eclipse


  • Open JD-GUI and File -> open -> open target jar file.
  • It will show java source code.

How to extract java files from jar in eclipse

  •  We can get java source files from jar file by using eclipse also for this we need to add a plugin.
  • Download jar file from http://jd.benow.ca/
  • Unzip it . you will get jd.ide.eclipse.plugin_1.0.0.jar file  and add it to eclipse plugin folder.
  • And restart your eclipse.
  • Add target jar file to a project and now click on the file you will get source code.
  • You can add target jar file using java project->java buildpath -> add external jars option


how to convert jar to java source in eclipse


  • Here i added jstl core jar to eclipse to see java source files.
  • Click on the file now it will show source code 
  • How to extract java files from jar

how to extract java files from jar in eclipse

Hibernate Native sql query with example

  • By Using Hibernate Native SQL we can write database dependent queries as part of hibernate.
  • Hibernate Native Sql allows us to write create , update,  delete and insert queries.
  • We can also call Stored procedures using Hibernate Native Sql.
  • When the query is too complex using HQL then we need to use hibernate sql query.
  • Hibernate uses the org.hibernate.SQLQuery interface for native SQL
    1. SQLQuery is a sub interface of Query
    2. Use createSQLQuery() factory method on Session to create SQLQuery object.
  •  Hibernate SQLQuery must be associated with an existing Hibernate entity or scalar result.



Hibernate native sql insert query example

  1. Session session = sessionFactory.openSession();
  2. session.beginTransaction();
  3.  
  4. SQLQuery insertsqlQuery = session.createSQLQuery("INSERT INTO
  5. Physician(firstname,lastname,fee,hospital)VALUES(?,?,?,?)");
  6.  
  7.  insertsqlQuery.setParameter(0, "Saidesh");
  8.  insertsqlQuery.setParameter(1, "Kilaru");
  9.  insertsqlQuery.setParameter(2, 50); 
  10.  insertsqlQuery.setParameter(3, "Yashoda");        
  11.  insertsqlQuery.executeUpdate();

 Hibernate scalar query example

  • Writing a Hibernate Sql query to get list of  scalars or values from single or multiple tables.
  • Lets see an example on Hibernate scalar query.


  1. String hibernate_sql = "SELECT first_name, fee FROM  Physician";
  2. SQLQuery query = session.createSQLQuery(hibernate_sql);
  3. query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
  4. List results = query.list();

 Hibernate named sql Queries

  • Writing a Hibernate Sql query to get entity object by using addEntity() method.
  • Lets see an example on Hibernate named sql query

hibernate native sql query parameters scalar


Hibernate Named Query Introduction Tutorial

  • If we are using HQL or Native SQL queries multiple time then it will be code mess because all queries will be scattered throughout the project.
  • Hibernate Named Query is a way to use queries by giving a name.
  • Hibernate Named Queries will be defined at one place and used anywhere in the project.
  • use of named query in hibernate
  • Writing HQL query in HBM file is called HQL Named Query.
  • Or we can use @NameQuery annotation in entity.
  • For writing Hibernate Named Queries we will use <query> tag in Hibernate mapping file Or @NameQuery annotation in the entity.
  • If we want to create Named Query using hibernate mapping file then we need to use query element.



Advantages of Named Query in Hibernate 

  • Global access
  • Easy to maintain.

Hibernate Named Queries by using Annotations:

  • If we want to create Named Queries using annotations in entity class then we need to use @NameQueries and @NameQuery annotations
  • @NameQuery will be used to create single Query
  • @NameQueries annotations will be used to create multiple Queries. When we are using @NameQueries for every query we need to use @NameQuery annotation.

Hibernate Named Query example by using Annotations:


Hibernate Named Query example join

  1. Query query = session.getNamedQuery("findDocterById");
  2.         query.setInteger("id", 37);
  3.        List empList = query.list();


Hibernate Named Queries by using Hibernate mapping file:

  • We need to configure Hibernate Named Queries as part of Hibernate mapping file.
  • By using <query> element we need to write Hibernate named Queries.

  1. <hibernate_mapping>
  2.             <class  >
  3.             ---------
  4.             </class>
  5.  
  6.  <query name = “findDocterById”>
  7.  <![CDATA[from Docter s where s.id = :id]]>
  8. </query>  

  9.  </hibernate_mapping>     


  1. Query query = session.getNamedQuery("findDocterById");
  2.         query.setInteger("id", 64);
  3.        List empList = query.list();

Hibernate Criteria Query Language (HCQL)

  • In Hibernate we pull the data from data base in two ways.
  • session.get()/ session.load()
  • Hibernate query langugae
  • Now we will discuss about the third way Hibernate criteria query language which solves the problems of above two approaches.




Hibernate Criteria Query Language / HCQL

  • In order to fetch the records based on some criteria we use Hibernate Criteria Query Language.
  • We can make select operation on tables by applying some conditions by using HCQL.
  • Criteria API is the alternative to HQL in object oriented approach.  
  • We can execute only SELECT statements using Criteria; we can’t execute UPDATE, DELETE statements using Criteria.

Advantages of  Hibernate Criteria Query Language (HCQL) 

  • Criteria API allows us to define criteria query object by applying rules ,filtration and logical conditions
  • So that we can add criteria to query.
  • Criteria is also database independent, Because it internally generates HQL queries.
  • Criteria is suitable for executing dynamic queries
  • Criteria API also include Query by Example (QBE) functionality for supplying example objects.
  • Criteria also includes projection and aggregation methods
     

Criteria Interface:

  • Criteria Interface has some methods to specify criteria.
  • Hibernate session interface has a method named createCriteria() to create criteria.

  1. public interface Criteria  extends CriteriaSpecification

  1. Criteria criteria = session.createCriteria(Student.class);
  2. List<Student> studentList= criteria .list();

Methods of Criteria interface:

  
Hibernate add crieria example hcql


Order Class

  1. public class Order extends Object implements Serializable
 
  • By using Order class we can sort the records in ascending or descending order.
  • Order class provides two different methods to make ascending and descending.
  1. public static Order asc(String propertyName)
  2. public static Order desc(String propertyName)

Hibernate Criteria query example using order class

  1.  Criteria criteria = session.createCriteria(Product.class)
  2.  
  3. // To sort records in descening order
  4. criteria.addOrder(Order.desc("price"));
  5.  
  6. // To sort records in ascending order
  7. criteria.addOrder(Order.asc("price"));

Restrictions Class

  1. public class Restrictions extends Object

  • Restrictions class provides methods that can be used  add restrictions (conditions) to criteria object.
  • We have many methods in Restrictions class some of the commonly using methods are.

hibernate criteria add example interview questions


Hibernate Criteria query example using Restrictions class


  1.  Criteria criteria = session.criteriaeateCriteria(Product.class);
  2. // To get records having price more than 3000
  3. criteria.add(Restrictions.gt("price", 3000));
  4.  
  5. // To get records having price less than 2000
  6. criteria.add(Restrictions.lt("price", 2000));
  7.  
  8. // To get records having productName starting with criteriaite
  9. criteria.add(Restrictions.like("productName", "criteriaite%"));
  10.  
  11. // Case sensitive form of the above restriction.
  12. criteria.add(Restrictions.ilike("productName", "zara%"));
  13.  
  14. // To get records having price in between 1000 and 2000
  15. criteria.add(Restrictions.between("price", 1000, 2000));
  16.  
  17. // To check if the given property price is null
  18. criteria.add(Restrictions.isNull("price"));
  19.  
  20. // To check if the given property is not null
  21. criteria.add(Restrictions.isNotNull("price"));
  22.  
  23. // To check if the given property price is empty
  24. criteria.add(Restrictions.isEmpty("price"));
  25.  
  26. // To check if the given property price is not empty
  27. criteria.add(Restrictions.isNotEmpty("price"));
  28.  
  29. List results = criteria.list();

Pagination using Hibernate Criteria

  • By using criteria methods setFirstResult() and setMaxResults() we can achieve pagination concept.


  1. Criteria criteria = session.createCriteria(Product.class);
  2. criteria.setMaxResults(10);
  3. criteria.setFirstResult(20);

Projections class in Hibernate

  1. public final class Projections extends Object

  • By Using org.hibernate.criterion.Projections  class methods we can perform operations like minimum , maximum, average , sum and count.

Hibernate Criteria query example using Projection class

  1. Criteria criteria = session.criteriaeateCriteria(Product.class);
  2.  
  3. // To get total row count.
  4. criteria.setProjection(Projections.rowCount());
  5.  
  6. // To get average price.
  7. criteria.setProjection(Projections.avg("price"));
  8.  
  9. // To get distinct countof name
  10. criteria.setProjection(Projections.countDistinct("name"));
  11.  
  12. // To get maximum price
  13. criteria.setProjection(Projections.max("price"));
  14.  
  15. // To get minimum price
  16. criteria.setProjection(Projections.min("price"));
  17.  
  18. // To get sum of price
  19. criteria.setProjection(Projections.sum("price"))
Select Menu