?
Solved

Jpa query

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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month7 days, 19 hours left to enroll

765 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