gpt4 book ai didi

java - JDO 持久字段返回 null

转载 作者:行者123 更新时间:2023-12-01 05:38:54 25 4
gpt4 key购买 nike

我正在使用GAE和GWT尝试创建一个网站,并且第一次尝试使用JDO作为数据库。当我将对象添加到数据库时,我没有收到任何错误,但是,当我尝试检索这些相同的对象时,我只检索未实例化的空对象。因此,如果我尝试创建一个 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 类,实现可序列化:

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 ;
}
}

任何关于我做错了什么的想法将不胜感激!

谢谢!

最佳答案

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

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

关于java - JDO 持久字段返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7641020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com