Showing posts with label Java Oops Interview Questions. Show all posts
Showing posts with label Java Oops Interview Questions. Show all posts

Friday, 21 December 2018


    1. filter employees with same salary count greater than five (group by salary) using hibernate criteria?
      1. Session session = getCurrentSession(); ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.groupProperty("totalCode")) .add(Projections.groupProperty("activityCode")) .add(Projections.sum("amount")) .add(Projections.rowCount()); Criteria criteria = session.createCriteria(Payment.class); criteria.setProjection(projectionList); List payments = criteria.list(); for (Object[] payment : payments) { System.out.println("totalCode: " + payment[0]); System.out.println("activityCode: " + payment[1]); System.out.println("amountSum: " + payment[2]); System.out.println("rowCount: " + payment[3]); }
    2. What is Hibernate N+1 Problems and its Solution?
      1. Hibernate n+1 problems only comes for one to many relationship.
      2. Let us see this problem by example – We have Department table with a one-to-many relationship with Employee. One Department may have many Employees.
      3. We have written the Hibernate Department Entity as below.
      4. @Entity public class Department { private Long id; @OneToMany private Employee[] Employees; }
      5. So now you want to print out all the details of Employee models. A native O/R implementation would SELECT all Department and then do N additional SELECTs for getting the information of Employee for each department.
      6. -- To Get all Departments SELECT * FROM Department; -- To get each Employee, get Employee details SELECT * FROM Employee WHERE Employee.departmentId = ?
      7. As you see, the N+1 problem can happen if the first query populates the primary object and the second query populates all the child objects for each of the unique primary objects returned.
      8. Solution for Hibernate N+1 Problem
        1. Using HQL fetch join
          1. You can use the fetch while using the HQL as below example.
          1. from Department d join fetch d.employees Employee
          2. Hibernate Generated SQL would be similer as –
          3. SELECT * FROM Department d LEFT OUTER JOIN Employee e ON d.id = d.department_id
        1. Using Criteria query
          1. Criteria criteria = session.createCriteria(Department.class); criteria.setFetchMode("employees", FetchMode.EAGER);
        2. In both above cases, the query returns a list of Department objects with the Employee initialized, and only one query needs to be run to return all the Department and Employee information required.

Friday, 15 September 2017


Order of Execution
  1. Parent Static Block 
  2. Child Static Block 
  3. Parent Normal Block 
  4. Parent Constructor 
  5. Child Normal Block 
  6. Child Constructor
  7. Child method invoked

Animal.java 
public class Animal { static { System.out.println("Animal Static Block "); } public Animal() { System.out.println("Animal Consuctor"); } { System.out.println("Animal Normal Block"); } public void aboutMe() { System.out.println("I am a Animal"); } }
Cow.java
public class Cow extends Animal { static { System.out.println("Cow Static Block "); } public Cow() { System.out.println("Cow Consuctor"); } { System.out.println("Cow Normal Block"); } public void aboutMe() { System.out.println("I am a Cow"); } }
App.java
public class App { public static void main(String[] args) { Cow cow = new Cow(); cow.aboutMe(); } }



  1. Implementing two interfaces in a class with same method. Which interface method is overridden? 

interface A{ int f(); } interface B{ int f(); } class Test implements A, B{ public static void main(String... args) throws Exception{ } @Override public int f() { // from which interface A or B return 0; } }
  • If a type implements two interfaces, and each interface define a method that has identical signature, then in effect there is only one method, and they are not distinguishable. 
  • If, say, the two methods have conflicting return types, then it will be a compilation error. This is the general rule of inheritance 
  • This is the general rule of inheritance, method overriding, hiding, and declarations, and applies also to possible conflicts not only between 2 inherited interface methods, but also an interface and a super class method, or even just conflicts due to type erasure of generics.
  • Reference
  • https://stackoverflow.com/questions/2801878/implementing-two-interfaces-in-a-class-with-same-method-which-interface-method

Search This Blog

Contact us

Name

Email *

Message *