使用Spring Framework坚持使用MySQL,Hibernate和JPA失败了,并没有真正持久化到数据库

时间:2011-01-04 18:32:01

标签: java hibernate spring jpa

我正在阅读有关此问题的所有帖子,但他们都没有帮助。

我试图在Spring Framework 3.0.1上使用Hibernate,JPA将我的实体持久化到MySQL。我可以从数据库和显示中找到行,当显示文本框的表单保存数据库中的字段时,这样做,但是当我尝试保存到数据库时,它根本不会更改任何行。

我的配置如下所示......

我的persistence.xml ......

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="userpersistence" transaction-type="RESOURCE_LOCAL">
        <provider>
            org.hibernate.ejb.HibernatePersistence
        </provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/acctdatabase"/>
            <property name="hibernate.connection.username" value="acct-user"/>
            <property name="hibernate.connection.password" value="password"/>
        </properties>
    </persistence-unit>
</persistence>

我的实体:

package edu.acct.tsegay.model;

import javax.enterprise.context.RequestScoped;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.springframework.beans.factory.annotation.Autowired;

@Entity
@Table(name = "programs")
public class Program {
    private int prog_Id;
    private String prog_Name;

    public Program() {
        super();
    }

    // @GeneratedValue
    @Id
    public int getProg_Id() {
        return prog_Id;
    }

    public void setProg_Id(int progId) {
        prog_Id = progId;
    }

    public String getProg_Name() {
        return prog_Name;
    }

    public void setProg_Name(String progName) {
        prog_Name = progName;
    }

    public String toString() {
        return "This is a String";
    }
}

我的道:

package edu.acct.tsegay.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import edu.acct.tsegay.model.Program;

@Repository
public class ProgramDaoImpl implements IProgramDao {
    protected EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @SuppressWarnings("unchecked")
    public List<Program> getProgram(int progId) {
        // TODO Auto-generated method stub
        return entityManager.createQuery("from program o").getResultList();
    }

    public void save(Program program) {
        entityManager.merge(program);
    }

    public Program getProg(int id) {
        return entityManager.createQuery(
                "SELECT c FROM Program c WHERE c.prog_Id = :id", Program.class)
                .setParameter("id", id).getSingleResult();
    }
}

我的服务:

package edu.acct.tsegay.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import edu.acct.tsegay.dao.IProgramDao;
import edu.acct.tsegay.model.Program;

@Service("programSerivce")
public class ProgramServiceImpl implements IProgramService {
    @Autowired
    private IProgramDao programDao;

    public IProgramDao getProgramDao() {
        return programDao;
    }

    public void setProgramDao(IProgramDao programDao) {
        this.programDao = programDao;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void save(Program program) {
        programDao.save(program);
    }

    public Program getProgram(int id) {
        return programDao.getProg(id);
    }
}

我的contextApplication

<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.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="org.h2.Driver"
    p:url="jdbc:h2:mem:gallery;DB_CLOSE_DELAY=-1" p:username="sa"
    p:password="" />
<bean id="mysqlDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.databaseurl}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="mysqlDataSource" />
    <!-- MYSQL SERVER 2008 database connection -->
<bean id="entityManagerFactoryMSSQL"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="mssqlDataSource" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="userDao" class="edu.acct.tsegay.dao.UserDaoJpa" />
<bean id="programDao" class="edu.acct.tsegay.dao.ProgramDaoImpl" />
<bean id="userSerivce" class="edu.acct.tsegay.service.UserServiceImpl" />
<bean id="programSerivce" class="edu.acct.tsegay.service.ProgramServiceImpl" />
<tx:annotation-driven mode="aspectj"
    transaction-manager="transactionManager" />
</beans>

我的控制器:

package edu.acct.tsegay.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import edu.acct.tsegay.model.Program;
import edu.acct.tsegay.model.User;
import edu.acct.tsegay.service.IProgramService;
import edu.acct.tsegay.service.IUserService;

@Controller
public class HelloController {
    @Autowired
    @Qualifier("userSerivce")
    private IUserService<User> userService;
    @Autowired
    @Qualifier("programSerivce")
    private IProgramService programService;

    public void setUserService(IUserService<User> userService) {
        this.userService = userService;
    }

    public void setProgramService(IProgramService programService) {
        this.programService = programService;
    }

    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public ModelAndView displayForm(ModelAndView model) {
        Program program = new Program();

        program.setProg_Name(programService.getProgram(16).getProg_Name());
        model.addObject("program", program);
        return model;
    }

    // Process the form.
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String processForm(Program program, Map model) {
        Program prog = new Program();
        prog.setProg_Name("New progrma");

        programService.save(prog);

        Program pro = programService.getProgram(17);
        // Add the saved book to the model
        model.put("pro", pro);
        model.put("program", program);
        return "program";
    }

    @RequestMapping("/bye")
    public ModelAndView sayBye() {
        ModelAndView model = new ModelAndView("bye");
        model.addObject("msg", "This is a Message from Tsegay done ..");
        return model;
    }

    @RequestMapping("/footer")
    public ModelAndView displayFooter() {
        ModelAndView model = new ModelAndView("footer");
        model.addObject("msg", "This is a footer ..");
        return model;
    }
}

我的观点

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Tsegay | Form</title>
    </head>
    <body>
        <form:form action="form.tsegay" method="POST" commandName="program">
            <table>
                <tr>
                    <td>First Name:</td>
                    <td><form:input path="Prog_Id" /> <form:errors path="Prog_Id" />
                    </td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><form:input path="Prog_Name" /> <form:errors
                        path="Prog_Name" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="Login" /></td>
                </tr>
            </table>
        </form:form>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

论坛帖子 Transactions are not starting 救了我的命。您需要更新(请参阅问题):

<tx:annotation-driven mode="proxy"
    transaction-manager="transactionManager" />