我不再喜欢写 em.persist() 了。我可以在返回新实例时执行此操作吗?
嗯,也许 id 与 grails-domains 混淆了。
例如,我有一个 AppConfig.java:
package spring;
import org.springframework.context.annotation.*;
import org.springframework.orm.jpa.*;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.*;
@Configuration
@EnableAspectJAutoProxy
@EnableTransactionManagement
public class AppConfig implements TransactionManagementConfigurer {
@Bean
public LocalEntityManagerFactoryBean entityManagerFactory() {
LocalEntityManagerFactoryBean bean = new LocalEntityManagerFactoryBean();
bean.setPersistenceUnitName("persistenceUnit");
return bean;
}
@Bean
@Scope("session")
public Test test() {
return new Test();
}
@Bean
public JpaTransactionManager txManager() {
return new JpaTransactionManager();
}
public PlatformTransactionManager annotationDrivenTransactionManager() {
return txManager();
}
}
和一个实体用户
package spring;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity @Table(name = "User")
public class User {
private long id;
@Id
@GeneratedValue
public long getId() { return id; }
public void setId(long id) { this.id = id; }
private String name;
@Column
public void setName(String name) { this.name = name; }
public String getName() { return name; }
}
一个persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="persistenceUnit">
<properties>
<property name="hibernate.ejb.cfgfile" value="/META-INF/hibernate.cfg.xml" />
</properties>
</persistence-unit>
</persistence>
和一个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 name="sessionFactory">
<property name="hibernate.hbm2ddl.auto">false</property>
<property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<property name="hibernate.connection.url">jdbc:hsqldb:mem:jamsession</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<mapping class="spring.User" />
</session-factory>
</hibernate-configuration>
一个名为 Test 的控制器:
package spring;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
@Controller
public class Test {
@PersistenceContext
transient EntityManager em;
@Transactional
public User newUser(String name) {
User user = new User();
user.setName(name);
// em.persist(user);
return user;
}
public List<User> getUsers() {
return em.createQuery("from User").getResultList();
}
}
最后但同样重要的是,这个 index.jsp:
<%@page import="spring.User"%>
<%@page import="spring.Test"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
Object o = wac.getBean("appConfig");
Test test = (Test) wac.getBean("test");
if (request.getParameter("insert") != null) {
test.newUser(request.getParameter("name"));
}
%>
<ol>
<% for (User u : test.getUsers()) {
out.print("<li>"+u.getName()+"</li>");
} %>
</ol>
<form method="post">
<input type="text" name="name" value="unnamed" />
<input type="submit" value="Insert" name="insert" />
</form>
要使用插入按钮保存新用户,我需要 em.persist(user);!我怎样才能摆脱 em.persist?