[英]Hibernate one to zero or one mapping. Exception: attempted to assign id from null one-to-one property?
I do as @Tony answered in Hibernate one to zero or one mapping . 我就像@Tony在Hibernate中以一到零或一个映射来回答。
I have a class FileMeta
. 我有一个类
FileMeta
。
@Entity
@Inheritance
@DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType = DiscriminatorType.STRING, length = 30)
@DiscriminatorValue("FileMeta")
public class FileMeta {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected long id;
...SOME ATTRIBUTES...
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "FK_GARBAGE")
@NotFound(action = NotFoundAction.IGNORE)
protected Garbage garbage;
@Enumerated(EnumType.ORDINAL)
private FileT type;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "FK_SHARE")
@NotFound(action = NotFoundAction.IGNORE)
private Share share;
...METHODS...
}
And a class Share
. 和一班
Share
。
@Entity
public class Share {
@Id
@GeneratedValue(generator = "shareForeignGenerator")
@GenericGenerator(
name = "shareForeignGenerator",
strategy = "foreign",
parameters = @Parameter(name = "property", value = "fileMeta")
)
private Long id;
...SOME ATTRIBUTES...
@OneToOne(mappedBy = "share")
@PrimaryKeyJoinColumn
@NotFound(action = NotFoundAction.IGNORE)
private FileMeta fileMeta;
...METHODS...
}
And when I tried to fill my FileMeta
with Share
: 当我尝试用
Share
填充FileMeta
时:
Share share = new Share();
share.setFileMeta(fileMeta);
fileMeta.setShare(share);
fileMetaRepository.save(fileMeta);
I received exception: attempted to assign id from null one-to-one property
我收到异常:
attempted to assign id from null one-to-one property
I followed into Hibernate
. 我跟随进入
Hibernate
。 And noticed that, in generator
method, the associatedObject
is not my given Share
object at all, but a new Share
object instantiated by a EventSource
并注意到,在
generator
方法中, associatedObject
根本不是我给定的Share
对象,而是由EventSource
实例化的新Share
对象
In DefaultMergeEventListener.java
, 在
DefaultMergeEventListener.java
,
if ( copyCache.containsKey( entity ) ) {
persister.setIdentifier( copyCache.get( entity ), id, source );
}
else {
( (MergeContext) copyCache ).put( entity, source.instantiate( persister, id ), true ); //before cascade!
}
The copyCache
sensed that entity
(ie my given Share
object) is not in copyCache
, and instantiated a new Share
object, which doesn't have a FileMeta
reference obviously. copyCache
感知到该entity
(即我给定的Share
对象)不在copyCache
,并实例化了一个新的Share
对象,该对象显然没有FileMeta
引用。
I've completely confused. 我完全感到困惑。
The solution was mentioned in the comments to the question. 该问题的评论中提到了解决方案。 It's given below for convenience.
为方便起见,以下给出。
You can use @OneToOne(optional = true)
or simply @OneToOne
since default value of optional
is true
. 您可以使用
@OneToOne(optional = true)
或仅使用@OneToOne
因为optional
默认值为true
。
If you want to enforce 1 to 1 relationship, specify @OneToOne(optional = false)
. 如果要强制
@OneToOne(optional = false)
关系,请指定@OneToOne(optional = false)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.