[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Jpa query

Posted on 2014-04-28
2
Medium Priority
?
248 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
  • 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

872 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