Home Jpa Eclipsejpaexamples JPA Abs Function
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

JPA Abs Function

Advertisement
In this section, you will learn about the JPA Abs() function. This method returns absolute value of the given field.

JPA Abs Function

     

In this section, you will learn about the JPA Abs() function. This method returns absolute value of the given field.

JPA Abs Function:

Query query=em.createQuery("SELECT p FROM Product p WHERE ABS(p.price)<50.0");

This query retrieve all products which have the price less than "50".

For JPA abs application, you need the following artifacts:

  1. Database Table: product
  2. Model Class: Product.java
  3. Main Class: JPAAbsFunction.java

Database Table: product

CREATE TABLE `product` ( 
`id` int(11) NOT NULL auto_increment, 
`itemName` varchar(50) NOT NULL, 
`price` double NOT NULL, 
PRIMARY KEY (`id`) 
)

Model Class: Product.java

/**

*/
package roseindia;

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="product")
public class Product {

@Id
@GeneratedValue
private int id;
/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}

@Column(name="itemName",length=50,nullable=false)
private String itemName;
/**
* @return the itemName
*/
public String getItemName() {
return itemName;
}

/**
* @param itemName the itemName to set
*/
public void setItemName(String itemName) {
this.itemName = itemName;
}

@Column(name="price")
private double price;
/**
* @return the price
*/
public double getPrice() {
return price;
}

/**
* @param price the price to set
*/
public void setPrice(double price) {
this.price = price;
}

}

Main Class: JPAAbsFunction.java

/**

*/
package roseindia;

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 JPAAbsFunction {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

EntityManagerFactory emf=Persistence.createEntityManagerFactory("jpa");
EntityManager em=emf.createEntityManager();
try{
EntityTransaction entr=em.getTransaction();
entr.begin();
Query query=em.createQuery("SELECT p FROM Product p WHERE ABS(p.price)<50.0");
List pList=query.getResultList();
Iterator pIterator=pList.iterator();
while(pIterator.hasNext()){
Product pro=(Product)pIterator.next();
System.out.println("itemName:"+pro.getItemName());
}
entr.commit();
}
finally{
em.close();
}

}

}

Output:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select product0_.id as id1_, product0_.itemName as itemName1_, product0_.price as price1_ from product product0_ where abs(product0_.price)<50.0

itemName:Dall

itemName:Chock

 

Product table records:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 16, 2005

Related Tutorials

Ask Questions?    Discuss: JPA Abs Function  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com