简体   繁体   English

为什么 Hibernate 允许我在外键约束上插入空值?

[英]Why does Hibernate allow me to insert a null value on a foreign key constraint?

I am experimenting with jpa and hibernate relations.我正在试验 jpa 和休眠关系。 I'm using a table named users and a table named emails.我正在使用一个名为 users 的表和一个名为 emails 的表。 A user can have many emails.一个用户可以有许多电子邮件。

When I run my Spring boot application along with the following code I get one email record in my h2 database.当我运行 Spring boot 应用程序和以下代码时,我在 h2 数据库中得到了一封电子邮件记录。 The email_address of this record is testAddress and the user_username column of this record is null.该记录的email_address 为testAddress,该记录的user_username 列为空。 The user_username column is listed as a foreign key on the emails table. user_username 列在 emails 表中被列为外键。 My question is, why is emailRepository.save(email1) successful when there is no corresponding user in the database?我的问题是,当数据库中没有对应的用户时,为什么 emailRepository.save(email1) 成功?

@Entity
@Table(name = "emails")
public class Email {

    @Id
    private String emailAddress;

    @ManyToOne
    private User user;

    ...

}

@Entity
@Table(name = "users")
public class User {

    @Id
    private String username;

    @OneToMany(mappedBy="user", cascade=CascadeType.ALL, orphanRemoval=true)
    private Set<Email> emails;

    ...

}

public interface UserRepository extends JpaRepository<User, String> {

}

public interface EmailRepository extends JpaRepository<Email, String> {

}

@Component
public class UserRepositoryCommandLineRunner implements CommandLineRunner {

    @Autowired
    private EmailRepository emailRepository;

    public void run(String... args) throws Exception {
        Email email1 = new Email();
        email1.setEmailAddress("testAddress");
        emailRepository.save(email1);
    }

}

Take a look at the documentation of the JoinColumn annotation: https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/JoinColumn.html#nullable()看一下JoinColumn注解的文档: https ://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/JoinColumn.html#nullable ()

It is mentioned:它提到:

If the JoinColumn annotation itself is defaulted, a single join column is assumed and the default values apply.如果 JoinColumn 注释本身是默认值,则假定为单个连接列并应用默认值。

Since you did not specify a JoinColumn in your ManyToOne mapping, then Hibernate would assume the default JoinColumn .由于您没有在ManyToOne映射中指定JoinColumn ,那么 Hibernate 将假定默认JoinColumn If you take a look at the JoinColumn.nullable attribute, it is defaulted to true.如果您查看JoinColumn.nullable属性,它默认为 true。 Thus, when Hibernate generates your schema, the foreign key column is by default NULLABLE.因此,当 Hibernate 生成您的架构时,外键列默认为 NULLABLE。

You may need to explicitly add a @JoinColumn annotation on top of your @ManyToOne mapping and set its nullable attribute to false.您可能需要在@ManyToOne映射之上显式添加@JoinColumn注释并将其nullable属性设置为 false。

@ManyToOne
@JoinColumn(nullable=false)
private User user;

This way, it'll throw out an error when you try to insert email without a user.这样,当您尝试在没有用户的情况下插入电子邮件时,它会抛出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM