JDO持久字段返回null

时间:2011-10-03 21:43:52

标签: java google-app-engine gwt persistence jdo

我正在使用GAE和GWT尝试创建一个网站,并且我第一次尝试将JDO用作数据库。当我向db添加对象时,我没有收到任何错误,但是,当我尝试检索那些相同的对象时,我只检索一个未实例化的null对象。因此,如果我尝试创建一个对象User并根据用户电子邮件存储它,那么当我尝试检索同一个用户(基于其电子邮件)时,我会得到一个用户对象,其所有字段都设置为null。

JDO经理班:

package com.sixpac.website.server;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Collections;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;

import com.sixpac.website.client.User;

public class UserManager implements UserInterface 
{
    private static final PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory( "transactions-optional" ) ;

    /**
     * create and return an instance of the persistence manager
     * @return
     */
    public static PersistenceManagerFactory getPersistenceManagerFactory()
    {
        return pmfInstance ;
    }


    public void addUser( User user )
    {
        PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager() ;

        System.out.println( "in addUser manager: " + user.getEmail() + ":" + user.getPassword() + ":" + user.getFName() + ":" + user.getLName() ) ;

        try
        {
            pm.currentTransaction().begin() ;
            pm.makePersistent( user ) ;
            pm.currentTransaction().commit() ;
        }
        catch( Exception e )
        {
            pm.currentTransaction().rollback() ;
            System.out.println( "addUser: " + e ) ;
//          throw new RuntimeException( e ) ;
        }
        finally
        {
            pm.close() ;
        }
    }

    public void removeUser( String email ) 
    {
        PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager() ;

        User user ;

        try
        {
            pm.currentTransaction().begin() ;
            user = pm.getObjectById( User.class, email ) ;
            pm.deletePersistent( user ) ;
            pm.currentTransaction().commit() ;
        }
        catch( Exception e )
        {
            pm.currentTransaction().rollback() ;
            System.out.println( "removeUser: " + e ) ;
//          throw new RuntimeException( e ) ;
        }
        finally
        {
            pm.close() ;
        }
    }

    public void updateUser( String email, int state ) 
    {
        PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager() ;

        User user ;

        try
        {
            pm.currentTransaction().begin() ;
            user = pm.getObjectById( User.class, email ) ;
            user.setState( state ) ;

            pm.makePersistent( user ) ;
            pm.currentTransaction().commit() ;
        }
        catch( Exception e )
        {
            pm.currentTransaction().rollback() ;
            System.out.println( "updateUser: " + e ) ;
//          throw new RuntimeException( e ) ;
        }
        finally
        {
            pm.close() ;
        }
    }

    public User getUser( String email ) 
    {
        PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager() ;

        System.out.println( "in getUser manager: " + email ) ;

        User user = new User() ;
        try
        {
            pm.currentTransaction().begin() ;
            user = pm.getObjectById( User.class, email ) ;
            pm.currentTransaction().commit() ;
        }
        catch( Exception e )
        {
            pm.currentTransaction().rollback() ;
//          throw new RuntimeException( e ) ;
        }
        finally
        {
            pm.close() ;
        }

        return user ;
    }

    public List<User> listUser() 
    {
        PersistenceManager pm = getPersistenceManagerFactory().getPersistenceManager() ;
        String query = "select from " + User.class.getName() ;
        return (List<User>) pm.newQuery( query ).execute() ;
    }
}

User类,它实现了serializable:

package com.sixpac.website.client;

import java.io.Serializable;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class User implements Serializable 
{
    private static final long ID = 1L ;

    @PrimaryKey
    @Persistent
    private String email ;
    @Persistent
    private String fname ;
    @Persistent
    private String lname ;
    @Persistent
    private String psswrd ;
    @Persistent
    private String inst ;
    @Persistent
    private String details ;
    @Persistent
    private int state ;

    public User()
    {
        email = null ;
        fname = null ;
        lname = null ;
        psswrd = null ;
        inst = null ;
        details = null ;
        state = 0 ;
    }

    public User( String email, String psswrd, String fname, String lname, String inst, String details )
    {
        this.psswrd = psswrd ;
        this.fname = fname ;
        this.lname = lname ;
        this.email = email ;
        this.inst = inst ;
        this.details = details ;
        state = 0 ;
    }

    public String getFName()
    {
        return fname ;
    }
    public void setFName( String fname )
    {
        this.fname = fname ;
    }

    public String getLName()
    {
        return lname ;
    }
    public void setLName( String lname )
    {
        this.lname = lname ;
    }

    public String getPassword()
    {
        return psswrd ;
    }
    public void setPassword( String psswrd )
    {
        this.psswrd = psswrd ;
    }

    public String getEmail()
    {
        return email ;
    }
    public void setEmail( String email )
    {
        this.email = email ;
    }

    public String getInstetute()
    {
        return inst ;
    }
    public void setInstetute( String inst )
    {
        this.inst = inst ;
    }

    public String getDetails()
    {
        return details ;
    }
    public void setDetails( String details )
    {
        this.details = details ;
    }

    public int getState()
    {
        return state ;
    }
    public void setState( int state )
    {
        this.state = state ;
    }
}

对我所做错的任何想法都将不胜感激!

谢谢!

1 个答案:

答案 0 :(得分:0)

UserManager应该由@PersistenceAware注释,对于用于对实体执行持久性操作的所有类也是如此。

http://www.datanucleus.org/products/accessplatform_3_0/jdo/class_mapping.html