gpt4 book ai didi

java - 带有 H2 数据库的 JUnit : Unique index or primary key violation when adding multilingual services for multiple data

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:20:04 27 4
gpt4 key购买 nike

Hibernate在创建数据库的过程中,在oe_iv_student_lang表中为外键id_student添加了唯一键约束,因为我们要实现 Serializable 接口(interface)导致 Hibernate 不允许我们在其对应的子表中添加具有相同父外键的多行。

我附上了代码片段以便更好地理解..

学生类(class):

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "oe_iv_student")
public class OeIvStudent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_student")
private Integer idStudent;
}

朗类同学:

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "oe_iv_student_lang")
public class OeIvStudentLang implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_student_lang")
private Integer idStudentLang;

@OneToOne
@JoinColumn(name = "id_student")
private OeIvStudent idStudent;

@Column(name = "ln_code")
private String lnCode;
}

位置类:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "oe_locations")
public class OeLocations {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_location")
private Integer idLocation;

@OneToOne
@JoinColumn(name = "id_student", referencedColumnName = "id_student")
private OeIvStudentLang idStudent;

@Column(name = "code")
private String code;
}

我使用 Serializable 接口(interface)的原因是我在 OeLocations 中映射了 OeIvStudentLang 类的对象,但引用的列是 OeIvStudent 类。拥有 OeIvStudentLang 的对象使我更容易浏览表格,即

OeLocations->OeIvStudentLang->OeIvStudent

如果我不在 OeIvStudentLang 类中使用 Serializable,Hibernate 会抛出一个 Exception 说明 OeIvStudentLang 是当我使用 OeLocations 类时,不是 Serializable。如果我改用 OeIvStudent 的对象,我将无法导航到OeLocations 中的 lang 类。

请看一下并建议要做什么。

谢谢:)

最佳答案

这与实现 Serializable 没有任何关系。

您已将 OeIvStudentLangOeIvStudent 之间的关系定义为 @OneToOne。这意味着每个 OeIvStudentLang 都有恰好一个 OeIvStudent。正如您所说,这应该是一对多关系,因此您需要将映射从 @OneToOne 更改为 @OneToMany 并且应该删除它唯一约束。

对于 reference :

A one-to-one association is similar to many-to-one association with a difference that the column will be set as unique.

关于java - 带有 H2 数据库的 JUnit : Unique index or primary key violation when adding multilingual services for multiple data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45159439/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com