hibernate jpa entitymanager不提供对象写入数据库 [英] hibernate jpa entitymanager commit not writing objects to the database

查看:118
本文介绍了hibernate jpa entitymanager不提供对象写入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用hibernate JPA(没有Spring),它运行良好,但是我遇到了一个问题,在过去的3天里我一直困扰着我。

I'm using hibernate JPA (without Spring) and it's working well, but I have come across a problem which has stumped me for the last 3 days.

我写了一些通用的DAO类,并使用它们来坚持我的对象。它们都工作正常,除了一类没有被持久保存的对象。没有例外被抛出。我试过在hibernate代码中调试,发现实体没有被保存的原因是在 org.hibernate.event.def.DefaultFlushListener onFlush()方法中, source.getPersistenceContext()。getEntityEntries()。size()== 0 因此不执行刷新。但我无法弄清楚为什么会出现这种情况。

I have written some generic DAO classes and am using them to persist my objects. They all work fine, except for one class of object which is not being persisted. No exceptions are thrown. I've tried debugging inside the hibernate code and found that the reason the entity is not being persisted is that in the org.hibernate.event.def.DefaultFlushListener onFlush() method, source.getPersistenceContext().getEntityEntries().size() == 0 so no flushing is performed. But I can't work out why that would be the case.

有问题的类看起来像这样:

The classes in question look like this:

    @Entity 
@Table(name="er_batch_runs")
public class BatchRun implements Serializable, Comparable<BatchRun>, BatchBean {

private Long runId; 
private String hostname;
.... more field here

@Override
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="runseq")
@SequenceGenerator(name="runseq", sequenceName="er_batch_runs_seq", allocationSize=1    /*, initialValue = 10*/)
@Column(name="batch_run_id")
public Long getId() {
    return runId;
}

public void setId(long runId) {
    this.runId = runId;
}

@Column(name="hostname")
public String getHostname() {
    return hostname;
}

public void setHostname(String hostname) {
    this.hostname = hostname;
}

非常简单的hibernate JPA stuff。

pretty straightforward hibernate JPA stuff.

以下是另一个类:

Here's another class:

@Entity
@Table(name="er_batch_txns")
public class BatchTxn implements Serializable, Comparable<BatchTxn>, BatchBean {

private long id;
    .......... more fields

@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="batchtxnseq")
@SequenceGenerator(name="batchtxnseq", sequenceName="ER_BATCH_TXNS_SEQ", allocationSize=1/*00, initialValue = 10*/)
@Override
@Id
@Column(name="BATCH_TXN_ID")
public Long getId() {
    return id;
}

BatchBean接口允许我使用这样的通用DAO: p>

the BatchBean interface is what allows me to use generic DAOs like this:

    public Long create(BatchBean newInstance) {
    getOpenEntityManager().persist(newInstance);
    logger.debug("hopefully created {} with id {}",newInstance.getTypeName(),newInstance.getId());
    return newInstance.getId(); 
}

正在手动处理事务。我已经将flush类型设置为COMMIT(即提交时刷新),并且当我完成了坚持时,我做了一个提交。在持续之后,BatchTxn对象被分配了序列中的主键。当我调试hibernate时,我可以看到getPersistenceContext()。getEntityEntries()返回一个空的Map。

Transactions are being handled manually. I've set the flush type to COMMIT (ie flush on commit) and when I've completed the persist, I do a commit. After the persist, then BatchTxn object has been assigned a primary key from the sequence. When I debug hibernate I can see that getPersistenceContext().getEntityEntries() returns an empty Map.

所以问题是为什么BatchTxn没有被提交持久化,当BatchRuns和其他5个实现BatchBean的类都是?

so the question is why the BatchTxn is not being persisted by the commit, when the BatchRuns, and 5 other classes which implement BatchBean, are?

我使用hibernate 3.6.0 Final

I'm using hibernate 3.6.0 Final

推荐答案

在你的代码中怀疑这是在BatchTxn类中:

The only thing I saw that is suspected in your code is this in the BatchTxn class:


private long id;

private long id;

这将自动设置为零。也许你应该使用Long(用大写字母)?

This will be set automatically to zero. Maybe you should use Long (with a capital letter)?

这篇关于hibernate jpa entitymanager不提供对象写入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆