持久化对象覆盖最后一条记录,而不是使用Spring MVC,Google App Engine,JDO3创建新记录

时间:2012-12-30 19:36:30

标签: google-app-engine spring-mvc jdo

我正在使用Spring MVC 3.1.2和Google App Engine。我使用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>

1 个答案:

答案 0 :(得分:0)

我的感觉是你没有初始化用户对象,而不是Autowired注释尝试以下代码。

@RequestMapping(value="/adduser.do", method=RequestMethod.POST)
String saveUser(@RequestParam String firstname,@RequestParam String lastname,   @RequestParam String email, @RequestParam String password){
    User user = new User();
    user.setFirstName(firstname);
    user.setLastName(lastname);
    user.setEmail(email);
    user.setPassword(password);
    userService.add(user);//this invokes the UseDaoImpl add(user)
}