相关demo项目查看
http://acheron.iteye.com/admin/blogs/402499
常见操作
A.查询操作
public List<Record> listAllRecord() {
PersistenceManager pm = getPersistenceManager();
// Transaction tx= pm.currentTransaction();
try {
// tx.begin();
Query query = pm.newQuery(Record.class);
query.setOrdering("addDate DESC");
//方法1:获得查询结果,得到对象数组
// Collection entries = (Collection) query.execute();
// Object objRecord[]=(Object [])entries.toArray();
//方式2:枚举集合中的entity并作单独处理
// Collection entries = (Collection) query.execute();
// Iterator i = entries.iterator();
// while (i.hasNext()) {
// Record obj = (Record) i.next();
// System.out.println("do sth on the record!");
// }
//方法3:获得list,显式把返回的Collection对象转换为List对象
// Collection entries = (Collection) query.execute();
// List<Record> list = new ArrayList(entries);
//方法4:直接强制转换为List返回
List<Record> list = (List<Record>) query.execute();
// tx.commit();
return list;
} catch (Exception e) {
e.printStackTrace();
// if (tx.isActive()) {
// tx.rollback();
// }
}
query.execute();
return null;
}
public List<Record> listAllRecord(int page) {
PersistenceManager pm = getPersistenceManager();
// Transaction tx= pm.currentTransaction();;
try {
page = (page == 0) ? 1 : page;
Query query = pm.newQuery(Record.class);
query.setOrdering("addDate DESC");
query.setRange(PAGE_SIZE * (page - 1), PAGE_SIZE * page);
return (List<Record>) pm.detachCopyAll((Collection<Record>) query.execute());
} finally {
releasePersistenceManager(pm);
}
}
public int getPage(){
PersistenceManager pm = getPersistenceManager();
Query query = pm.newQuery(Record.class);
query.setOrdering("addDate DESC");
List<Record> list = (List<Record>) query.execute();
return (int)(list.size()/10+1);
}
B.删除操作
public void removeRecordById(Long id) {
PersistenceManager pm = getPersistenceManager();
// Transaction tx= pm.currentTransaction();
try {
// tx.begin();
Extent r = pm.getExtent (Record.class, false);
String filter = "id == "+id;//注意是==
Query query = pm.newQuery(r,filter);
//方式1:迭代Collection获得元素然后处理
// Collection entries = (Collection) query.execute();
// Iterator i = entries.iterator();
// while (i.hasNext()) {
// Record delRecord = (Record) i.next();
// pm.deletePersistent(delRecord);
// }
//方式2:作为list处理
List<Record> list = (List<Record>) query.execute();
// if(!list.isEmpty()){}//isEmpty是首先判断是否为空(null),然后判断size
// if(list.size()!=0){ }
// if(list.iterator().hasNext()){}
if(!list.isEmpty()){
Record delRecord = list.get(0);
pm.deletePersistent(delRecord);
}
// tx.commit();
}catch (Exception e) {
e.printStackTrace();
// if (tx.isActive()) {
// tx.rollback();
// }
}
}
异常处理:
dataNucleus Enhancer (version 1.1.0) : Enhancement of classes
2009-6-1 16:02:25 org.datanucleus.metadata.MetaDataManager loadClasses
严重: Class "com.jun.demos.book.dao.RecordJdoDao" was not found in the CLASSPATH. Please check your specification and your CLASSPATH.
org.datanucleus.exceptions.ClassNotResolvedException: Class "com.jun.demos.book.dao.RecordJdoDao" was not found in the CLASSPATH. Please check your specification and your CLASSPATH.
at org.datanucleus.JDOClassLoaderResolver.classForName(JDOClassLoaderResolver.java:255)
at org.datanucleus.JDOClassLoaderResolver.classForName(JDOClassLoaderResolver.java:420)
at org.datanucleus.metadata.MetaDataManager.loadClasses(MetaDataManager.java:337)
at org.datanucleus.enhancer.DataNucleusEnhancer.getFileMetadataForInput(DataNucleusEnhancer.java:723)
at org.datanucleus.enhancer.DataNucleusEnhancer.enhance(DataNucleusEnhancer.java:525)
at org.datanucleus.enhancer.DataNucleusEnhancer.main(DataNucleusEnhancer.java:1235)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.appengine.tools.enhancer.Enhancer.execute(Enhancer.java:57)
at com.google.appengine.tools.enhancer.Enhance.<init>(Enhance.java:60)
at com.google.appengine.tools.enhancer.Enhance.main(Enhance.java:41)
DataNucleus Enhancer completed with success for 0 classes. Timings : input=78 ms, enhance=0 ms, total=78 ms. Consult the log for full details
DataNucleus Enhancer completed and no classes were enhanced. Consult the log for full details
解决方法:
这个错误是因为spring的JdoSupport依赖于spring-dao.jar ,
也就是需要spring-jdo.jar和spring-dao.jar加到构建目录中。