Home Jpa Eclipsejpaexamples JPA Substring() Function
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JPA Substring() Function

Advertisement
In this section, you will learn about the JPQL substring() function. The substring() function returns the some part of string arguments.

JPA Substring() Function

     

In this section, you will learn about the JPQL substring() function. The substring() function returns the some part of string arguments. It takes three arguments. First is string argument that have to be concatenate, second is starting index and third is length of string .

JPA Substring() Function:

Query query=em.createQuery(
"UPDATE Student st SET st.sname=SUBSTRING('vinod kumar',1,5)
WHERE st.sname='vinod kumar'"
);

For JPA substring application, you need the following artifacts:

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

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: JPASubStringFunction.java

 /**
* 
*/
package roseindia;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
* @author Administrator
*
*/
public class JPASubStringFunction {

/**
* @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("UPDATE Student st SET st.sname=SUBSTRING('vinod kumar',1,5) WHERE st.sname='vinod kumar'");
int updateRecord=query.executeUpdate();
System.out.println(updateRecord+" reocrds are updated");
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: update student set sname=substring('vinod kumar', 1, 5) where sname='vinod kumar'

1 reocrds are updated

 

Before substring:

After executing the substring:

Advertisement

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: October 4, 2006

Ask Questions?    Discuss: JPA Substring() Function  

Post your Comment


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