【问题标题】:ORACLE: org.hibernate.ObjectNotFoundException: No row with the given identifier existsORACLE:org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行
【发布时间】:2013-08-28 02:42:24
【问题描述】:

我正在为我的应用程序使用 Hibernate(3.0) + Oracle DB (10g)。

我的域对象就像 PluginConfig.java

@Entity
@Table(name = "plugin_config" , schema = "test")
@org.hibernate.annotations.Cache(usage =org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
public class Config {

private static final long serialVersionUID = -1959019321092627830L;

/** This object's id */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected long id;

@OneToOne
@JoinColumn(name = "plugin_id")
private Plugin plugin;

@Column(name = "config_name")
@NaturalId(mutable = true)
private String name;

@Column(name = "config_desc")
private String description;

另一个域对象 Plugin.java

@Entity
@Table(name = "plugin", schema = "test")
@org.hibernate.annotations.Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.READ_WRITE)
public class Plugin implements Serializable {

private static final long serialVersionUID = 5694761325202724778L;

/** This object's id */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected long id;

@Column(name = "plugin_name")
@NaturalId
private String pluginName;
@OneToOne
@JoinColumn(name = "plugin_config_id")
private PluginConfig pluginConfig;

@Column(name = "plugin_desc")
private String description;

每当我尝试通过我的数据库服务方法加载插件时(使用 @Transactional 注释,因此它也会加载它的所有子节点)我收到以下错误

org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:[PluginConfig#53]

plugin_config 表中实际上没有id = 53 的行。 而且没有插件表条目有plugin_config_id=53

那么,hibernate 是从哪里挑选这些值的呢? 我在这里注意到一件事,值“53”实际上是 plugin_config 表中应该被引用的行号。

见下图:

这是我的查询应该寻找的插件配置。但它会尝试使用标识符#53(行号:)而不是#95(主键“id”的值)来搜索它。

我哪里出了问题?

【问题讨论】:

  • 在这里查看图片! [1]:i.stack.imgur.com/hVpob.png
  • 基于plugin_config_id的对应对象是否存在?您如何尝试在plugin_config 表中加载实体?
  • 你是如何生成你的表ID的?
  • @c.s. : 是的!基于 plugin_config_id 的对象存在于标识符 #95 但不存在于 #53(hibernate 正在寻找的)。
  • 我正在通过休眠条件加载插件(父对象)来加载 plugin_config(子对象)。带有条件的方法带有 @Transactional 注释。

标签: java sql oracle hibernate


【解决方案1】:

我不知道这是否是最好的解决方案,但您可以使用@NotFound 注释和IGNORE 命令让Hibernate 丢弃异常并将pluginConfig 设置为null

@OneToOne
@JoinColumn(name = "plugin_config_id")
@NotFound(action = NotFoundAction.IGNORE)
private PluginConfig pluginConfig;

【讨论】:

  • 遗憾的是,这不是我的选择。在这种特殊情况下,我本可以这样做,但我有一个完整的应用程序,我面临同样的问题。我需要一些其他的解决方法。
猜你喜欢
  • 1970-01-01
  • 2015-06-02
  • 2013-10-11
  • 2012-02-17
  • 1970-01-01
  • 2011-02-20
  • 2013-03-29
相关资源
最近更新 更多