我正在尝试使用 JDO 查询检查现有字符串,以防止插入重复的字符串。
我检查现有字符串的查询工作正常,除非我正在比较的两个字符串的值中有逗号。如果逗号存在,则使用“==”进行比较。
例如,如果我查询“Architecture”是否存在,我会得到正确的结果(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;
}
}
任何关于决议或查明疏忽的想法将不胜感激。干杯。