Null Expression Example

In this section, you will see the example of null expression in with JPQL (Java Persistence Query Language).

Null Expression Example

Null Expression Example

     

In this section, you will see the example of null expression in with JPQL (Java Persistence Query Language). In this example, we will use a JPQL query ("SELECT st FROM Student st WHERE st.scourse IS NULL"). This query retrieve all record that contains "scourse" file is null.

You need the following artifacts:

  1. Database Table: student
  2. Model Class: Student.java
  3. Main Class: JPANullExpression.java

Database Table: student

CREATE TABLE `student` ( 
`id` int(11) NOT NULL auto_increment, 
`sname` varchar(40) NOT NULL, 
`sroll` int(11) NOT NULL, 
`scourse` varchar(10) default NULL, 
PRIMARY KEY (`id`) 
)

Model Class: Student.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package jpacrud;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
*
* @author Administrator
*/
@Entity
@Table(name="student")

public class Student implements java.io.Serializable {

@Id
@GeneratedValue
private int id;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Column(name="sname",length=40,nullable=false)
private String sname;

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}

@Column(name="sroll",nullable=false)
private int sroll;

public int getSroll() {
return sroll;
}

public void setSroll(int sroll) {
this.sroll = sroll;
}

@Column(name="scourse",length=10,nullable=true)
private String scourse;

public String getScourse() {
return scourse;
}

public void setScourse(String scourse) {
this.scourse = scourse;
}

}

Main Class: JPANullExpression.java

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package jpacrud;

import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
*
* @author Administrator
*/
public class JPANullExpression {
public static void main(String arg[]){
EntityManagerFactory emf=Persistence.createEntityManagerFactory("netjpa");
EntityManager em=emf.createEntityManager();
try{
EntityTransaction entr=em.getTransaction();
entr.begin();
Query query=em.createQuery("SELECT st FROM Student st WHERE st.scourse IS NULL");
List stEmtyList=query.getResultList();
Iterator stEmptyIterator=stEmtyList.iterator();
while(stEmptyIterator.hasNext()){
Student stu=(Student)stEmptyIterator.next();
System.out.print("Name:"+stu.getSname());
System.out.print(" Roll:"+stu.getSroll());
System.out.print(" Course:"+stu.getScourse());
System.out.println();
}
entr.commit();
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
finally{
em.close();
}
}

}

Output:

init:
deps-jar:
compile-single:
run-single:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select student0_.id as id0_, student0_.scourse as scourse0_, student0_.sname as sname0_, student0_.sroll as sroll0_ from student student0_ where student0_.scourse is null

Name:Suman Roll:104 Course:null
Name:Noor Roll:105 Course:null
BUILD SUCCESSFUL (total time: 3 seconds)

Download Application