質問

I am attempting to check for an existing string using a JDO query, in my attempt to prevent the insertion of a duplicate string.

My query to check for an existing string works fine, unless the two strings I am comparing have a comma in the value. If the commas exists, the comparison bombs using "==".

For example, if I query to see if "Architecture" exists, I get the right result (Horrray!). If I attempt to see if "Architecture, Engineering, and Drafting" exists, and it does, the query comes back and says an identical value does not exist (Boo!).

The code I'm using is as follows:

Called from the 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);
}

Persist Operation

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();
    }
}

Query

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 Entity

@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;
    }
}

Any thoughts on a resolution or pinpointing an oversight would be very much appreciated. Cheerio.

役に立ちましたか?

解決

So you're calling industryNameExists("Architecture, Engineering, and Drafting") and trying to match a JDO with industryName exactly equal "Architecture, Engineering, and Drafting"?

Assuming you don't have any typo or space difference the only thing suspect is the getBytes(). Try the following:

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

You can also try variation filters like "this.industryName.equalsIgnoreCase(:industryNameParam)" and "this.industryName.startWith(:industryNameParam)" to troubleshoot.

If it still does not work, try logging the SQL generated for review and compare with a hand-written query that works.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top