Solved

Jpa query

Posted on 2014-04-28
2
203 Views
Last Modified: 2014-05-11
Let me know how to write a sql  through jpa annotation

I have  to find a single value column through jpa here is the sql statement
select userkey from right table where code = + code

Open in new window

0
Comment
Question by:roy_sanu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 40028819
make the class as Entity and on top of that write named queries.

@Entity
@Table(name="A")
@NamedQuery(name="selectUser",query="select userkey from right table where code = :code")
public class A implements java.io.Serializable{
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      private Long id;


write other variables...
}


call this query like this.

Query query = em.createNamedQuery("selectUser");
            query.setParameter("code", 1234);
            List<String> resultList = (List<String>) query.getResultList();
0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 500 total points
ID: 40028823
or you can do like this..

String num = (String) em.createQuery("select userkey from right table where code = :code").setParameter("code",1234) .getSingleResult();
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question