简体   繁体   English

休眠一对零或一个映射。 例外:尝试从空的一对一属性分配ID?

[英]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.

相关问题 Hibernate 尝试从 null 一对一属性分配 id - Hibernate attempted to assign id from null one-to-one property Hibernate merge org.hibernate.id.IdentifierGenerationException:尝试从空的一对一属性分配ID - Hibernate merge org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property org.hibernate.id.IdentifierGenerationException:“试图从空一对一属性分配 id” - org.hibernate.id.IdentifierGenerationException: "attempted to assign id from null one-to-one property" Hibernate 错误:试图从 null 一对一属性分配 id。 为什么? - Hibernate Error : attempted to assign id from null one-to-one property. Why? Hibernate OneToOne 尝试从 null 一对一属性分配 id - Hibernate OneToOne attempted to assign id from null one-to-one property org.hibernate.id.IdentifierGenerationException:试图从空一对一属性(hibernate+postgres)分配id - org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property (hibernate+postgres) org.hibernate.id.IdentifierGenerationException:尝试从空的一对一属性(使用session.merge)分配ID - org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property (with session.merge) 在Spring Boot中尝试从空的一对一属性分配ID - Attempted to assign id from null one-to-one property in spring boot Java JPA-将多对多结构保存到DB-错误:尝试从空的一对一属性分配ID - Java JPA - save many-to-many structure to DB - error: attempted to assign id from null one-to-one property 错误:试图从空一对一属性 [com.dbtest.springboot.db_model.Family.user] 分配 id - Error: attempted to assign id from null one-to-one property [com.dbtest.springboot.db_model.Family.user]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM