• Register

Share:- Whatsapp Facebook Facebook

------------------------------------- Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

0 votes
1.5k views
What is named SQL query in Hibernate?
in Hibernate by
edited

1 Answer

0 votes

Named Query is a type of query that is witten in simple bean (setter or getter) class. Any query is written in bean class for updating or working with databse.

You will see the following example to use Named Queries.

Employee.java
package developerhelpway.jpafunctions;

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;

@Entity
@Table(name="Employee")
@javax.persistence.NamedQuery(name="empAllRecords",
 query="SELECT emp FROM Employee emp")
@NamedQueries({
    @NamedQuery(name="empGetRecords",
query="SELECT emp FROM Employee emp"),
    @NamedQuery(name="empDeleteRecords",
 query="DELETE FROM Employee emp WHERE emp.id= ?1")

})
public class Employee {

  @Id
  @GeneratedValue
  @Column(name="id")
  private int id;
 
  @Column(name="empName")
  private String empName;
 
  @Column(name="empSalary")
  private int empSalary;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getEmpName() {
    return empName;
  }

  public void setEmpName(String empName) {
    this.empName = empName;
  }

  public int getEmpSalary() {
    return empSalary;
  }

  public void setEmpSalary(int empSalary) {
    this.empSalary = empSalary;
  }
 
}

For more: http://www.developerhelpway.com/framework/jpahibernate/jpa-named-queries.php

 

by
edited
...