GoogleAppEngineでSpringMVC3.1.2を使用しています。JDO3を使用してデータストア内のオブジェクトを永続化することに問題があります。奇妙な問題は、オブジェクトを永続化するときはいつでも(短時間)、最後に追加されたオブジェクトが新しいレコードに永続化されず、同じID(最後のレコードのID)を保持して最後のレコードを上書きするだけです。関係する可能性のあるファイルは次のとおりです。
User.java
@Component
@PersistenceCapable
public class User {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String firstName;
@Persistent
private String lastName;
@Persistent
private String email;
@Persistent
private String password;
public Long getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
public void setId(Long id) {
this.id = id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmail(String email) {
this.email = email;
}
public void setPassword(String password) {
this.password = password;
}
}
UserDaoImpl.java
@Repository
public class UserDaoImpl implements UserDao {
private static PersistenceManager pm;
@Override
public void add(User user) {
if (pm == null || pm.isClosed()) {
pm = PMF.get().getPersistenceManager();
}
try {
pm.makePersistent(user);
} finally {
pm.close();
}
}
}
PMF.java
public final class PMF {
private static final PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");
private PMF() {}
public static PersistenceManagerFactory get() {
return pmfInstance;
}
}
UserController.java
@Controller
public class UserController {
@Autowired
UserService userService;
@Autowired
User user;
@RequestMapping(value="/adduser", method=RequestMethod.GET)
String adduser(){
return "adduser";
}
@RequestMapping(value="/adduser.do", method=RequestMethod.POST)
String saveUserr(@RequestParam String firstname,@RequestParam String lastname, @RequestParam String email, @RequestParam String password){
this.user.setFirstName(firstname);
this.user.setLastName(lastname);
this.user.setEmail(email);
this.user.setPassword(password);
userService.add(user);//this invokes the UseDaoImpl add(user)
}
}
jdoconfig.xml
<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig_3_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig http://java.sun.com/xml/ns/jdo/jdoconfig_3_0.xsd">
<persistence-manager-factory name="transactions-optional">
<property name="javax.jdo.PersistenceManagerFactoryClass"
value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory"/>
<property name="javax.jdo.option.ConnectionURL" value="appengine"/>
<property name="javax.jdo.option.NontransactionalRead" value="true"/>
<property name="javax.jdo.option.NontransactionalWrite" value="true"/>
<property name="javax.jdo.option.RetainValues" value="true"/>
<property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
<property name="datanucleus.appengine.singletonPMFForName" value="true"/>
</persistence-manager-factory>
</jdoconfig>