Добавление объекта в таблицу в Azure на Java - PullRequest
0 голосов
/ 06 мая 2018

Я пытаюсь добавить объект в таблицу в 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 не может получить доступ к члену таблицы класса. CustomerEntity с модификаторами «public» в java.base / jdk.internal.reflect.Reflection.newIllegalAccessException (неизвестный источник) в java.base / java.lang.reflect.AccessibleObject.checkAccess (неизвестный источник) в 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)

1 Ответ

0 голосов
/ 07 мая 2018

Вызвано: java.lang.IllegalAccessException: класс com.microsoft.azure.storage.table.PropertyPair не может получить доступ к члену таблицы классов .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;
}
}
...