【问题标题】:Getting @Id of lazy-loaded @ManyToOne entity allways returns null获取延迟加载的 @ManyToOne 实体的 @Id 总是返回 null
【发布时间】:2015-01-29 11:47:39
【问题描述】:

我正在使用 Hibernate 4.3.8.Final 并且在检索延迟获取属性的 @Id 属性时遇到问题:对于调用 aidConfiguration.getChipApplication().getId() 的附加类,总是返回 null。其他属性,例如。 aidConfiguration.getChipApplication().getVersion() 正确返回 DB 中的值。如果chipApplication 没有延迟加载(参见代码中的注释),则aidConfiguration.getChipApplication().getId() 返回正确的非空值。

我哪里错了?

顺便说一句,我需要它变得懒惰。

基础实体:

@MappedSuperclass
public class BaseEntity implements Serializable {

    @Id
    @Column(name = "ID", unique = true)
    @Size(min = 1, max = 255)
    private String id;

    @PrePersist
    public final void generateUuid() {
        if (this.getId() == null) {
            this.setId(UUID.randomUUID().toString());
        }
    }

    public final String getId() {
        return id;
    }

    public final void setId(final String id) {
        this.id = id;
    }
}

辅助配置:

@Entity
@Audited
public class AidConfiguration extends BaseEntity {

    @Column
    @NotBlank
    private String name;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY) // if it is EAGER (defaut) then then aidConfiguration.getChipApplication().getId() returns correctly non-null value
    private ChipApplication chipApplication;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "aidConfiguration", cascade = CascadeType.ALL) // cascade for auto-saving and deleting items
    private List<AidConfigurationItem> aidConfigurationItems;

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public ChipApplication getChipApplication() {
        return chipApplication;
    }

    public void setChipApplication(final ChipApplication chipApplication) {
        this.chipApplication = chipApplication;
    }

    public List<AidConfigurationItem> getAidConfigurationItems() {
        return aidConfigurationItems;
    }

    public void setAidConfigurationItems(final List<AidConfigurationItem> aidConfigurationItems) {
        this.aidConfigurationItems = aidConfigurationItems;
    }
}

芯片应用:

@Entity
@Audited
public class ChipApplication extends BaseEntity {

    @Column
    @NotBlank(message = "Aid can not be empty")
    private String aid;

    @Column
    @NotBlank(message = "Product can not be empty")
    private String product;

    @Column
    @NotBlank(message = "Version can not be empty")
    private String version;

    @NotNull(message = "Network is mandatory")
    @ManyToOne(fetch = FetchType.LAZY)
    private Network network;

    @ManyToMany(fetch =  FetchType.EAGER)
    private List<AidTag> aidTags;

    public String getAid() {
        return aid;
    }

    public void setAid(final String aid) {
        this.aid = aid;
    }

    public String getProduct() {
        return product;
    }

    public void setProduct(final String product) {
        this.product = product;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(final String version) {
        this.version = version;
    }

    public Network getNetwork() {
        return network;
    }

    public void setNetwork(final Network network) {
        this.network = network;
    }

    public List<AidTag> getAidTags() {
        return aidTags;
    }

    public void setAidTags(final List<AidTag> aidTags) {
        this.aidTags = aidTags;
    }
}

【问题讨论】:

    标签: java hibernate jpa lazy-loading


    【解决方案1】:

    有点晚了,但问题 HH-9588 仍未解决,我也遇到了同样的问题(不过是 XML 映射而不是注释)。

    当绑定是惰性的时,无法从 getter 获取 id。在 Eager 或 fetch 加入时得到它。

    通过去掉 getId() 访问器上的“final”修饰符来修复它。 (最后是试图保护在超类中为所有实体定义主键/标识符的方式)

    之前:

    public abstract class Foo  {
    
    Long id;
    
    public final Long getId() {
        return id;
    }
    
    protected final void setId( Long id ){
        this.id = id;
    }
    ...
    

    之后:

    public abstract class Foo {
    
    Long id;
    
    // No more final 
    public Long getId() {
        return id;
    }
    
    // No more final 
    protected void setId( Long id ){
        this.id = id;
    }
    ...
    

    现在,我也可以使用惰性绑定来获取 Id。

    在我看来,这个“final”修饰符不允许 Hibernate 按预期代理这个访问器。其他访问器不是“最终的”,可以从代理访问它们的值。

    所以,我想知道 HH-9588 是否真的是一个错误或对休眠方式的误解?

    【讨论】:

    • 我也有同样的问题。你能分享代码吗,你曾经解决过这个问题。提前致谢。
    【解决方案2】:

    这似乎是一个错误,如果你没有错过任何东西。我会在 Hibernate 的错误跟踪系统上报告它。如果您之后能通过错误链接更新此答案,那就太好了。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    相关资源
    最近更新 更多