[英]spring MVC3 errors Could not instantiate bean class [java.util.List]: Specified class is an interface Occasional
[英]Hibernate Cannot Instantiate Abstract class or interface: java.util.List
我在实体 MyUser 和 Circuit 之间存在父子关系。 当我尝试使用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]
这是两个实体:
我的用户:
@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);
}
}
值得注意的是,当我第一次创建用户时,尚未创建 child(Circuit)。 它可能会在很久以后创建。 此错误背后的原因是什么以及如何解决此问题?
任何帮助表示赞赏。
您正在尝试使用默认值启动一对多关系。 与其创建一个新的 ArrayList,不如尝试使用 null 来初始化它。
这将解决您的问题,因为如果您尝试在没有分配任何电路的情况下保留用户,它将起作用,否则,您将收到该错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.