java - Embeddable 类中的重复列抛出错误
问题描述
我使用 JPA 工具创建了实体类,它为没有主键的表创建了两个类。一个是@Entity 类,另一个是@Embeddable 类,下面是这两个类的代码
国家语言
package com.geolocation.entities;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the countrylanguage database table.
*
*/
@Entity
@NamedQuery(name="Countrylanguage.findAll", query="SELECT c FROM Countrylanguage c")
public class Countrylanguage implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private CountrylanguagePK id;
private String isOfficial;
private String language;
private float percentage;
//bi-directional many-to-one association to Country
@ManyToOne
@JoinColumn(name="CountryCode")
private Country country;
public Countrylanguage() {
}
// public CountrylanguagePK getId() {
// return this.id;
// }
//
// public void setId(CountrylanguagePK id) {
// this.id = id;
// }
public String getIsOfficial() {
return this.isOfficial;
}
public void setIsOfficial(String isOfficial) {
this.isOfficial = isOfficial;
}
public String getLanguage() {
return this.language;
}
public void setLanguage(String language) {
this.language = language;
}
public float getPercentage() {
return this.percentage;
}
public void setPercentage(float percentage) {
this.percentage = percentage;
}
public Country getCountry() {
return this.country;
}
public void setCountry(Country country) {
this.country = country;
}
}
** 国家语言PK**
package com.geolocation.entities;
import java.io.Serializable;
import javax.persistence.*;
/**
* The primary key class for the countrylanguage database table.
*
*/
@Embeddable
public class CountrylanguagePK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(insertable=false, updatable=false)
private String countryCode;
private String language;
public CountrylanguagePK() {
}
public String getCountryCode() {
return this.countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getLanguage() {
return this.language;
}
public void setLanguage(String language) {
this.language = language;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CountrylanguagePK)) {
return false;
}
CountrylanguagePK castOther = (CountrylanguagePK)other;
return
this.countryCode.equals(castOther.countryCode)
&& this.language.equals(castOther.language);
}
public int hashCode() {
final int prime = 31;
int hash = 17;
hash = hash * prime + this.countryCode.hashCode();
hash = hash * prime + this.language.hashCode();
return hash;
}
}
执行时抛出错误..
- org.springframework.beans.factory.BeanCreationException:在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]中定义名称为“entityManagerFactory”的bean创建错误:调用init方法失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; 嵌套异常是 org.hibernate.MappingException:实体映射中的重复列:com.geolocation.entities.Countrylanguage 列:country_code(应使用 insert="false" update="false" 映射)
- 引起:javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; 嵌套异常是 org.hibernate.MappingException:实体映射中的重复列:com.geolocation.entities.Countrylanguage 列:country_code(应使用 insert="false" update="false" 进行映射)
- 引起:org.hibernate.MappingException:实体映射中的重复列:com.geolocation.entities.Countrylanguage 列:country_code(应使用 insert="false" update="false" 进行映射)
解决方案
嵌入类型不应该使用insertable = false, updatable = false
,而是关联@JoinColumn
的。@ManyToOne
推荐阅读
- django - 如何为过滤器列表中的每个项目设置排序和项目数量?
- google-analytics - 有没有办法在 Google Data Studio 的综合浏览量报告中包含上次修改 SharePoint 页面的时间?
- javascript - MongoDB 从正文中查找、更新和验证空字段
- c# - 使用 Left Dock 时出现 AutoScroll 问题 - C# Telerik Winforms
- javascript - 如何在 Laravel Mix 中为每个文件使用不同的编译设置来编译多个 JavaScript 文件
- elasticsearch - Elasticsearch 上的结果可疑的低
- excel - 对于列中的每个 xxx 未按预期工作
- opentok - OpenTok / ToxBox:将 API 密钥保密?
- html - Flexbox 容器的粘性位置
- php - 如何返回数组的文本值而不是函数结果?