我正在阅读关于这个问题的所有职位,但都没有帮助。
我试图利用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>