【发布时间】: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