@Entity
@Table(name = "users")
public class User {
private int userId;
private String firstName;
private String middleName;
private String lastName;
private String login;
private String password;
@Transient
private String confirmPassword;
private int sex;
private String email;
private Set<Role> roles;
public User(){}
public String getConfirmPassword() {
return confirmPassword;
}
public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "users_roles", joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "role_id")})
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
@Id
@Column(name = "user_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@Column(name = "first_name")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "middle_name")
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
@Column(name = "last_name")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name = "login")
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
@Column(name = "password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "sex")
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
@Column(name = "email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", firstName='" + firstName + '\'' +
", middleName='" + middleName + '\'' +
", lastName='" + lastName + '\'' +
", login='" + login + '\'' +
", password='" + password + '\'' +
", sex=" + sex +
", email=" + email +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
if (userId != user.userId) return false;
if (sex != user.sex) return false;
if (!firstName.equals(user.firstName)) return false;
if (middleName != null ? !middleName.equals(user.middleName) : user.middleName != null) return false;
if (!lastName.equals(user.lastName)) return false;
if (login != null ? !login.equals(user.login) : user.login != null) return false;
if (password != null ? !password.equals(user.password) : user.password != null) return false;
if (confirmPassword != null ? !confirmPassword.equals(user.confirmPassword) : user.confirmPassword != null)
return false;
if (!email.equals(user.email)) return false;
return roles != null ? roles.equals(user.roles) : user.roles == null;
}
@Override
public int hashCode() {
int result = userId;
result = 31 * result + firstName.hashCode();
result = 31 * result + (middleName != null ? middleName.hashCode() : 0);
result = 31 * result + lastName.hashCode();
result = 31 * result + (login != null ? login.hashCode() : 0);
result = 31 * result + (password != null ? password.hashCode() : 0);
result = 31 * result + (confirmPassword != null ? confirmPassword.hashCode() : 0);
result = 31 * result + sex;
result = 31 * result + email.hashCode();
result = 31 * result + (roles != null ? roles.hashCode() : 0);
return result;
}
}
@Repository
public class UserDaoImpl implements UserDao {
private static final Logger log = LoggerFactory.getLogger(UserDaoImpl.class);
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void addUser(User user) {
Session session = sessionFactory.getCurrentSession();
session.persist(user);
log.info("User saves successfully. User details: " + user);
}
public void updateUser(User user) {
Session session = sessionFactory.getCurrentSession();
session.update(user);
log.info("User updated successfully. User details: " + user);
}
public void removeUser(int id) {
Session session = sessionFactory.getCurrentSession();
User user = (User) session.load(User.class, id);
if(user != null) {
session.delete(user);
}
log.info("User removed successfully. User details: " + user);
}
public User getUserById(int id) {
Session session = sessionFactory.getCurrentSession();
User user = (User) session.load(User.class, id);
log.info("User loaded successfully. User details: " + user);
return user;
}
@SuppressWarnings("unchecked")
public List<User> listUsers() {
Session session = sessionFactory.getCurrentSession();
List<User> userList = session.createQuery("from users").list();
for (User user : userList) {
log.info("User list: " + user);
}
return userList;
}
}
@Service
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Transactional
public void addUser(User user) {
userDao.addUser(user);
}
@Transactional
public void updateUser(User user) {
userDao.updateUser(user);
}
@Transactional
public void removeUser(int id) {
userDao.removeUser(id);
}
@Transactional
public User getUserById(int id) {
return userDao.getUserById(id);
}
@Transactional
public List<User> listUsers() {
return userDao.listUsers();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Notes Web Application</display-name>
<!--Zkoss config-->
<listener>
<listener-class>org.zkoss.zk.ui.http.HttpSessionListener</listener-class>
</listener>
<servlet>
<description>The ZK loader for ZUML pages</description>
<servlet-name>zkLoader</servlet-name>
<servlet-class>org.zkoss.zk.ui.http.DHtmlLayoutServlet</servlet-class>
<init-param>
<param-name>update-uri</param-name>
<param-value>/zkau</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zul</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zhtml</url-pattern>
</servlet-mapping>
<servlet>
<description>The asynchronous update engine for ZK</description>
<servlet-name>auEngine</servlet-name>
<servlet-class>org.zkoss.zk.au.http.DHtmlUpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>auEngine</servlet-name>
<url-pattern>/zkau/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<!--Spring config-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/pages/index.zul</welcome-file>
</welcome-file-list>
</web-app>
<?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" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="ru.mightynoobs.springhibernate"/>
<context:component-scan base-package="ru.mightynoobs.springsecurity"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--Database information-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/mightynoobsdb"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</bean>
<!--Hibernate 4 SessionFactory Bean definition-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>ru.mightynoobs.springhibernate.model.user.User</value>
<value>ru.mightynoobs.springhibernate.model.user.Role</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!--UserDao and UserService beans-->
<bean id="userDao" class="ru.mightynoobs.springhibernate.dao.user.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userService" class="ru.mightynoobs.springhibernate.service.user.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<!--Zkoss controller's-->
</beans>
<window id="window" title="User List" border="normal"
apply="ru.mightynoobs.springhibernate.controller.UserNotesListController">
<listbox id="listbox" model="${window$composer.userModel}" checkmark="true" mold="paging" pageSize="9">
<listhead>
<listheader width="32px" />
<listheader label="First Name" align="center" sort="auto(firstName)" />
<listheader label="Middle Name" align="center" sort="auto(middleName)" />
<listheader label="Last Name" align="center" sort="auto(lastName)" />
<listheader label="Sex" align="center" sort="auto(sex)"/>
<listheader label="eMail" align="center" sort="auto(email)" />
</listhead>
<template name="model">
<listitem>
<listcell/>
<listcell label="${each.firstName}"/>
<listcell label="${each.middleName}"/>
<listcell label="${each.lastName}"/>
<listcell label="${each.sex}" style="text-align: right"/>
<listcell label="${each.email}" style="text-align: right"/>
</listitem>
</template>
</listbox>
</window>
and after running tomcat i have this
org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [zkLoader] in context with path [] threw exception
java.lang.NullPointerException
at ru.mightynoobs.springhibernate.service.user.UserServiceImpl.listUsers(UserServiceImpl.java:45)
at ru.mightynoobs.springhibernate.controller.UserNotesListController.<init>(UserNotesListController.java:32)
对不起此代码视图。需要有关此空指针的帮助。所以我有NullPointer。我的数据库不是空的。用户表有1行。这是我第一次尝试将zkoss与Spring集成。提前谢谢
答案 0 :(得分:1)
您应该在HQL中使用类名而不是表名。