1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)

Hibernate interview questions

!!! Hibernate interview questions !!!

Filter in Hibernate with Example?




Filter in Hibernate ------

USER ( ID INT, USERNAME VARCHAR, ACTIVATED BOOLEAN) - TABLE



public class User

{

private int id;

private String username;

private boolean activated;





public boolean isActivated()

{

return activated;

}

public void setActivated(boolean activated)

{

this.activated = activated;

}

public int getId()

{

return id;

}

public void setId(int id)

{

this.id = id;

}

public String getUsername()

{

return username;

}

public void setUsername(String username)

{

this.username = username;

}

}

-----------------------------------------------------------------

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-mapping

PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">



<hibernate-mapping>

<class name="User">

<id name="id" type="int">

<generator class="native"/>

</id>



<property name="username" type="string" length="32"/>

<property name="activated" type="boolean"/>

<filter name="activatedFilter" condition=":activatedParam = activated"/>

</class>

<filter-def name="activatedFilter">

<filter-param name="activatedParam" type="boolean"/>

</filter-def>

</hibernate-mapping>



--------------------------------------------------------------------

Save and Fetch using filter example



User user1 = new User();

user1.setUsername("name1");

user1.setActivated(false);



session.save(user1);



User user2 = new User();

user2.setUsername("name2");

user2.setActivated(true);



session.save(user2);



User user3 = new User();

user3.setUsername("name3");

user3.setActivated(true);



session.save(user3);



User user4 = new User();

user4.setUsername("name4");

user4.setActivated(false);



session.save(user4);



All the four user saved to Data Base User Table.



Now Fetch the User using Filter..



Filter filter = session.enableFilter("activatedFilter");

filter.setParameter("activatedParam",new Boolean(true));

Query query = session.createQuery("from User");

Iterator results = query.iterate();

while (results.hasNext())

{

User user = (User) results.next();

System.out.print(user.getUsername() + " is ");



}





Guess the Result :

name2 name3



Because Filer is filtering ( only true value) data before query execute.

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy