我正在尝试将一个实体添加到Azure的表中,但是我遇到了很多错误。这是我的代码:
package table;
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.table.*;
import com.microsoft.azure.storage.table.TableQuery.*;
public class tableTutorial {
public static final String storageConnectionString=
"DefaultEndpointsProtocol=https;"+
"AccountName=my_storage_name;"+
"AccountKey=my_storage_account_key;"+
"EndpointSuffix=core.windows.net";
public static void main (String args[]) {
try
{
// Retrieve storage account from connection-string.
CloudStorageAccount storageAccount =
CloudStorageAccount.parse(storageConnectionString);
// Create the table client.
CloudTableClient tableClient =
storageAccount.createCloudTableClient();
// Create a cloud table object for the table.
CloudTable cloudTable = tableClient.getTableReference("people");
// Create a new customer entity.
CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.setEmail("Walter@contoso.com");
customer1.setPhoneNumber("425-555-0101");
// Create an operation to add the new customer to the people table.
TableOperation insertCustomer1 =
TableOperation.insertOrReplace(customer1);
// Submit the operation to the table service.
cloudTable.execute(insertCustomer1);
}
catch (Exception e)
{
// Output the stack trace.
e.printStackTrace();
}
}
}
class CustomerEntity extends TableServiceEntity {
public CustomerEntity(String lastName, String firstName) {
this.partitionKey = lastName;
this.rowKey = firstName;
}
public CustomerEntity() {
String email;
String phoneNumber;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return this.phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
我得到的错误是:
com.microsoft.azure.storage.StorageException:尝试在序列化期间访问实体的无法访问的成员。 在com.microsoft.azure.storage.table.TableServiceEntity.writeEntity(TableServiceEntity.java:468) 在com.microsoft.azure.storage.table.TableEntitySerializer.getPropertiesFromDictionary(TableEntitySerializer.java:213) 在com.microsoft.azure.storage.table.TableEntitySerializer.writeJsonEntity(TableEntitySerializer.java:129) 在com.microsoft.azure.storage.table.TableEntitySerializer.writeSingleEntityToStream(TableEntitySerializer.java:63) 在com.microsoft.azure.storage.table.TableOperation.insertImpl(TableOperation.java:381) 在com.microsoft.azure.storage.table.TableOperation.performInsert(TableOperation.java:362) 在com.microsoft.azure.storage.table.TableOperation.execute(TableOperation.java:682) 在com.microsoft.azure.storage.table.CloudTable.execute(CloudTable.java:529) 在com.microsoft.azure.storage.table.CloudTable.execute(CloudTable.java:496) at table.tableTutorial.main(tableTutorial.java:86) 引起:java.lang.IllegalAccessException:类com.microsoft.azure.storage.table.PropertyPair无法访问类table.CustomerEntity的成员,修饰符为“public” at java.base / jdk.internal.reflect.Reflection.newIllegalAccessException(Unknown Source) at java.base / java.lang.reflect.AccessibleObject.checkAccess(Unknown Source) 在java.base / java.lang.reflect.Method.invoke(未知来源) 在com.microsoft.azure.storage.table.PropertyPair.generateEntityProperty(PropertyPair.java:291) 在com.microsoft.azure.storage.table.TableServiceEntity.writeEntityWithReflection(TableServiceEntity.java:211) 在com.microsoft.azure.storage.table.TableServiceEntity.writeEntity(TableServiceEntity.java:465)
答案 0 :(得分:1)
引起:java.lang.IllegalAccessException:class com.microsoft.azure.storage.table.PropertyPair无法访问类table.CustomerEntity的成员,带有修饰符" public"
要序列化您的实体,您的CustomerEntity
应该是公开的,因此应该在单独的文件中定义它。另请注意,不要在构造函数方法中声明属性。
以下是您可以参考的代码。
package table;
import com.microsoft.azure.storage.table.TableServiceEntity;
public class CustomerEntity extends TableServiceEntity {
public CustomerEntity(String lastName, String firstName) {
this.partitionKey = lastName;
this.rowKey = firstName;
}
public CustomerEntity() { }
String email;
String phoneNumber;
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return this.phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}