- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我必须创建类的层次结构,其中父类(super class)具有表示 map 的 @CollectionTable
。我试图实现它,但它只适用于一个子类。
稳定(工作)代码如下所示:
@MappedSuperclass
public class Animal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
}
@Entity(name = "cats")
@Audited
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.CHAR)
@DiscriminatorValue(value = Cat.PET_TYPE)
public abstract class Cat extends Animal {
public static final String PET_TYPE = "C";
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "name")
@Column(name = "value")
@CollectionTable(name = "cat_properties", joinColumns = @JoinColumn(name = "cat_id"))
private Map<String, String> properties = new HashMap<>();
}
@Audited
@Entity(name = "persiancats")
@DiscriminatorValue(value = PersianCat.PERSIAN_CAT_TYPE)
public class PersianCat extends Cat {
public static final String PERSIAN_CAT_TYPE = "P";
}
这就是我尝试实现修改的方式:
@MappedSuperclass
public class Animal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
}
@MappedSuperclass
public abstract class Pet extends Animal {
@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "name")
@Column(name = "value")
@CollectionTable(name = "pet_properties", joinColumns = @JoinColumn(name = "pet_id"))
private Map<String, String> properties = new HashMap<>();
}
@Entity(name = "cats")
@Audited
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.CHAR)
@DiscriminatorValue(value = Cat.PET_TYPE)
public class Cat extends Pet {
public static final String PET_TYPE = "C";
}
@Entity(name = "dogs")
@Audited
public class Dog extends Pet {
}
@Audited
@Entity(name = "persiancats")
@DiscriminatorValue(value = PersianCat.PERSIAN_CAT_TYPE)
public class PersianCat extends Pet {
public static final String PERSIAN_CAT_TYPE = "P";
}
Hibernate 创建了 pet_properties
表,但它只引用了 dogs
或 cats
。我的意图是为狗和猫(以及波斯猫)属性创建一个通用表。
我错过了什么或做错了什么?
最佳答案
我想您要实现的目标在逻辑上是不可能的,这就是原因(我偶然发现了同样的问题,尽管是在另一个领域)。要从您的 pet_properties
表中引用一个表,Hibernate 创建一个从 pet_properties
到目标表(在您的例子中是狗或猫)的外键。外键本质上意味着这个表中的每个条目都应该对应另一个表中的一个条目,而另一个表只是一个表。因此,如果您说您的 pet_properties
条目引用了 dogs
和 cats
表,那么它会立即破坏外键属性,因为如果它引用狗,则它不引用猫,反之亦然。
因此,Hibernate 为您提供的自动生成选项(单独的表 dogs_properties
和 cats_properties
)似乎是正确的方法。
关于java - 如何使用继承处理 JPA CollectionTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40020132/
我需要覆盖 JPA 中的集合表吗?也许有人有想法 public class nationality{ @Embedded @AttributeOverrides({
我有一个域 Service带收藏tags如下: @Entity public class Service extends AbstractEntity { private st
我正在 Spring RESTfull API 中创建一个 @CollectionTable。我有一个名为大学的实体表。现在我正在创建一个名为 TargetedGift 的实体,其中我将有一个大学 I
简而言之 JPA 文档说 CollectionTable.joinColumns属性是“引用实体主表的集合表的外键列。” 我可以让它不引用实体表的主键列,而是引用另一个具有唯一值的列吗? 详情 有一个
我正在尝试使用嵌入式注释在 JPA 中建立关系,但我无法成功运行它, 这里我的数据库sql脚本如下, create table TBL_COLLEGE( id integer primary k
我必须创建类的层次结构,其中父类(super class)具有表示 map 的 @CollectionTable。我试图实现它,但它只适用于一个子类。 我想做以下事情。我在左边有当前结构,在右边有所需
嗨,有人可以帮我解决这个异常吗?这里我有一个名为 Person 的实体类,并且具有实例变量的 Person 类有 name 和 petnames,这里 petnames 我将其作为简单值类型,数据类型
使用带有collection标签的域Service,如下所示: @Entity public class Service extends AbstractEntity { private st
嗨,有人可以帮我解决这个异常吗?这里我有一个名为 Person 的实体类,并且具有实例变量的 thisclass 有名称和宠物名称,这里的宠物名称我采用了简单的值类型,数据类型是集合,并且具有使用 j
我有一个用 SpringBoot 2 和 MySQL 数据库编写的项目,我正在尝试使用 MongoDB 将其重写为 Spring WebFlux。 有 2 个表:CONTRACT 和 CONTRACT
我尝试审核Order类有 List field 。Order类看起来像这样: class Order { @Audited @ElementCollection(fetch = Fet
您好,我有以下 JPA 实体类。它有两个用于组和联系人 ID 的 @CollectionTable 映射。我很感兴趣从 contact_details 表中获取唯一联系人 ID 的列表,并在下面的 U
尝试使用 CriteriaBuilder 生成动态查询时,Hibernate 没有针对与 @ElementCollection 关联的实体成员变量创建正确的 SQL。 . 示例实体: @Entity
这个问题已经有答案了: How to delete data from org.hibernate.collection.PersistentBag? | Hibernate (Jpa) (1 个回答
假设我有以下实体,它为订阅者建模并使用 CollectionTable 来为订阅列表建模,如下所示: @Entity @Table(name = "SUBSCRIBER") public class
@Entity public class User{ @ElementCollection @Enumerated(EnumType.STRING) @CollectionTa
我有一个非常简单的实体产品,它有代码、名称和标签。标签存储在另一个表 (product_tag) 中,其中包含 product_id 和 tag 列。 我需要使用 CriteriaQuery 搜索带有
我有一个如下所示的实体。它被简化了,因为我无法发布原始代码,但它应该足以理解这个想法。 重点是: 使用 EntityManager 我可以删除 my_table 表中的条目。到目前为止,一切都很好。现
我有以下结构: public class Profile { ... @ElementCollection(targetClass = ProfileFieldImpl.class, fetch
就我而言,我有两个表:父级“VDI_DICT_EMPOYEES_SV”和子级“VDI_DICT_EMPOYEES_RV”由具有相同名称“R_OBJECT_ID”的列连接。 (参见ER图) “父”实体的
我是一名优秀的程序员,十分优秀!