如何使用hibernate获取记录

时间:2016-01-25 05:37:57

标签: java spring hibernate spring-mvc

我是hibernate的新手。我想使用条件获取记录。当我检索数据时,我收到错误。我在下面添加我的文件。请检查并给出解决方案。谢谢

这是我的控制器方法文件名是MainController.java

@RequestMapping(value="/Searching")
protected ModelAndView searching(/*@RequestParam("skills") String skills,@RequestParam("exp") String Exp*/@RequestParam Map<String,String> mp)
{
    ModelAndView model;
    System.out.println("Entering .....");

    sf=new Configuration().configure().buildSessionFactory();
    Session session=sf.getCurrentSession();

    try
    {

        session.beginTransaction();

        Criteria criteria=session.createCriteria(RegisterForm.class);
        criteria.add(Restrictions.like("Skills", mp.get("skills")));
        System.out.println("skills="+mp.get("skills"));  //mp.get("skills")=java_developer
        System.out.println("after criteria");
        @SuppressWarnings("unchecked")
        List<RegisterForm> register=(List<RegisterForm>)criteria.list();    //i am getting error in this line

        System.out.println("after stored to register");
        /*System.out.println("working befor commit");*/

        session.getTransaction().commit();
        session.close();

        for(RegisterForm registers:register)
            System.out.println("Names are =="+registers.getName());
    }
    catch(Exception e)
    {
        System.out.println(e);
    }

    model=new ModelAndView("Search");
    model.addObject("msg", "Welcome to search page");
    return model;
}

错误讯息是

org.hibernate.QueryException: could not resolve property: Skills of: com.maincontroller.RegisterForm

RegisterForm.java文件是

package com.maincontroller;

import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Registeration_Table")
public class RegisterForm 
{
    @Id @GeneratedValue
    private int rollNo;

    @Column(name="Name")
    private String name;

    @Column(name="Sex")
    private String sex;

    @Column(name="Skills")
    private String skills;

    @Column(name="Experience")
    private int Experience;

    @Column(name="ComPany_Name")
    private String c_Name;

    @Column(name="Salary")
    private double salary;

    @Column(name="Religion")
    private String religion;



public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

public String getSkills() {
    return skills;
}

public void setSkills(String skills) {
    this.skills = skills;
}

public int getExperience() {
    return Experience;
}

public void setExperience(int experience) {
    Experience = experience;
}

public String getC_Name() {
    return c_Name;
}

public void setC_Name(String c_Name) {
    this.c_Name = c_Name;
}

public double getSalary() {
    return salary;
}

public void setSalary(double salary) {
    this.salary = salary;
}

public String getReligion() {
    return religion;
}

public void setReligion(String religion) {
    this.religion = religion;
    }
}

我是hibernate的新手。请帮帮我,谢谢

2 个答案:

答案 0 :(得分:1)

试试这个:

GradientOrigin修改此行

MainController.java

criteria.add(Restrictions.like("Skills", mp.get("skills")));

答案 1 :(得分:0)

使用小型大写技能,因为您的字段名称是小型大写

criteria.add(Restrictions.like("skills", mp.get("skills")));