em.persist() を書くのはもう好きではありません。新しいインスタンスを返すときにこれを行うことはできますか?
うーん、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 を取り除くことができますか?