user2083175
user2083175

Reputation: 133

Select query in hibernate annotations in spring mvc

Hi i am writing an spring mvc, employee application using mysql database,hibernate annotations and jsp . The database contains one table "Empdata" where empid is primary key.And there is a column "team" in "Empdata".I want to select employees in a specific team, example all the details of employees in "Team1".Here i can perform delete and edit operations in the application. For delete opertaion i am using

sessionfactory.getCurrentSession().createQuery("DELETE FROM Resource WHERE empid=" +resource.getEmpId()).executeUpdate();

query.I know the commandline query for select is

SELECT * FROM EmpData ERE EMPLTEAM ="Team1"

I want to know how to convert this query into hibernate.

please help,thanks in advance..

Upvotes: 1

Views: 1440

Answers (2)

you can convert the query in the following way:

String sql = "select ed from EmpData ed where emplTeam = :emplTeam";

Query query = session.createQuery(sql);

query.setParameter("emplTeam ", team);

List<EmpData> empDataList = (List<EmpData>)query.list();

but you should have a class called EmpData containing a property emplTeam similar to the following:

@Entity
@Table(name = "EmpData")
class EmpData {

    ....

    @Column(name = "EMPLTEAM")
    private String emplTeam;

    public String getEmplTeam() {
        return emplTeam;
    }

    public void setEmplTeam(String emplTeam) {
        this.emplTeam = emplTeam;
    }
}

(I used annotations hibernate .. but you can do it the same way using .hbm.xml files)

Upvotes: 2

Aviad
Aviad

Reputation: 1549

For example

Query query = session.createQuery("from Student where name=:name");
query.setParameter("name", "Raj");

In your case i guess the Entity name is Empdata(The object that represent the table) And the field in the object is team(That has getter and setter in object)

Query query = session.createQuery("from Empdata where team=:teamParam");
query.setParameter("teamParam", "team1");

Upvotes: 0

Related Questions