我在实体MyUser和电路之间有父子关系。当我尝试使用myUserRepository.save( MyUser )保存myUser时,我得到以下错误:
org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: : java.util.List
at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:79) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.tuple.component.AbstractComponentTuplizer.instantiate(AbstractComponentTuplizer.java:84) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.type.ComponentType.instantiate(ComponentType.java:580) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.type.ComponentType.deepCopy(ComponentType.java:500) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.type.ComponentType.deepCopy(ComponentType.java:497) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.type.TypeHelper.deepCopy(TypeHelper.java:54) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
下面是两个实体:
MyUser:
@Entity
@Data
public class MyUser {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
@NotBlank
private String firstName;
@NotNull
private String lastName;
@NotNull
@Email
@Column(unique = true)
private String email;
@NotBlank
private String password;
private String chargeBeeCustomerID;
private String company = null;
@Enumerated(EnumType.STRING)
private UsagePurpose usagePurpose = null;
@Enumerated(EnumType.STRING)
private CountryCode countryCode = null;
private Instant createdAt = Instant.now();
@Embedded
private MyUserDetails details = new MyUserDetails();
private double creditBalance = 0;
@OneToMany(mappedBy = "myUser", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Circuit> circuitList = new ArrayList<>();
public void addCircuit(Circuit circuit) {
this.circuitList.add(circuit);
circuit.setMyUser(this);
}
public void removeCircuit(Circuit circuit) {
this.circuitList.remove(circuit);
circuit.setMyUser(null);
}
@Override
public String toString() {
return email;
}
}
电路:
@Entity
@Data
public class Circuit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NotBlank
private String circuitID;
@NotBlank
private String name;
@NotNull
@Enumerated(EnumType.STRING)
private ContinentCode continentCode;
@Enumerated(EnumType.STRING)
private CountryCode countryCode;
@NotNull
private boolean enabled;
private boolean hasOnlyIPAclAccess;
@ElementCollection
private List<String> ipACL;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_my_user")
private MyUser myUser;
public Circuit() {
circuitID = generateCircuitID();
name = circuitID;
enabled = true;
hasOnlyIPAclAccess = true;
ipACL = new ArrayList<>();
}
private static String generateCircuitID() {
return RandomStringUtils.randomAlphanumeric(15);
}
@Override
public int hashCode() {
return this.circuitID.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || obj.getClass() != this.getClass())
return false;
Circuit circuit = (Circuit) obj;
return circuit.getCircuitID().equals(this.circuitID);
}
}
值得注意的是,当我第一次创建用户时,子级(Circuit)还没有创建。它可能会在很长一段时间后创建。这个错误背后的原因是什么?如何解决这个问题?
任何帮助都是非常感谢的。
发布于 2018-10-21 02:15:14
您正在尝试使用默认值启动一对多关系。不是创建新的ArrayList,而是尝试使用null初始化它。
这将解决您的问题,因为如果您尝试在没有分配任何电路的情况下持久化用户,它将工作,否则,您将得到该错误。
https://stackoverflow.com/questions/52908502
复制相似问题