简体   繁体   中英

Default value for an embedded field in Hibernate. java.sql.SQLException: Field 'url' doesn't have a default value

I have an abstract class A , class B and C are subclasses of A . Class B has an embedded field d . Now when I try to save an instance of B everything is fine. However on creating an instance of C and saving it, I get an error :

java.sql.SQLException: Field 'string' doesn't have a default value.

Eg classes:

public abstract class  A {

}

@Embeddable
public class D {

    private String string;
}

public class B extends A{

    @Embedded
    @NotNull
    private D d;
}

public class C extends A {

}

The error you got is java.sql.Exception telling you that:

`Field 'string' doesn't have a default value.`

From the wording I assume you are using MySQL, and what the error means is, that your string field was not populated at insertion time and declared as not null. You would receive the exact same error if you were to insert a record into that table and leave the string column empty.

What is causing your problem in the end is that you have the same table used for both classes, the string column added for the sake of object b and not populated in case of using c .

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