【问题标题】:Comparing strings in a JDO query fails when value contains a "Comma"当值包含“逗号”时,比较 JDO 查询中的字符串失败
【发布时间】:2013-09-07 01:17:51
【问题描述】:

我正在尝试使用 JDO 查询检查现有字符串,以防止插入重复的字符串。

我检查现有字符串的查询工作正常,除非我比较的两个字符串的值中有逗号。如果逗号存在,则使用“==”进行比较。

例如,如果我查询“架构”是否存在,我会得到正确的结果(Horray!)。 如果我尝试查看“架构、工程和绘图”是否存在,并且确实存在,则查询会返回并说不存在相同的值(嘘!)。

我使用的代码如下:

从 RPC 调用

public void addCommas()
{
    final Industry e = new Industry();
    e.setIndustryName("Architecture, Engineering, and Drafting");
    persist(e);
}

public void addNoCommas()
{
    final Industry e = new Industry();
    e.setIndustryName("Architecture");
    persist(e);
}

坚持经营

private void persist(Industry industry)
{
    if (industryNameExists(industry.getIndustryName()))
    {
        return;
    }
    final PersistenceManager pm = PMF.get().getPersistenceManager();
    pm.currentTransaction().begin();
    try
    {
        pm.makePersistent(industry);
        pm.flush();
        pm.currentTransaction().commit();
    } catch (final Exception ex)
    {
        throw new RuntimeException(ex);
    } finally
    {
        if (pm.currentTransaction().isActive())
        {
            pm.currentTransaction().rollback();
        }
    pm.close();
    }
}

查询

public static boolean industryNameExists(final String industryName)
{
    final PersistenceManager pm = PMF.get().getPersistenceManager();
    Query q = null;
    q = pm.newQuery(Industry.class);
    q.setFilter("industryName == industryNameParam");
    q.declareParameters(String.class.getName() + " industryNameParam");
    final List<Industry> industry = (List<Industry>) q.execute(industryName.getBytes());
    boolean exists = !industry.isEmpty();
    if (q != null)
    {
        q.closeAll();
    }
    pm.close();
    return exists;
}

JDO 实体

@PersistenceCapable(detachable = "true")
public class Industry implements StoreCallback
{
    @NotNull(message = "Industry Name is required.")
    private String          industryName;
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @PrimaryKey
    private Key             key;

    public Industry()
    {
        super();
    }

    public Key getIndustryKey()
    {
        return key;
    }

    public String getIndustryName()
    {
        return industryName;
    }

    @Override
    public void jdoPreStore()
    {
        if (industryName != null)
        {
            industryName = industryName.trim();
        }
    }
    public void setIndustryName(final String industryName)
    {
        this.industryName = industryName;
    }
}

任何关于解决方案或指出疏忽的想法都将不胜感激。 干杯。

【问题讨论】:

  • 您的查询有点奇怪。你能发布你的 JDO 类和映射吗?
  • 我已经添加了有问题的 JDO 文件。为简洁起见,我确实删除了一些 getter/setter 和属性。我愿意接受有关如何重写查询的建议。
  • 我仍然对你的逗号分隔字符串感到困惑。是行业名称参数的输入吗?还是 Industry.industryName 的值?如果前者是您将逗号分隔的字符串作为输入传递给单个方法调用吗?您还可以发布对行业名称存在的调用吗?这会有所帮助。
  • 在行业实体的行业名称上设置逗号分隔字符串。我在上面的示例中添加了持久化操作和调用。

标签: google-app-engine jdo


【解决方案1】:

所以您调用的是行业名称Exists("Architecture, Engineering, and Drafting") 并试图将 JDO 与行业名称完全匹配为“Architecture, Engineering, and Drafting”?

假设您没有任何拼写错误或空格差异,唯一值得怀疑的是 getBytes()。请尝试以下操作:

Query q = pm.newQuery(Industry.class, "this.industryName == :industryNameParam");
List<Industry> industry = (List<Industry>) q.execute(industryName);

您还可以尝试“this.industryName.equalsIgnoreCase(:industryNameParam)”和“this.industryName.startWith(:industryNameParam)”等变体过滤器进行故障排除。

如果仍然不起作用,请尝试记录生成的 SQL 以供审核,并与有效的手写查询进行比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 2020-06-09
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    相关资源
    最近更新 更多