In this section, you will learn about the JPA Upper() function. This function returns upper-case of the specified string arguments.
JPA Upper Function:
| Query trimQuery=em.createQuery("UPDATE Student st SET st.sname=UPPER(st.sname)"); |
This retrieve all records from student table in upper case because we will use the upper() jpa function.
For JPA Lower application, you need the following artifacts:
Database Table: student
| CREATE TABLE `student` ( `id` int(11) NOT NULL auto_increment, `sname` varchar(100) NOT NULL, `sroll` int(11) NOT NULL, `scourse` varchar(10) NOT NULL, PRIMARY KEY (`id`) ) |
Model Class: Student.java
| /** * */ package roseindia; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; /** * @author Administrator * */ @Entity @Table(name="student") public class Student { @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="sname", length=100,nullable=false) private String sname; /** * @return the sname */ public String getSname() { return sname; } /** * @param sname the sname to set */ public void setSname(String sname) { this.sname = sname; } @Column(name="sroll",nullable=false) private int sroll; /** * @return the sroll */ public int getSroll() { return sroll; } /** * @param sroll the sroll to set */ public void setSroll(int sroll) { this.sroll = sroll; } @Column(name="scourse",length=10,nullable=false) private String scourse; /** * @return the scourse */ public String getScourse() { return scourse; } /** * @param scourse the scourse to set */ public void setScourse(String scourse) { this.scourse = scourse; } } |
Main Class: JPAUpperFunction.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 JPAUpperFunction { /** * @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 st FROM Student st"); List stuList=query.getResultList(); Iterator stuIterator=stuList.iterator(); while(stuIterator.hasNext()){ Student st=(Student)stuIterator.next(); System.out.print("sname:"+st.getSname()); System.out.println(); } //Upper Query trimQuery=em.createQuery("UPDATE Student st SET st.sname=UPPER(st.sname)"); int updateRecord=trimQuery.executeUpdate(); System.out.println(updateRecord+" records are updated"); entr.commit(); } finally{ em.close(); } } } |
Output:
|
log4j:WARN No appenders could be
found for logger log4j:WARN Please initialize the log4j system properly. Hibernate: select student0_.id as id0_, student0_.scourse
as sname:vinod sname:ravi kumar sname:suman saurabh kumar Hibernate: update student set sname=upper(sname) 3 records are updated
|
Before Upper:

After upper:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: JPA Upper Function
Post your Comment