微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么在使用@ManyToOne时不能在数据库中插入行?

如何解决为什么在使用@ManyToOne时不能在数据库中插入行?

我有这些实体。

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Language {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @NotNull
    private String language;
    
}

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Sentence {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @NotNull
    private String wordInfrench;
    
    @NotNull
    private String wordInOtherLanguage;
    
    @ManyToOne
    @NotNull
    private Language language;
    
}

当我尝试通过使用以下代码将行插入sentence数据库中时:

// My repository
private SentenceRepository sentenceRepository;

// Object information
Long id = 0L; 
String wordInfrench = "Oui oui"
String wordInOtherLanguage = "Ja ja"
Language language = new Language(id,"Swedish");

// Save to database
sentenceRepository.save(new Sentence(id,wordInfrench,wordInOtherLanguage,language));

我收到此错误

org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find 
    se.danielmartensson.entity.Language with id 0; nested exception is javax.persistence.EntityNotFoundException: Unable to find se.danielmartensson.entity.Language with id 0

    Caused by: javax.persistence.EntityNotFoundException: Unable to find se.danielmartensson.entity.Language with id 0

如果我使用Long id = 1L;,也会出现此错误

org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find 
se.danielmartensson.entity.Language with id 1; nested exception is javax.persistence.EntityNotFoundException: Unable to find se.danielmartensson.entity.Language with id 1

Caused by: javax.persistence.EntityNotFoundException: Unable to find se.danielmartensson.entity.Language with id 1

那是怎么回事?为什么不能将sentence插入数据库,而language数据库也将更新?

解决方法

问题是Hibernate试图在数据库中找到id = 0(和1)的Language实例,但是它不存在。

首先,您必须插入语言,然后才能插入句子:

// My repositories
private LanguageRepository languageRepository;
private SentenceRepository sentenceRepository;

// Inserting the Language
Language language = new Language();
language.setLanguage("Swedish");
// Here,note that I'm not forcing the value of the attribute "id". 
// The "@GeneratedValue" will generate one for me.

// Also note that the "save" method returns an object. In this case,// it will be a "Language" instance with the new id
language = languageRepository.save(language);

// Inserting the Sentence
Sentence sentence = new Sentence();
sentence.setWordInFrench("Oui oui");
sentence.setWordInOtherLanguage("Ja ja");
sentence.setLanguage(language);

sentenceRepository.save(sentence);  

或者,您可以执行以下操作:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Sentence {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @NotNull
    private String wordInFrench;
    
    @NotNull
    private String wordInOtherLanguage;
    
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "id_language",nullable = false)
    private Language language;

我们使用cascade = CascadeType.PERSIST表示每次尝试插入新句子时,Hibernate都会自动检查数据库中是否已经存在language对象。否则,将language预先插入。 此外,对于外键,建议使用@JoinColumn映射来配置列定义,例如“名称”,“可空”,“唯一”等。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。