在Hibernate中保存多个oneToMany实体关系时引发错误

时间:2019-01-18 04:58:12

标签: java hibernate hibernate-mapping

我有一个实体类,其中有两个具有一对多关系的Entity类。并且有一个根父实体,其ID变量正在被所有实体扩展。当我尝试保存MessagingEnablerCfg实体而不初始化它的成员变量(messagingProfiles和messagementSessions)时。它正在保存。但如果我将尝试初始化这些设置变量。保存或更新操作时抛出以下错误:

原因:java.sql.SQLException:字段'messagingProfiles_id'没有默认值

注意::如果MessagingEnablerCfg实体中只有一个变量(messagingProfiles或essagingSessions),那么它将可以很好地保存或更新。 我觉得很奇怪,但现在对我来说是个大麻烦。

请找到下面的Entity类

@Entity
//@Indexed
@Proxy(lazy=false)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class MessagingConfigEntity{

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    private String id;
    //@Override
    public String getId() {
        return id;
    }
}

另一个实体类是:

@Entity
//@Indexed
@Proxy(lazy=false)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class MessagingEnablerCfg extends MessagingConfigEntity {        
    private String serviceName; 
    @OneToMany(cascade = { CascadeType.ALL}, orphanRemoval = true, fetch = FetchType.EAGER)
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Set<MessagingProfileCfg> messagingProfiles = new HashSet<>();

    //@OneToMany(cascade = { CascadeType.ALL}, orphanRemoval = true, fetch = FetchType.EAGER)
    @OneToMany(cascade = { CascadeType.ALL}, orphanRemoval = true, fetch = FetchType.EAGER)
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private Set<MessagingSessionCfg> messagingSessions  = new HashSet<>();

    public MessagingEnablerCfg(){

    }
    public MessagingEnablerCfg(String service){
        this.serviceName = service;
    }
    public String getServiceName() {
        return serviceName;
    }
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    public Set<MessagingSessionCfg> getMessagingSessions() {
        return messagingSessions;
    }
    public void setMessagingSessions(Set<MessagingSessionCfg> messagingSessions) 
   {
        this.messagingSessions = messagingSessions;
    }
    public Set<MessagingProfileCfg> getMessagingProfiles() {
        return messagingProfiles;
    }
    public void setMessagingProfiles(Set<MessagingProfileCfg> messagingProfiles) 
    {
        this.messagingProfiles = messagingProfiles;
    }
    public void addSession(MessagingSessionCfg session){
        messagingSessions.add(session);

    }
    public void addProfile(MessagingProfileCfg profile){
        messagingProfiles.add(profile); 
    }
}

另一个儿童班是:

@Entity
//@Indexed
@Proxy(lazy = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class MessagingSessionCfg extends MessagingConfigEntity{

    private String sessionName;
    private String provider;

    public MessagingSessionCfg(){

    }
    public MessagingSessionCfg(String name,String provider){
        this.sessionName = name;
        this.provider = provider;
    }

    public String getSessionName() {
        return sessionName;
    }
    public void setSessionName(String sessionName) {
        this.sessionName = sessionName;
    }
    public String getProvider() {
        return provider;
    }
    public void setProvider(String provider) {
        this.provider = provider;
    }
    public String toString(){
        return "sessionName:"+sessionName+"  provider:"+provider;
    }
}

最后一个子实体是:

@Entity
//@Indexed
@Proxy(lazy=false)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class MessagingProfileCfg extends MessagingConfigEntity {

    private String profileId;
    private String sourceAddress;

    public MessagingProfileCfg(){

    }
    public MessagingProfileCfg(String profileId, String sourceAddress){
        this.profileId = profileId;
        this.sourceAddress = sourceAddress;
    }
    public String getProfileId() {
        return profileId;
    }
    public void setProfileId(String profileId) {
        this.profileId = profileId;
    }
    public String getSourceAddress() {
        return sourceAddress;
    }
    public void setSourceAddress(String sourceAddress) {
        this.sourceAddress = sourceAddress;
    }   
}

0 个答案:

没有答案