gpt4 book ai didi

java - DuplicateMappingException 表 [] 包含由多个逻辑列名称引用的物理列名称 [] : [_id], [Id]

转载 作者:行者123 更新时间:2023-12-01 17:00:36 25 4
gpt4 key购买 nike

我正在重写一个项目,但遇到了构建问题。

Caused by: org.hibernate.DuplicateMappingException: Table [language] contains physical column name [person_id] referred to by multiple logical column names: [person_id], [personId]

哪里

@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@IdClass(LanguagePK.class)
@Table(name = "language")
public class Language {
@Id
@Column(name = "person_id")
private int personId;
@Id
@Column(name = "position")
private int position;
@Id
@Column(name = "language_name")
private String languageNameVal;
@Column(name = "language_level")
private String languageLevelVal;
@ManyToOne
@JoinColumn(name = "person_id", referencedColumnName = "id", nullable = false, insertable = false,updatable = false)
@JsonBackReference(value = "languages")
private Person person;
@ManyToOne
@JoinColumn(name = "language_name", referencedColumnName = "name", nullable = false, insertable = false,updatable = false)
@JsonIgnore
private LanguageName languageName;
@ManyToOne
@JoinColumn(name = "language_level", referencedColumnName = "level", nullable = false, insertable = false,updatable = false)
@JsonIgnore
private LanguageLevel languageLevel;
}

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "person")
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@JsonManagedReference(value = "languages")
@OneToMany(mappedBy = "person", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private Set<Language> languages;

我用

  • hibernate 版本 5.4.13.Final
  • spring-boot v.2.2.5.RELEASE
  • 梯度
  • java 1.8
  • Lombok

这些变化非常小,就像我之前使用 maven、用于 hibernate 的 SessionFactory、传统的 getter 和 setter 一样。

我看到了一些类似的问题,其中 NamingStrategy 在 hibernate 4 和 5 之间迁移期间出现问题,但此代码适用于 hiberante 为 5.4.10.Final 的第一个项目。

我喜欢 IdClass,因为我可以访问 personId 作为原始值,并且不必从对象 person 加载数据。

我在没有 insertable = false,updatable = false 的情况下看到了类似的问题,但我有它......

发现了类似问题 here

最佳答案

解决方案

问题在于访问策略字段与属性访问。

在之前的项目中,一切都是基于属性的访问,但在我添加 lombok 后,类 Language 是基于字段的访问。

LanguagePK

public class LanguagePK implements Serializable {
private static final long serialVersionUID = 1L;
private int personId;
private int position;
private String languageNameVal;

@Column(name = "person_id")
@Id
public int getPersonId() {
return personId;
}

public void setPersonId(int personId) {
this.personId = personId;
}

@Column(name = "position")
@Id
public int getPosition() {
return position;
}

public void setPosition(int position) {
this.position = position;
}

@Column(name = "language_name")
@Id
public String getLanguageNameVal() {
return languageNameVal;
}

public void setLanguageNameVal(String languageNameVal) {
this.languageNameVal = languageNameVal;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

LanguagePK that = (LanguagePK) o;

if (personId != that.personId) return false;
if (position != that.position) return false;
if (languageNameVal != null ? !languageNameVal.equals(that.languageNameVal) : that.languageNameVal != null)
return false;

return true;
}

@Override
public int hashCode() {
int result = personId;
result = 31 * result + position;
result = 31 * result + (languageNameVal != null ? languageNameVal.hashCode() : 0);
return result;
}
}

这已经足够了并且有效

@EqualsAndHashCode
public class EducationPK implements Serializable {
private int personId;
private int position;
}

关于java - DuplicateMappingException 表 [] 包含由多个逻辑列名称引用的物理列名称 [] : [_id], [Id],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61510176/

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