我正在用条目填充数据库,如果它们已经存在,则应该替换它们。为此,首先我检查该条目是否已经存在于一个 find 中(它被包装在一个只读事务中),然后如果返回值为 null,我将保存新创建的条目(包装在一个事务中),如果返回值是现有条目,我首先将其删除(包装在事务中),然后添加新条目。除了这些可以在单个事务中完成的许多事务之外,我想知道如果条目已经存在,为什么会出现异常。我检查了刷新模式并将其设置为自动,因此它应该在事务结束时刷新。这是异常的堆栈跟踪:
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.engine.internal.NaturalIdXrefDelegate.validateNaturalId(NaturalIdXrefDelegate.java:175)
at org.hibernate.engine.internal.NaturalIdXrefDelegate.cacheNaturalIdCrossReference(NaturalIdXrefDelegate.java:85)
at org.hibernate.engine.internal.StatefulPersistenceContext$1.cacheNaturalIdCrossReferenceFromLoad(StatefulPersistenceContext.java:1817)
at org.hibernate.engine.internal.StatefulPersistenceContext.getNaturalIdSnapshot(StatefulPersistenceContext.java:340)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkNaturalId(DefaultFlushEntityEventListener.java:110)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:199)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:156)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:225)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:402)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:480)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:392)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy31.save(Unknown Source)
at controller.FileParserCore.update(FileParserCore.java:39)
at controller.Core.runCore(Core.java:122)
at controller.Core.staticRunCore(Core.java:74)
at controller.Core.main(Core.java:33)
我正在使用 Hibernate 和 Spring。
如果条目已经存在,这是失败的代码位:
Entry entry = getNewEntry();
if (entryDAO.find(entry.getName()) != null) {
entryDAO.remove(entry.getName());
}
entryDAO.save(entry);
这是对应的 DAO:
@Repository
public class EntryDAO extends GenericDAO<Entry> implements IEntryDAO {
private static final Logger log = LoggerFactory.getLogger(EntryDAO.class);
@Override
@Transactional
public void save(Entry entry) {
makePersistent(entry);
log.info("Saved: {}", entry);
}
@Override
@Transactional
public void remove(String name) {
Entry entry = find(name);
if (entry != null) {
makeTransient(entry);
log.info("Removed: {}", entry);
} else {
log.warn("Could not remove: {}, entry not found", name);
}
}
/**
* find an entry by its name and return it
*/
@Override
@Transactional(readOnly = true)
public Entry find(String name) {
return (Entry) createCriteria(Restrictions.eq("name",name)).uniqueResult();
}
}
这是入口域对象:
@Entity
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@EqualsAndHashCode(callSuper = false, of = { "name" })
public class Entry extends DomainObject implements Serializable {
@NaturalId
@NotEmpty
@Length(max = 16)
@Index(name = "ix_name")
private String name;
}
这是直到异常的输出:
信息 2013-03-01 10:22:17,899 主要 - 已删除:someEntry 信息 2013-03-01 10:22:18,117 主要 - 已保存:someEntry
当我检查数据库时,旧条目被删除,但新条目没有保存。另外,旧条目和新条目的ID不同,只是名称相同。从堆栈跟踪来看,它似乎与 naturalID 有关,可能是因为刷新没有完成删除旧条目,新条目的名称与此冲突?但是当它进入 save() 方法时,旧条目不应该早就消失了吗?
更新:我还检查了将所有内容放在一个事务中是否可行,但是当我尝试删除一个条目并立即添加一个具有相同 NaturalID(他们的名字)的条目时,它当然开始哭泣。所以我想:让我们在两者之间放置一个 getCurrentSession().flush() 。结果:与上述相同的异常。
更新:这是 DomainObject 类:
@MappedSuperclass
@EqualsAndHashCode
public abstract class DomainObject implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
public Long getId() {
return id;
}
@Override
public abstract String toString();
}