嗨,我对 Spring 和休眠非常陌生。我正在尝试构建一个简单的 CRUD Web 应用程序并且遇到很多异常。我正在关注网上找到的一些教程。
以下是我拥有的文件。
员工详情
package com.imaginea.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="employees_details")
public class EmployeeDetails {
private Integer id;
private String email;
private Integer managerId;
private String userName;
private String firstName;
private String lastName;
@Id
@GeneratedValue
@Column(name = "id")
public Integer getId(){
return id;
}
public void setId(Integer value){
id = value;
}
@Column(name="manager_id")
public Integer getManagerId(){
return managerId;
}
public void setManagerId(Integer value){
managerId = value;
}
@Column(name="email")
public String getEmail(){
return email;
}
public void setEmail(String value){
email = value;
}
@Column(name="user_name")
public String getUserName(){
return userName;
}
public void setUserName(String value){
email = userName;
}
@Column(name="first_name")
public String getFirstName(){
return firstName;
}
public void setFirstName(String value){
firstName = value;
}
@Column(name="last_name")
public String getLastName(){
return lastName;
}
public void setLastName(String value){
lastName = value;
}
}
EmployeeDetailsDAOImpl.java
package com.imaginea.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.imaginea.model.EmployeeDetails;
@Repository
public class EmployeeDetailsDAOImpl implements EmployeeDetailsDAO {
@Autowired
private SessionFactory sessionFactory;
public List<EmployeeDetails> getEmployeeDetailForUserName(String userName) {
List employees = sessionFactory.getCurrentSession().createQuery("FROM EmployeeDetails ed WHERE ed.userName = '" + userName + "'").list();
return employees;
}
public List<EmployeeDetails> getEmployeeDetailForEmail(String email) {
List employees = sessionFactory.getCurrentSession().createQuery("FROM EmployeeDetails ed WHERE ed.email = '" + email + "'").list();
return employees;
}
public EmployeeDetails getEmployeeDetails(Integer id){
System.out.println("in EmployeeDAO getEmployeeDetails");
System.out.println("in getEmployeeDetails id: " + id.toString());
System.out.println("in getEmployeeDetails sessionFactory.getCurrentSession(): " + sessionFactory.getCurrentSession().toString());
return (EmployeeDetails)sessionFactory.getCurrentSession().load(EmployeeDetails.class, id);
}
public void updateEmployeeDetails(EmployeeDetails employeeDetails){
sessionFactory.getCurrentSession().update(employeeDetails);
}
}
EmployeeDetailsServiceImpl
package com.imaginea.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.imaginea.dao.EmployeeDetailsDAO;
import com.imaginea.model.EmployeeDetails;
@Service
public class EmployeeDetailsServiceImpl implements EmployeeDetailsService{
@Autowired
private EmployeeDetailsDAO employeeDetailsDAO;
@Transactional
public List<EmployeeDetails> getEmployeeDetailsForUserName(String userName){
return employeeDetailsDAO.getEmployeeDetailForUserName(userName);
}
@Transactional
public List<EmployeeDetails> getEmployeeDetailsForEmail(String email){
return employeeDetailsDAO.getEmployeeDetailForEmail(email);
}
@Transactional
public EmployeeDetails getEmployeeDetails(Integer id) {
return employeeDetailsDAO.getEmployeeDetails(id);
}
@Transactional
public void updateEmployeeDetails(EmployeeDetails employeeDetails){
System.out.println("in updateEmployeeDetails");
employeeDetailsDAO.updateEmployeeDetails(employeeDetails);
System.out.println("in updateEmployeeDetails called employeeDetailsDAO.updateEmployeeDetails(employeeDetails);");
}
}
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/mvc-dispatcher-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.txt</url-pattern>
</servlet-mapping>
</web-app>
mvc-dispatcher-servlet.xml
<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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan
base-package="com.imaginea.model, com.imaginea.controller, com.imaginea.dao, com.imaginea.services" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
p:url="jdbc:mysql://localhost:3306/organization" p:username="root"
p:password="root" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.imaginea.model.LeaveApplication</value>
<value>com.imaginea.model.EmployeeDetails</value>
</list>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
现在,当我尝试解雇 employeeDetailsService.getEmployeeDetails(id); 我得到错误
org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话
我已经尝试了很多事情,但我不断收到一个错误或另一个错误。请帮忙!!!