【问题标题】:Entity not mapped to a single property error with inherited entites of one table实体未映射到具有一个表的继承实体的单个属性错误
【发布时间】:2021-04-03 04:24:16
【问题描述】:

我有两个实体 SuperAlbumEntityAlbumEntity 反映同一个表“albums”

超级专辑实体

@Entity
@Table(name = "albums")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class SuperAlbumEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    //other fields
}

相册实体

@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "albums")
public class AlbumEntity extends SuperEntity{

    //some fields

    @Column(name = "country")
    private String country;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "country_name", referencedColumnName = "country")
    private Set<CountryEntity> countrySet = new HashSet<>();
}

AlbumEntity@OneToMany 映射到 CountryEntity

@Entity
@Table(name = "countries")
public class CountryEntity implements Serializable {

    @Id
    String id;

    String country_name;

    //other fields
}

运行我的应用程序出现以下错误:

...
Caused by: org.hibernate.AnnotationException: referencedColumnNames(country) of CountryEntity.countrySet referencing AlbumEntity not mapped to a single property
...

有趣的是,如果我将 country 字段从 SuperAlbumEntity 移动到 AlbumEntity 一切正常... 谁能解释一下为什么会出现这个错误?

【问题讨论】:

  • 我看到 countrySet 是一组 ContryEntity?这将是问题
  • 是的,它是一组 CountryEntity,因为我有一个 @OneToMany 映射
  • @Omar Amaoun,如果我将“countrySet”更改为“country”,一切正常,但我不明白为什么......

标签: spring-boot spring-data-jpa spring-data hibernate-mapping


【解决方案1】:

我不确定,但我认为这与您使用它的内在类型有关。尝试将您的超类修改为以下内容:

超级专辑实体:

@MappedSuperclass
public abstract class SuperAlbumEntity {

}

相册实体:

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name = "albums")
public class AlbumEntity extends SuperEntity {

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "country_name", referencedColumnName = "country")
    private Set<CountryEntity> countrySet = new HashSet<>();
}

【讨论】:

  • 不幸的是没有帮助
猜你喜欢
  • 1970-01-01
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-02
  • 1970-01-01
  • 2022-01-02
相关资源
最近更新 更多