Hibernate无法实例化Abstract类或接口:java.util.List

时间:2018-10-20 17:51:50

标签: java spring hibernate

我在实体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)。它可能会在以后创建。 该错误的原因是什么?如何解决该问题?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您正在尝试使用默认值启动一对多关系。与其创建一个新的ArrayList,不如尝试使用null来初始化它。

这将解决您的问题,因为如果您尝试在没有分配任何电路的情况下保留用户,则它将起作用,否则,您将得到该错误。