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;
}
}
和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();
}
}
和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());
}
}
和spring.xml文件
<?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>
我是这个春天的初学者和冬眠。在这个代码错误(java.lang.NullPointerException)出现在行(查询query = getSession()。getCurrentSession()。createSQLQuery(hql);)我没有在代码中得到什么错误...请告诉我什么时候会话被打开,当它关闭时,如何在此处执行搜索和插入查询。
答案 0 :(得分:1)
您的查询应该是
String hql = "SELECT count(e.id) FROM Employee e";
假设Employee
是您的实体类,其中id是带有@Id注释的变量,它是表中的主键。
每当您使用Hibernate查询语言时,查询都应该是 写在您的实体类和变量名称的上下文中 class而不是表名和列名