Java 为什么可以';当我使用@ManyToOne时,是否在数据库中插入一行?
我有这些实体Java 为什么可以';当我使用@ManyToOne时,是否在数据库中插入一行?,java,spring-boot,spring-data-jpa,Java,Spring Boot,Spring Data Jpa,我有这些实体 @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 Language {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
private String language;
}
及
当我尝试使用以下代码将一行插入语句
数据库时:
// 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
那么发生了什么?为什么我不能在数据库中插入一个语句
,而语言
数据库也会更新?问题是Hibernate试图在数据库中找到id=0(和1)的语言
实例,但它不存在
首先必须插入语言,然后才能插入句子:
// 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都会自动检查语言
对象是否已存在于数据库中。如果没有,则将事先插入语言
。
此外,对于外键,建议使用@JoinColumn
映射来配置列定义,如“name”、“nullable”、“unique”等。我认为您需要您的语言的数据库引用。这意味着您应该首先通过两个步骤来持久化它,或者@ManyToOne(fetch=FetchType.LAZY,cascade=CascadeType.persist)可以be@Novy不起作用。替代建议不起作用,但我确信我需要使用cascade Presist或其他什么?CascadeType.PERSIST
是一种额外有用的配置,但完全是可选的。替代方案出了什么问题?有错误日志可以给我看吗?那么第一个解决方案呢?它有效吗?您的第一个示例有效=)。首先保存语言,然后通过在其中包含语言来保存句子。PRESIST给出了一个错误,说它无法处理PRESIST。顺便说一下!如果你想尝试这个简单的应用程序,你可以从这里下载。只需打开maven并在项目文件夹中运行“mvn spring boot:run”。注意,您还需要MySQL数据库:)
@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;