简体   繁体   中英

Hibernate Cannot Instantiate Abstract class or interface: java.util.List

I have a parent-child relationship between entities MyUser and Circuit. When I try to save MyUser using myUserRepository.save(myUser) , I get the following error:

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]

Here are both the entities:

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;
    }
}

Circuit:

@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);
    }
}

Its notable then when I am first creating the user, the child(Circuit) is not yet created. It may be created a lot later. What is the reason behind this error and how to resolve this?

Any help is appreciated.

You are trying to initiate a one to many relation with a default value. Instead of creating a new ArrayList, try initiating it using null instead.

This will solve your issue, since if you try to persist a User without any circuit assigned, it will work, otherwise, you will get that error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM