简体   繁体   中英

createSQLQuery in spring using hibernate not working

employee.java

package pckg;


import java.util.*;
import javax.persistence.Entity;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table( name = "employee")
public class Employee {

public void Employee(int empid,String empName,String empPassword, boolean isPermanent){
    this.empid=empid;
    this.empName=empName;
    this.empPassword=empPassword;
    this.isPermanent=isPermanent;
}

@Id
private int empid;
private String empName;
private String empPassword;
private boolean isPermanent;

@Transient
public String getEmpPassword() {
    return empPassword;
}
public void setEmpPassword(String empPassword) {
    this.empPassword = empPassword;
}

@Basic
public boolean isPermanent() {
    return isPermanent;
}
public void setPermanent(boolean isPermanent) {
    this.isPermanent = isPermanent;
}


@Id
@Column(name = "Empid")
public int getEmpid() {
    return empid;
}
public void setEmpid(int empid) {
    this.empid = empid;
}
public String getEmpName() {
    return empName;
}
public void setEmpName(String empName) {
    this.empName = empName;
}

}

and employeeDaoImpl.java

package pckg.dao;
import java.util.*;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.annotations.NamedQuery;
import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import pckg.Employee;
import pckg.dao.*;


@Repository 
public class EmployeeDaoImpl {

@Autowired
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
    return sessionFactory;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

private Employee employee;
private int empid;

public Employee getEmployee(int empid) {
    return (Employee)getSessionFactory().getCurrentSession().get(Employee.class,  empid);
}


public void add(Employee employee) {
    getSessionFactory().getCurrentSession().save(employee);
}

public void delete(int empid) {
    getSessionFactory().getCurrentSession().delete(getEmployee(empid));
}

public void edit(Employee employee) {
    getSessionFactory().getCurrentSession().update(employee);
}

public int getAllEmployee(){
    String hql = "select count(e) from Employee e";
    ;
    System.out.println("hello");
    Query query = getSessionFactory().openSession().createQuery(hql);
    System.out.println("hello");
    return ((Long) query.uniqueResult()).intValue();
}
}

and employeeServiceImpl.java

package pckg.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pckg.Employee;
import pckg.dao.EmployeeDaoImpl;


@Service
public class EmployeeServiceImpl  {

@Autowired
private EmployeeDaoImpl employeeDao;

@Transactional
public void add(Employee employee) {
    employeeDao.add(employee);
}

@Transactional
public void delete(int empid) {
    employeeDao.delete(empid);

}

@Transactional
public void edit(Employee employee) {
    employeeDao.edit(employee);

}

@Transactional
public Employee getEmployee(int empid) {
    return employeeDao.getEmployee(empid);
}

@Transactional
public int getAllEmployee() {
    return employeeDao.getAllEmployee();
}
public static void main(String arg[]){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
EmployeeDaoImpl dao = (EmployeeDaoImpl)  ctx.getBean("EmployeeDaoImpl",EmployeeDaoImpl.class);
System.out.println(dao.getAllEmployee());
}
}

and spring.xml file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/employee_record"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<!-- <property name="maxActive" value="5"/>  -->

</bean>



<bean id ="sessionFactory"    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
 <property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="pckg"/>
<property name="hibernateProperties">
<props>
    <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<bean name="EmployeeDaoImpl" class="pckg.dao.EmployeeDaoImpl"></bean>

</beans>

I am very beginner to this spring and hibernate. in this code error(java.lang.NullPointerException) comes at line(Query query = getSession().getCurrentSession().createSQLQuery(hql);) i am not getting whats wrong in the code... AND please tell me when the session gets open and when it gets closed and how can i perform search and insert query in this.

your query should be

String hql = "SELECT count(e.id) FROM Employee e";

assuming Employee is your entity class where id is the variable with @Id annotations which is primary key in your table.

Whenever you are using Hibernate query language, the queries should be written in context of your entity class and variable names in the class instead of table and column names

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM