代码之家  ›  专栏  ›  技术社区  ›  Spedge

从JDO类访问自定义类

  •  0
  • Spedge  · 技术社区  · 16 年前

    是的,我不知道我是不是找错了方向——我发现JDO和谷歌看起来有点棘手。不管怎样,给你。

    我有一个类,它包含另一个类作为它的内部变量之一(参见player1)

      @PersistenceCapable(identityType = IdentityType.APPLICATION)
       public class JDOGame 
       {    
        @PrimaryKey 
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
        private Long id; 
    
        @Persistent
        private String map; 
    
        @Persistent
        private RPCDataPlayer player1;
    
       // getters, setters, constructors etc...
       }
    

    类rpcdataplayer是可序列化的,非常基本……

    public class RPCDataPlayer implements IsSerializable 
    {
        public String name;
        public int id;
    
            // getters & setters & constructors oh my
    
            public int getId() { return id; }
    }
    

    所以,我的问题是……我如何创建一个查询,在该查询中我可以获取包含id=x的rpcdataplayer的所有jdogames?

    我不能像…

    SELECT FROM JDOGame.class.getName() WHERE player1.getId() == x
    

    …那么,人们有什么技术或建议来实现这一目标呢?

    事先谢谢。

    2 回复  |  直到 16 年前
        1
  •  2
  •   Patrick    16 年前

    谷歌应用引擎数据库不是关系数据库,因此您不能进行连接。您可以将rpcdataplayer持久化为表。

    @PersistenceCapable(identityType = IdentityType.APPLICATION)
    public class RPCDataPlayer {
    
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key key;
    
        @Persistent(mappedBy = "player1")
        private List<JDOGame> jdoGames = new ArrayList<JDOGame>();
    
        @Persistent
        public String name;
    
        @Persistent
        public int id;
    
        public int getId() {
            return id;
        }
    
        public Key getKey() {
            return key;
        }
    
        public void setJdoGames(List<JDOGame> jdoGames) {
            this.jdoGames = jdoGames;
        }
    
        public List<JDOGame> getJdoGames() {
            return jdoGames;
        }
    
    }
    

    然后你可以这样查询。

    SELECT FROM RPCDataPlayer.class.getName() WHERE id == x
    

    一旦您拥有和rpcdataplayer实例,您可以通过调用以下命令获取jdogame:

    List<JDOGame> jdoGames = rpcDataPlayer.getJdoGames();
    
        2
  •  1
  •   DataNucleus    16 年前

    您的字段在数据存储中是串行的,因此显然不能在数据存储中进行查询,因此所有这些记录都需要检索,查询在内存中进行。当GAE/J最终将他们的行为结合起来并允许人们这样做时,这将是微不足道的,直到那时,您需要自己检索所有记录并进行检查。

    与联接完全无关