【问题标题】:Azure insert does not add new properties of an entity (Java)Azure 插入不添加实体的新属性 (Java)
【发布时间】:2019-03-16 02:29:42
【问题描述】:

我有一个带有 Azure 表存储的数据库,我在其中存储具有各种属性的实体,并且一切运行良好。但是,当我尝试添加新属性时,它根本不会被存储。我的 InsertEntity 代码如下所示:

LunchDateEntity entity = new LunchDateEntity(ID);
try{

AppStorage storage = new AppStorage(TABLENAME);
entity.setProperty1(prop1)
entity.setProperty2(prop2)
entity.setProperty3(prop3)

entity.setNewProperty(newProp)

TableOperation operation = TableOperation.insert(entity);
m_table.execute(operation);
} 
catch(...) {...}

Prop 1,2,3 是我之前拥有的实体,它们被添加到表存储中,但是,newProp 并没有在表存储中显示。

我尝试在构建实体时对属性进行硬编码,但无济于事。

我的实体类看起来像:

public class myEntity extends TableServiceEntity {
public static final String TABLENAME = "myTable";
public static final String PARTITION_KEY = "part1";

public String m_prop1 = "";
public String m_prop2 = "";
public String m_prop3 = "";
public String m_newProp = "";

public myEntity() {
 this.partitionKey = PARTITION_KEY;
} 

public void setProp1(String prop1) {
  m_prop1 = prop1;
}

public String getProp1() {
 return m_prop1;
}

public void setNewProp(String newProp) {
 m_newProp = newProp;
}

等等所有属性。他们看起来都一样。

我是否误解了插入功能的工作原理?有什么想法为什么这不起作用?

提前致谢

【问题讨论】:

  • 无法在我这边重现您的问题。您可以使用 Fiddler 捕获插入请求正文以查看您的实体吗?
  • 您使用的是哪个表存储版本?新属性不显示或只是空值?
  • 这很奇怪......我的代码位于函数应用程序中,我插入带有 HTTP POST 请求的实体。如果我对实体进行字符串化并返回它,则 newProp 会显示在 Postman 的响应中。在我看来,问题出在插入功能上。我将尝试将 TableStorage 更新到 1.4.2,看看会发生什么。
  • 当然,我也会尝试在 azure http 触发函数中插入实体。
  • 更新没有任何区别。该值不为空,该表根本没有该属性的列。编辑:我有所有属性的设置器和获取器,但 newProp 设置/获取是从早期有效的道具复制+粘贴。

标签: java azure


【解决方案1】:

根据我的测试,我认为这是因为名称规则。我测试了添加 newProp 或 newProp1 字段,与您的解决方案相同。新房没有出现。

然后我尝试添加名称、电话甚至 oldProp,一切正常。

功能代码:

@FunctionName("addentity")
    public String addentity(@HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) String req,
                            ExecutionContext context) {

        String storageConnectionString =
                "***";
        String jsonStr = "insert failed";

        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("test");

            // Create a new customer entity.
            myEntity entity = new myEntity("jay1","gong");
            //entity.setProp1("a");
            entity.setNewProp("new");
            entity.setOldProp("old");
            //entity.setNewProp1("new1");
            //entity.setName("kkkkk");

            // Create an operation to add the new customer to the people table.
            TableOperation insertEntity =
                    TableOperation.insert(entity);

            // Submit the operation to the table service.
            cloudTable.execute(insertEntity);

            jsonStr = entity.toString();

        } catch (Exception e) {
            // Output the stack trace.
            e.printStackTrace();
        }


        return jsonStr;
    }

我的实体:

package com.fabrikam.functions;

import com.microsoft.azure.storage.table.TableServiceEntity;

public class myEntity extends TableServiceEntity {

    public String m_prop1 = "";
    public String m_newProp = "";
    public String m_newProp1 = "";

    public String name = "";
    public String oldProp = "";

    public myEntity(String lastName, String firstName) {
        this.partitionKey = lastName;
        this.rowKey = firstName;
    }

    public void setProp1(String prop1) {
        m_prop1 = prop1;
    }

    public String getProp1() {
        return m_prop1;
    }


    public String getNewProp(String newProp) {
        return m_newProp;
    }

    public String getNewProp1(String newProp1) {
        return m_newProp1;
    }


    public void setNewProp(String newProp) {
        m_newProp = newProp;
    }

    public void setNewProp1(String newProp1) {
        m_newProp1 = newProp1;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOldProp() {
        return oldProp;
    }

    public void setOldProp(String oldProp) {
        this.oldProp = oldProp;
    }
}

输出:

我认为像 'newProp' 这样的东西已经被 Azure 表存储保留了,你可以调整命名规则然后一切都会好起来的。


我做进一步测试:


最后,只是为了总结,这都是关于我的 getter 和 setter 的错误格式。我们需要检查对象定义。

【讨论】:

  • 这太客气了。我尝试了不同的名称,例如“testString”和“details”,但它们没有显示。我可以试试 Name,因为这对你有用。
  • @EAAL 好的,尝试重新创建表存储以再次测试。或者清理所有内容。
  • 我在另一张桌子上尝试过,但结果相同......我现在注意到一些非常奇怪的东西。我的实体类和 addEntity 类当然是不同的文件。当我将函数名称 setProp1 更改为 setP1 之类的名称时,表不会插入该值,它会被设置为 null。尽管我唯一做的就是更改函数名称。我不知道为什么会这样。
  • @EAAL 我做了进一步的测试,它对我有用。也许更改文件对您无效?
  • @EAAL 也许尝试粘贴我的工作代码进行测试?不要修改您自己的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 2016-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多