English 中文(简体)
依靠普春框架、解放党和人民军加入MySQL的做法失败了,实际上没有坚持数据库。
原标题:Persisting to MySQL using Spring Framework, Hibernate and JPA failed, not really persisting to the database

我正在阅读关于这个问题的所有职位,但都没有帮助。

我试图利用Hibernate, JPA在 3.0.1。 我可以找到数据库和显示的浏览量,这样,当标本箱的表格存放在数据库中,但在我试图节约数据库时,它根本不会改变任何浏览量。

我的组合如下:

我的坚持。

<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";
    }
}

My DAO:

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);
    }
}

我的情况

<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>
最佳回答

http://forum.springsource.org/showthread.php?67628-Transactions-are-not-starting”rel=“nofollow” 交易不是从>开始,拯救了我的生命。 您需要更新(见问题):

<tx:annotation-driven mode="proxy"
    transaction-manager="transactionManager" />
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签