【问题标题】:Hibernate : Update Complex entity throwing NullPointerException without any clue休眠:更新复杂实体抛出 NullPointerException 没有任何线索
【发布时间】:2021-05-16 22:40:43
【问题描述】:

美好的一天!

我正在 Spring Boot 中开发一个使用 Hibernate 与 My-SQL DB 对话的应用程序。

我在这里面临的问题是

我有一个与其他 4 个子实体有连接的父实体。在父实体中与子实体的所有连接都是一对多的,并且父实体是关系的所有者。

当我在 parentEntity 上保存数据时,数据会从父级保存到所有子级。但是当我尝试通过传递父实体 ID 来进行更新时,我遇到了错误。

任何帮助将不胜感激。

提前致谢。

例外:

java.lang.NullPointerException: null
at org.hibernate.type.descriptor.java.AbstractTypeDescriptor.extractHashCode(AbstractTypeDescriptor.java:78)
    at org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:200)
    at org.hibernate.type.AbstractStandardBasicType.getHashCode(AbstractStandardBasicType.java:205)
    at org.hibernate.type.EntityType.getHashCode(EntityType.java:383)
    at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:249)
    at org.hibernate.engine.spi.EntityKey.generateHashCode(EntityKey.java:61)
    at org.hibernate.engine.spi.EntityKey.<init>(EntityKey.java:54)

实体:

@Entity
@Table(name = "theater")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Theater implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    @Column(name = "theater_id")
    private Long id;

    @Column(name = "theater_name", nullable = false, unique = true)
    private String theaterName;

    @ManyToOne(fetch = FetchType.LAZY)
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    @JoinColumn(name = "theater_type", nullable = false)
    private TheaterType theaterType;

    @ManyToOne(fetch = FetchType.EAGER)
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    @JoinColumn(name = "location_id", nullable = false)
    private Location location;

    @Column(name = "no_of_screens", nullable = false)
    private Long noOfScreens;

    @OneToMany(
            mappedBy = "theater", fetch = FetchType.EAGER, orphanRemoval = true)
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    private Set<Screen> screens;

    @OneToMany(
            mappedBy = "theater", orphanRemoval = true)
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    private List<TheaterScreenScreenTypeSoundType> theaterScreenScreenTypeSoundTypes;

    @OneToMany(
            mappedBy = "theater", orphanRemoval = true)
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    private List<TheaterScreenSeatCategory> theaterScreenSeatCategories;


    @Column(name = "theater_status", nullable = true)
    private String status;
    @Column(name = "theater_address", nullable = true)
    private String address;
    @Column(name = "theater_official_site_url", nullable = true)
    private String officialSiteUrl;
    @Column(name = "theater_contact_email", nullable = true)
    private String contactEmail;
    @Column(name = "theater_contact_phone", nullable = true)
    private String contactPhone;
    @Column(name = "theater_gps_link", nullable = true)
    private String gpsLink;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || !(o instanceof Theater))
            return false;

        Theater other = (Theater) o;

        if (id == other.getId()) return true;
        if (id == null) return false;

        // equivalence by id
        return id.equals(other.getId());
    }

    @Override
    public int hashCode() {
        if (id != null) {
            return id.hashCode();
        } else {
            return super.hashCode();
        }
    }

}

【问题讨论】:

  • 你能发布更新的代码吗?
  • 你找到决定了吗?

标签: java mysql spring-boot hibernate spring-data-jpa


【解决方案1】:

您的表中似乎有一行theater_idnull,这对于主键是非法的。

【讨论】:

  • Theater_id 永远不会为空,下面代码中的实体使用复合键作为主键。@IdClass 或@EmbeddedId 处理它。只有当我们单独处理下面的(子)实体时才会发生更新,而不是来自父母。父类有子实体时的休眠限制,其中复合键是主键注意:复合键再次是少数实体的组合,而不是字符串,将其长为自定义对象 private List TheaterScreenScreenTypeSoundTypes;私人列表 TheaterScreenSeatCategories;
  • 我不知道你在写什么。您问题中的示例没有复合ID。您看到的 NPE 是因为您在表格某行的 theater_id 列中有一个 null。如果您还有其他问题,请提出新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 2013-07-24
  • 2018-10-24
  • 1970-01-01
  • 2016-12-20
相关资源
最近更新 更多