【问题标题】:OneToMany relationship error: Field 'XX' doesn't have a default valueOneToMany 关系错误:字段“XX”没有默认值
【发布时间】:2018-03-27 07:01:03
【问题描述】:

我正在尝试在两个对象之间创建一对多关系,但出现此错误。我不知道如何将属性 ID 从对象 MyUserAccount 映射到对象 Book。 ID 是从 Google 收到的字符串数据(我在我的项目中进行社交登录)。

错误

PreparedStatementCallback; SQL [INSERT INTO Books(TENSACH, TACGIA, NHANXET, TINHTRANG, THELOAI, IMAGE, IMAGE_NAME) VALUES ( ?, ?, ?, ?, ?, ?, ?)]; Field 'ID' doesn't have a default value; nested exception is java.sql.SQLException: Field 'ID' doesn't have a default value

BookDao(我如何将对象 Book 保存到数据库中)

public void save(Book book) {
    // TODO Auto-generated method stub
    KeyHolder keyHolder = new GeneratedKeyHolder();
    String sql = "INSERT INTO Books(TENSACH, TACGIA, NHANXET, TINHTRANG, THELOAI, IMAGE, IMAGE_NAME) "
            + "VALUES ( :tensach, :tacgia, :nhanxet, :tinhtrang, :theloai, :image, :image_name)";

    namedParameterJdbcTemplate.update(sql, getSqlParameterByModel(book), keyHolder);
    book.setBook_ID(keyHolder.getKey().intValue());
}

private SqlParameterSource getSqlParameterByModel(Book book) {
        MapSqlParameterSource paramSource = new MapSqlParameterSource();
        paramSource.addValue("book_id", book.getBook_ID());
        paramSource.addValue("tensach", book.getTensach());
        paramSource.addValue("tacgia", book.getTacgia());
        paramSource.addValue("nhanxet", book.getNhanxet());
        paramSource.addValue("tinhtrang", book.getTinhtrang());
        paramSource.addValue("image", book.getImage());
        paramSource.addValue("image_name", book.getImage_name());
        paramSource.addValue("theloai", book.getTheloai());
        return paramSource;
    }

样板书

public class Book implements Serializable {

private static final long serialVersionUID = 1L;
private Integer book_ID;
private String tensach;
private String tacgia;
private String nhanxet;
private String tinhtrang;
private String theloai;
private byte[] image;
private String image_name;
private String data;
private MyUserAccount myUserAccount;

public Book() {

}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID", nullable = false)
public MyUserAccount getMyUserAccount() {
    return this.myUserAccount;
}

public void setMyUserAccount(MyUserAccount myUserAccount) {
    this.myUserAccount = myUserAccount;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "book_id", unique = true, nullable = false)
public Integer getBook_ID() {
    return book_ID;
}

@Column(name = "image_name")
public String getImage_name() {
    return image_name;
}

@Column(name = "tensach", length = 50, nullable = true)
public String getTensach() {
    return tensach;
}

@Lob
@Type(type = "org.hibernate.type.BinaryType")
@Column(name = "image", columnDefinition = "LONGBLOB", nullable = true)
public byte[] getImage() {
    return image;
}

@Column(name = "tacgia", length = 50, nullable = true)
public String getTacgia() {
    return tacgia;
}

@Column(name = "nhanxet", length = 100, nullable = true)
public String getNhanxet() {
    return nhanxet;
}

@Column(name = "tinhtrang", length = 50, nullable = true)
public String getTinhtrang() {
    return tinhtrang;
}

@Column(name = "theloai", length = 50, nullable = true)
public String getTheloai() {
    return theloai;
}

@Column(name = "data", length = 16777215)
public String getData() {
    return data;
}

public void setBook_ID(Integer book_ID) {
    this.book_ID = book_ID;
}

public void setImage_name(String image_name) {
    this.image_name = image_name;
}

public void setImage(byte[] image) {
    this.image = image;
}

public void setTensach(String tensach) {
    this.tensach = tensach;
}

public void setTacgia(String tacgia) {
    this.tacgia = tacgia;
}

public void setNhanxet(String nhanxet) {
    this.nhanxet = nhanxet;
}

public void setTinhtrang(String tinhtrang) {
    this.tinhtrang = tinhtrang;
}

public void setTheloai(String theloai) {
    this.theloai = theloai;
}

public void setData(String data) {
    this.data = data;
}

@Override
public String toString() {
    return "Book [book_ID=" + book_ID + ", tensach=" + tensach + ", tacgia=" + tacgia + ", nhanxet=" + nhanxet
            + ", tinhtrang=" + tinhtrang + ", theloai=" + theloai + ", image=" + Arrays.toString(image) + "]";
}
}

为 MyUserAccount 建模。

public class MyUserAccount implements Serializable {

private static final long serialVersionUID = 1L;
public static final String ROLE_USER = "ROLE_USER";
private String id;
private String email;
private String userName;
private String firstName;
private String lastName;
private String password;
private String role;
private String enabled;
private List<Book> book = new ArrayList<Book>(0);

public MyUserAccount() {

}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "myUserAccount")
public List<Book> getBook() {
    return book;
}

public void setBook(List<Book> book) {
    this.book = book;
}

public MyUserAccount(String id, String email, String userName, String firstName, //
        String lastName, String password, String role, String enabled) {
    this.id = id;
    this.email = email;
    this.userName = userName;
    this.firstName = firstName;
    this.lastName = lastName;
    this.password = password;
    this.role = role;
    this.enabled = enabled;
}

@Id
@Column(name = "ID", unique = true, nullable = false)
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

@Column(name = "EMAIL", unique = true, nullable = false)
public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

@Column(name = "USER_NAME", unique = true, nullable = false)
public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

@Column(name = "FIRST_NAME", nullable = false)
public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

@Column(name = "LAST_NAME", nullable = false)
public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

@Column(name = "PASSWORD", nullable = false)
public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

@Column(name = "ROLE", nullable = false)
public String getRole() {
    return role;
}

public void setRole(String role) {
    this.role = role;
}

@Column(name = "ENABLED", columnDefinition = "VARCHAR(1) default 'Y'", nullable = false)
public String getEnabled() {
    return enabled;
}

public void setEnabled(String enabled) {
    this.enabled = enabled;
}
}

控制器

@RequestMapping(value = "/motsach/add/", method = RequestMethod.POST)
    public String saveBook(@ModelAttribute("bookForm") @Validated Book book, BindingResult result, Model model,
            @RequestParam CommonsMultipartFile[] image, String userName, final RedirectAttributes redirectAttributes)
            throws IOException, UnsupportedEncodingException {

    MyUserAccount myUserAccount = myUserAccountDAO.findByUserName(userName);
    System.out.println(userName + "sssssssssssss");
    book.setMyUserAccount(myUserAccount);
    redirectAttributes.addFlashAttribute("css", "success");
    if (book.getBook_ID() == null) {
        System.out.println(book.getBook_ID());
        redirectAttributes.addFlashAttribute("msg", "book added successfully!");
    } else {
        redirectAttributes.addFlashAttribute("msg", "book updated successfully!");
    }
    for (CommonsMultipartFile aFile : image) {
        System.out.println("Saving file: " + aFile.getOriginalFilename());
        book.setImage_name(aFile.getOriginalFilename());
        book.setImage(aFile.getBytes());
        System.out.println("Damn that Shit!");
    }
    bookService.saveOrUpdate(book);
    // POST/REDIRECT/GET
    return "redirect:/motsach/" + book.getBook_ID();
}

【问题讨论】:

    标签: java mysql hibernate orm one-to-many


    【解决方案1】:

    您有一个不能为空的@manyToOne(以 ID 作为名称)关系。因此,为了添加一本书,您必须在保存之前将MyUserAccount 设置为一本书,或者您可以变成:

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID", nullable = true)
    public MyUserAccount getMyUserAccount() {
        return this.myUserAccount;
    }
    

    并修改数据库中的列以设置空值的可能性。

    【讨论】:

    • 谢谢,但是如何将 MyUserAccount 设置为 Book?我真的需要这样做,我刚刚更新了我的控制器。我不知道如何获得属性 userName
    • 您现在遇到的错误是什么?是同一个吗?
    • 不,错误是属性 userName 为空。正如您所说,我正在尝试将 MyUserAccount 设置为 Book,但首先我什至无法获取 MyUserAccount 数据。它总是返回 null。
    • 从哪里获得属性 userName ?是表格还是什么?
    • 如果您之前没有尝试在您的数据库中添加一个用户并获取他的用户名示例:如果您的用户有用户名,请将这一行 myUserAccountDAO.findByUserName(userName); 替换为这一行:myUserAccountDAO.findByUserName( "userTest" );
    猜你喜欢
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    相关资源
    最近更新 更多