我正在学习spring hvc的spring。我已经尝试了一个教程,其中显示了反向工程和hbm.xml。但我想为它使用带注释的域类。为此我尝试了一些,但没有运气。我使用spring和hibernate框架在NetBeans中进行了一个Web项目。当我提交表单时,它工作正常,并重定向到其他页面,但数据未保存。有谁可以请帮助我吗?这里是我的尝试下面::休眠和弹簧mvc组合不能正常工作
我的项目结构>>>
我的调度员servlet.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.controller" />
<mvc:annotation-driven/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
</beans>
我的应用程序上下文>>
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
</beans>
我的web.xml >>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<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>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
我的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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/springhibernate?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.jdbc.batch_size">50</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>
</session-factory>
</hibernate-configuration>
我的用户控制器>>>
@Controller
public class UserController {
private static SessionFactory factory;
@RequestMapping(value="/getForm", method = RequestMethod.GET)
public ModelAndView getDemoForm(){
ModelAndView model = new ModelAndView("demoForm");
return model;
}
@RequestMapping(value = "/submitDemoForm.html", method = RequestMethod.POST)
public ModelAndView submitAdmissionForm(@ModelAttribute("employee") EmployeeAnnotation employee) {
try {
factory = new AnnotationConfiguration().
configure().
//addPackage("com.xyz") //add package if used.
addAnnotatedClass(EmployeeAnnotation.class).
buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
Session session = factory.openSession();
Transaction tx = null;
Integer employeeID = null;
try {
tx = session.beginTransaction();
employeeID = (Integer) session.save(employee);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
ModelAndView model = new ModelAndView("demoFormSuccess");
return model;
}
}
我EmployeeAnnotation类(POJO)>>>
import javax.persistence.*;
@Entity
@Table(name = "employee_annotation")
public class EmployeeAnnotation {
@Id @GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "salary")
private int salary;
public EmployeeAnnotation() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String first_name) {
this.firstName = first_name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String last_name) {
this.lastName = last_name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
我demoForm.jsp >>>
<form action="/springMvcHibernate/submitDemoForm.html" method="post">
<p>
Firts Name : <input type="text" name="firstName"/>
</p>
<p>
Last Name : <input type="text" name="lastName"/>
</p>
<p>
Salary : <input type="number" name="salary"/>
</p>
<input type="submit" value="Submit"/>
</form>
我demoFormSuccess.jsp >>>
<body>
<h1>ID is :: ${employee.id}</h1>
</body>
你也可以做的就是添加'@Transactional(隔离= Isolation.READ_COMMITTED)'所以你不需要刷新,可以压倒你的数据库中的会话。但为此,您需要创建一个调用DAO的服务。它不适用于上面的情况,但不要将'session.flush()' –
谢谢你的回复。我试过你的解决方案,但没有运气。它给了以下错误>>'org.hibernate.impl.SessionFactoryObjectFactory - 没有约束力工厂JNDI,没有JNDI名称configured','org.hibernate.util.JDBCExceptionReporter - SQL错误:1364 SQLSTATE:HY000','组织.hibernate.util.JDBCExceptionReporter - 字段'id'没有默认值,'org.hibernate.exception.GenericJDBCException:无法插入:[com.model。EmployeeAnnotation]' –
它说没有生成id(主键),因为你没有指定任何策略,所以你可以改变你的id(实例变量)如下:@GeneratedValue(strategy = GenerationType.AUTO) – developer