0

我正在使用带有 Google App Engine 的 Spring MVC 3.1.2。我在使用 JDO3 将对象持久保存在数据存储中时遇到问题。奇怪的问题是,每当我持久化对象时(在短时间内),最后添加的对象不会保留在新记录中,它只会覆盖保持相同 id 的最后一条记录(最后一条记录的 id)。以下是可能涉及的文件。

用户.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;
    }
}

用户控制器.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>
4

1 回答 1

0

What I feel is you are not initializing the User Object, instead of Autowired annotation try the following code.

@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)
}
于 2012-12-31T09:34:35.497 回答