ORACLE:org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行

时间:2013-08-25 14:34:16

标签: java sql oracle hibernate

我正在为我的应用程序使用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表中应该被引用的行号。

见下图:

enter image description here

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

我可以在哪里出错?

1 个答案:

答案 0 :(得分:1)

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

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