我正在使用mongodb的datanucleus。我想保留我自己的类型(扩展java.util.Set)但不能。它抛出异常。我该怎么办?
以下是代码:
我的自定义对象是我想要保留的对象的一部分 - CustomSet.java
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class CustomSet<E> implements Set<E> {
private final Set<E> hashSet = new HashSet<E>();
@Override
public int size() {
return hashSet.size();
}
@Override
public boolean isEmpty() {
return hashSet.isEmpty();
}
@Override
public boolean contains(Object o) {
return hashSet.contains(o);
}
@Override
public Iterator<E> iterator() {
return hashSet.iterator();
}
@Override
public Object[] toArray() {
return hashSet.toArray();
}
@Override
public <T> T[] toArray(T[] a) {
return hashSet.toArray(a);
}
@Override
public boolean add(E e) {
return hashSet.add(e);
}
@Override
public boolean remove(Object o) {
return hashSet.remove(o);
}
@Override
public boolean containsAll(Collection<?> c) {
return hashSet.containsAll(c);
}
@Override
public boolean addAll(Collection<? extends E> c) {
return hashSet.addAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
return hashSet.retainAll(c);
}
@Override
public boolean removeAll(Collection<?> c) {
return hashSet.removeAll(c);
}
@Override
public void clear() {
hashSet.clear();
}
}
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class CustomSet<E> implements Set<E> {
private final Set<E> hashSet = new HashSet<E>();
@Override
public int size() {
return hashSet.size();
}
@Override
public boolean isEmpty() {
return hashSet.isEmpty();
}
@Override
public boolean contains(Object o) {
return hashSet.contains(o);
}
@Override
public Iterator<E> iterator() {
return hashSet.iterator();
}
@Override
public Object[] toArray() {
return hashSet.toArray();
}
@Override
public <T> T[] toArray(T[] a) {
return hashSet.toArray(a);
}
@Override
public boolean add(E e) {
return hashSet.add(e);
}
@Override
public boolean remove(Object o) {
return hashSet.remove(o);
}
@Override
public boolean containsAll(Collection<?> c) {
return hashSet.containsAll(c);
}
@Override
public boolean addAll(Collection<? extends E> c) {
return hashSet.addAll(c);
}
@Override
public boolean retainAll(Collection<?> c) {
return hashSet.retainAll(c);
}
@Override
public boolean removeAll(Collection<?> c) {
return hashSet.removeAll(c);
}
@Override
public void clear() {
hashSet.clear();
}
}
我想要保留的My Object - MyDBObject.java
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable(detachable = "true")
public class MyDBObject {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.UUIDHEX)
private String id;
@Persistent
private String name = null;
@Persistent
private CustomSet<String> set = null;
public MyDBObject(String name, CustomSet<String> set) {
this.name = name;
this.set = set;
}
}
显示问题的测试代码:
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable(detachable = "true")
public class MyDBObject {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.UUIDHEX)
private String id;
@Persistent
private String name = null;
@Persistent
private CustomSet<String> set = null;
public MyDBObject(String name, CustomSet<String> set) {
this.name = name;
this.set = set;
}
}
执行上面的代码后,我收到以下异常:
public class Test {
private final static PersistenceManagerFactory pmFactory = JDOHelper
.getPersistenceManagerFactory(new File("appdb.properties"));
public static void main(String[] args) {
CustomSet<String> set = new CustomSet<>();
set.add("Test-1");
set.add("Test-2");
set.add("Test-3");
saveOrUpdate(pmFactory.getPersistenceManager(), new MyDBObject(
"TestObject", set));
}
public static <T> void saveOrUpdate(PersistenceManager pm, T object) {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(object);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
}
public class Test {
private final static PersistenceManagerFactory pmFactory = JDOHelper
.getPersistenceManagerFactory(new File("appdb.properties"));
public static void main(String[] args) {
CustomSet<String> set = new CustomSet<>();
set.add("Test-1");
set.add("Test-2");
set.add("Test-3");
saveOrUpdate(pmFactory.getPersistenceManager(), new MyDBObject(
"TestObject", set));
}
public static <T> void saveOrUpdate(PersistenceManager pm, T object) {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(object);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
}