Google App Engine JDO 如何定义实例字段?
我有一个这样的类:
import java.io.*;
import java.util.*;
public class Contact_Info_Entry implements Serializable
{
public static final long serialVersionUID=26362862L;
String Contact_Id,First_Name="",Last_Name="",Company_Name="",Branch_Name="",Address_1="",Address_2="",City="",State="",Zip="",Country="",E_Mail="",Phone;
int I_1,I_2;
float F_1,F_2;
boolean B_1,B_2;
GregorianCalendar Date_1, Date_2;
Vector<String> A_Vector=new Vector<String>();
public Contact_Info_Entry() { }
......
}
如果我想将其转换为 JDO 类,我是否需要自己定义每个字段,或者我可以一次定义一个组吗?
例如,我是否必须这样做:
@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class Contact_Info_Entry implements Serializable
{
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
public static final long serialVersionUID=26362862L;
@Persistent
String Contact_Id;
@Persistent
String First_Name;
@Persistent
String Last_Name;
......
@Persistent
int I_1;
@Persistent
int I_2;
...
@Persistent
float F_1;
...
@Persistent
boolean B_1;
@Persistent
boolean B_2;
@Persistent
GregorianCalendar Date_1;
...
@Persistent
Vector<String> A_Vector=new Vector<String>();
public Contact_Info_Entry() { }
......
}
或者我可以像这样一次做一个组:
@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class Contact_Info_Entry implements Serializable
{
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
public static final long serialVersionUID=26362862L;
@Persistent
String Contact_Id,First_Name,Last_Name="";
......
@Persistent
int I_1=0,I_2=1;
...
@Persistent
float F_1;
...
@Persistent
boolean B_1,B_2;
@Persistent
GregorianCalendar Date_1;
...
@Persistent
Vector<String> A_Vector=new Vector<String>();
public Contact_Info_Entry() { }
......
}
或者我可以像这样一起跳过“@Persistent”:
import java.io.*;
import java.util.*;
@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class Contact_Info_Entry implements Serializable
{
public static final long serialVersionUID=26362862L;
String Contact_Id,First_Name="",Last_Name="",Company_Name="",Branch_Name="",Address_1="",Address_2="",City="",State="",Zip="",Country="",
E_Mail="",Phone;
int I_1,I_2;
float F_1,F_2;
boolean B_1,B_2;
GregorianCalendar Date_1, Date_2;
Vector<String> A_Vector=new Vector<String>();
public Contact_Info_Entry() { }
......
}
哪些是正确的?
坦率
I have a class like this :
import java.io.*;
import java.util.*;
public class Contact_Info_Entry implements Serializable
{
public static final long serialVersionUID=26362862L;
String Contact_Id,First_Name="",Last_Name="",Company_Name="",Branch_Name="",Address_1="",Address_2="",City="",State="",Zip="",Country="",E_Mail="",Phone;
int I_1,I_2;
float F_1,F_2;
boolean B_1,B_2;
GregorianCalendar Date_1, Date_2;
Vector<String> A_Vector=new Vector<String>();
public Contact_Info_Entry() { }
......
}
If I want to translate it to a class for JDO, do I need to define each field by it self or can I do a group at a time ?
For instance do I have to make it like this :
@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class Contact_Info_Entry implements Serializable
{
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
public static final long serialVersionUID=26362862L;
@Persistent
String Contact_Id;
@Persistent
String First_Name;
@Persistent
String Last_Name;
......
@Persistent
int I_1;
@Persistent
int I_2;
...
@Persistent
float F_1;
...
@Persistent
boolean B_1;
@Persistent
boolean B_2;
@Persistent
GregorianCalendar Date_1;
...
@Persistent
Vector<String> A_Vector=new Vector<String>();
public Contact_Info_Entry() { }
......
}
Or can I do a group at a time like this :
@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class Contact_Info_Entry implements Serializable
{
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
public static final long serialVersionUID=26362862L;
@Persistent
String Contact_Id,First_Name,Last_Name="";
......
@Persistent
int I_1=0,I_2=1;
...
@Persistent
float F_1;
...
@Persistent
boolean B_1,B_2;
@Persistent
GregorianCalendar Date_1;
...
@Persistent
Vector<String> A_Vector=new Vector<String>();
public Contact_Info_Entry() { }
......
}
Or can I skip the "@Persistent" all together like this :
import java.io.*;
import java.util.*;
@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class Contact_Info_Entry implements Serializable
{
public static final long serialVersionUID=26362862L;
String Contact_Id,First_Name="",Last_Name="",Company_Name="",Branch_Name="",Address_1="",Address_2="",City="",State="",Zip="",Country="",
E_Mail="",Phone;
int I_1,I_2;
float F_1,F_2;
boolean B_1,B_2;
GregorianCalendar Date_1, Date_2;
Vector<String> A_Vector=new Vector<String>();
public Contact_Info_Entry() { }
......
}
Which are correct ?
Frank
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要使用 @Persistent 注释每个字段。绝大多数类型(原语、它们的包装器、字符串等)默认是持久的。只需按照您的班级应有的方式编写您的班级即可。读
http://www.datanucleus.org/products/accessplatform_1_1/jdo/types。 html
您无法保留静态字段
http://www.datanucleus.org/products/accessplatform_1_1/jdo/fields_properties。 html
Google 文档对所有内容使用 @Persistent 完全具有误导性
至于同一行上的多个字段的声明以及注释是否适用于所有字段,我假设编译器会将注释应用于该行上的所有字段,但这与 Java 以及编译器如何实现注释有关,而不是与 JDO 特定的任何内容有关。
You do NOT need to annotate every field with @Persistent. The vast majority of types (primitives, their wrappers, String etc etc) are by default persistent. Just write your class as your class ought to be. Read
http://www.datanucleus.org/products/accessplatform_1_1/jdo/types.html
You can't persist static fields
http://www.datanucleus.org/products/accessplatform_1_1/jdo/fields_properties.html
Google's docs usage of @Persistent on everything is totally misleading
As for declarations of multiple fields on the same line and whether the annotation applies to all, I would assume that the compiler will apply the annotation to all fields on that line, but that is to do with Java and how the compiler implements annotations rather than anything specific to JDO.