Solved

Jpa query

Posted on 2014-04-28
2
186 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 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB Script to add site to Java Exception List 4 75
servlet example issue 6 38
tomcat startup error 5 59
eclipse buid path vs tomcat lib path 10 22
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

785 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