问题描述
我已经使用 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:错误 使用在类路径中定义的名称“entityManagerFactory”创建 bean 资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: init 方法调用失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: 默认] 无法构建 Hibernate SessionFactory;嵌套异常是 org.hibernate.MappingException:映射中的重复列 实体:com.geolocation.entities.Countrylanguage 列:country_code (应该用 insert="false" update="false" 映射)
- Caused by: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
。