简体   繁体   中英

field ManyToOne doesn't have default value HIBERNATE

I have created a java class called books, one of the fields ir referencing Many to one an Id of another class called Course.

@javax.persistence.Id
    @Column(name="Id")
    @GeneratedValue(strategy= GenerationType.AUTO)
    private int Id;
    
    @Column(name= "Title", length= 128, nullable = false, columnDefinition = "VARCHAR(60)")
    private String Title;
    @Column(name= "Year", length= 128, nullable = false, columnDefinition = "VARCHAR(60)")
    private String Year;
    
    @ManyToOne
    @JoinColumn(name="Course_id",insertable=false, updatable=false, nullable=false,columnDefinition = "int default 1")
    private Course Subject;
    public int getId() {
        return Id;
    }
    

Course class

    private int id;
    private Person person;
    private String name;
    private Set persons = new HashSet(0);

I get this error:

dic. 18, 2020 5:44:46 P. M. org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Field 'Subject' doesn't have a default value
dic. 18, 2020 5:44:46 P. M. org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Field 'Subject' doesn't have a default value

Any ideas?

Many thanks.

your field 'Subject' is not null(nullable=false), so when you are creating a new person you must define a course for it. for making the course optional for person you can try this(nullable=true). it depends on your business!

notice: it's better to use lowercase for the first letter of fields(this is a naming convention)

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