Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate无法实例化抽象类或接口:java.util.List_Java_Spring_Hibernate - Fatal编程技术网

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

Hibernate无法实例化抽象类或接口:java.util.List,java,spring,hibernate,Java,Spring,Hibernate,我在实体MyUser和回路之间存在父子关系。 当我尝试使用myUserRepository.save(MyUser)保存MyUser时,出现以下错误: org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: : java.util.List at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiat

我在实体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);
    }
}
@实体
@资料
公共类MyUser{
@身份证
@GeneratedValue(策略=GenerationType.SEQUENCE)
私人长id;
@不空白
私有字符串名;
@NotNull
私有字符串lastName;
@NotNull
@电子邮件
@列(唯一=真)
私人字符串电子邮件;
@不空白
私有字符串密码;
私有字符串chargeBeeCustomerID;
私有字符串company=null;
@枚举(EnumType.STRING)
私人用途USAGEUSION=null;
@枚举(EnumType.STRING)
私有CountryCode CountryCode=null;
private Instant createdAt=Instant.now();
@嵌入
private MyUserDetails details=new MyUserDetails();
私人双贷方余额=0;
@OneToMany(mappedBy=“myUser”,orphanRemoving=true,cascade=CascadeType.ALL,fetch=FetchType.LAZY)
private List circuitList=new ArrayList();
公共电路(电路){
此.circuitList.add(电路);
circuit.setMyUser(本);
}
公共无效清除电路(电路){
此.circuitList.remove(电路);
circuit.setMyUser(null);
}
@凌驾
公共字符串toString(){
回复邮件;
}
}
电路:

@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);
    }
}
@实体
@资料
公共级电路{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
@不空白
私有字符串电路ID;
@不空白
私有字符串名称;
@NotNull
@枚举(EnumType.STRING)
私人密码;
@枚举(EnumType.STRING)
私人国家代码国家代码;
@NotNull
启用私有布尔值;
私有布尔hasonlypaclaccess;
@元素集合
私人名单ipACL;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“fk\u我的用户”)
私有MyUser-MyUser;
公共电路(){
circuitID=generateCircuitID();
名称=电路ID;
启用=真;
hasOnlyIPAclAccess=true;
ipACL=新的ArrayList();
}
私有静态字符串generateCircuitID(){
返回RandomStringUtils.randomstringalphameric(15);
}
@凌驾
公共int hashCode(){
返回此.circuitID.hashCode();
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
如果(obj==null | | obj.getClass()!=this.getClass())
返回false;
电路=(电路)obj;
返回circuit.getCircuitID().equals(this.circuitID);
}
}
值得注意的是,当我第一次创建用户时,子(回路)尚未创建。以后可能会创建很多。 这一错误背后的原因是什么?如何解决


非常感谢您的帮助。

您正在尝试使用默认值启动一对多关系。不要创建新的ArrayList,而是尝试使用null启动它


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

是的,这解决了问题。但我从这里学到了这种风格:作者使用空列表来实例化。为什么它在我的情况下不起作用?我看不到你的全部代码,但我认为这是正在发生的事情。在保留任何回路之前,您不会将其添加到用户的回路列表中。因此,由于您有一个空列表,它导致了该问题。当然,如果你有一个空值,它不会引起任何问题。