我正在使用 java , struts2 ,休眠构建简单的 MVC 应用。我创建了应用的结构我希望休眠创建表以继续执行下一个任务。 我相信,在将JARS导入到我的应用及其正确版本时,我有一个缺少事实。当我尝试插入数据时,hibernate不会创建用户表,并告诉我 session为空。
这是我的代码和导入的jars。
谢谢!
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/development</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">dothebest</property>
<property name="connection.pool_size">1</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-- <mapping class="com.SchoolManagement.domain.Address" /> -->
<mapping class="com.SchoolManagement.domain.User" />
</session-factory>
</hibernate-configuration>
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="hibernate-default,struts-default">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="home" method="home" class="com.SchoolManagement.action.BaseAction">
<result name="success" type="tiles">home</result>
</action>
<action name="training" method="training" class="com.SchoolManagement.action.BaseAction">
<result name="success" type="tiles">training</result>
</action>
<action name="contact" method="contact" class="com.SchoolManagement.action.BaseAction">
<result name="success" type="tiles">contact</result>
</action>
<action name="listUser" method="list" class="com.SchoolManagement.action.UserAction">
<result name="success" type="tiles">listmyUser</result>
</action>
<action name="saveOrUpdateUser" method="saveOrUpdate" class="com.SchoolManagement.action.UserAction">
<result name="success" type="tiles">listmyUser</result>
</action>
</package>
</struts>
UserAction.java
package com.SchoolManagement.action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.ArrayList;
import java.util.List;
import com.SchoolManagement.domain.User;
import com.SchoolManagement.service.UserService;
import com.SchoolManagement.service.UserServiceImpl;
public class UserAction extends ActionSupport implements ModelDriven<User>{
private static final long serialVersionUID = -6659925652584240539L;
private User user = new User();
private List<User> userList = new ArrayList<User>();
private UserService userService = new UserServiceImpl();
@Override
public User getModel() {
return user;
}
/**
* To save or update user.
* @return String
*/
public String saveOrUpdate()
{
userService.saveOrUpdateUser(user);
return SUCCESS;
}
public String list()
{
userList = userService.listUser();
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
UserServiceImpl.java
package com.SchoolManagement.service;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.SchoolManagement.domain.User;
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
public class UserServiceImpl implements UserService{
@SessionTarget
Session session;
@TransactionTarget
Transaction transaction;
/**
* Used to save or update a user.
*/
@Override
public void saveOrUpdateUser(User user) {
try {
session.saveOrUpdate(user);
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}
/**
* Used to list all the users.
*/
@SuppressWarnings("unchecked")
@Override
public List<User> listUser() {
// TODO Auto-generated method stub
List<User> userList = null;
System.out.println("1");
try {
System.out.println("2");
userList = session.createQuery("from User").list();
System.out.println("3");
} catch (Exception e) {
e.printStackTrace();
}
return userList;
}
}
listUser.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration Page</title>
<head>
</head>
<body>
<div class="page">
<s:form action="saveOrUpdateUser">
<s:push value="user">
<s:hidden name="id" />
<s:textfield name="name" label="User Name" />
<s:textfield name="email" label="Email" />
<s:submit />
</s:push>
</s:form>
<s:if test="userList.size() > 0">
<div class="content">
<table class="userTable" cellpadding="5px">
<tr class="even">
<th>Name</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<s:iterator value="userList" status="userStatus">
<tr
class="<s:if test="#userStatus.odd == true ">odd</s:if> <s:else>even</s:else>">
<td><s:property value="name" /></td>
<td><s:property value="email" /></td>
<td>
<s:url id="editURL" action="editUser">
<s:param name="id" value="%{id}"></s:param>
</s:url>
<s:a href="%{editURL}">Edit</s:a>
</td>
<td>
<s:url id="deleteURL" action="deleteUser">
<s:param name="id" value="%{id}"></s:param>
</s:url>
<s:a href="%{deleteURL}">Delete</s:a>
</td>
</tr>
</s:iterator>
</table>
</div>
</s:if>
</div>
</body>
</html>
这是我的罐子
这是错误:
答案 0 :(得分:1)
对于休眠核心会话和事务注入功能,我们需要使用basicStackHibernate
拦截器。
因此将<interceptor-ref name="basicStackHibernate" />
添加到您的操作中:
<action name="listUser" method="list" class="com.SchoolManagement.action.UserAction">
<interceptor-ref name="basicStackHibernate" />
<result name="success" type="tiles">listmyUser</result>
</action>