- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要关联 @OneToMany
从 Country 到父类(super class) Place
(@MappedSuperclass
)。它可以是双向的。我需要像 @OneToAny
这样的东西。
@MappedSuperclass
public class Place {
private String name;
private Country country;
@Column
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToOne
@JoinColumn(name="country_id")
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
}
国家:
@Entity
public class Country {
private long id;
private String name;
private List<Place> places;
@Any(metaColumn = @Column(name = "place_type"), fetch = FetchType.EAGER)
@AnyMetaDef(idType = "integer", metaType = "string", metaValues = {
@MetaValue(value = "C", targetEntity = City.class),
@MetaValue(value = "R", targetEntity = Region.class) })
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
//@JoinColumn(name="unnecessary")
//@OneToMany(mappedBy="country") // if this, NullPointerException...
public List<Place> getPlaces() {
return places;
}
//and rest of class
没有 @JoinColunm
会有一个异常(exception):
Caused by: org.hibernate.AnnotationException: @Any requires an explicit @JoinColumn(s): tour.spring.bc.model.vo.Country.places
在表 City 和 Region 中是表 Country (Region.country_id, City.country_id) 的外键,这没问题。但是我不需要表 Country 中的外键到表 Region 和 City,所以我不需要 @JoinColum
。
我能做什么?
最佳答案
@Any
在这里没有意义,因为外键位于 Place
一侧,因此它不需要额外的元列。
我不确定是否可以创建与@MappedSuperclass
的多态关系。但是,您可以尝试将 Place
声明为 @Entity @Inheritance(InheritanceType.TABLE_PER_CLASS)
,它应该产生相同的数据库模式并允许多态关系。
关于java - @MappedSuperclass 和@OneToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4769546/
在 JPA/Hibernate 中,像 BetterBaseEntity 这样的映射父类(super class)可以是另一个映射父类(super class) BaseEntity 的子类吗? 我想
我在 Symfony 5 中与 MappedSuperclass 的多对一关系存在问题。我有实体 Employee、Employer,它扩展了 MappedSuperclass 抽象类 Person。
伙计们! 如果我定义一个实体类并用 @MappedSuperclass 注释它和一个 @EntityListener ,在子类中是否还会调用监听器来处理生命周期事件? 例子: @MappedSuper
我在搜索从@MappedSuperclass 扩展的实体时遇到了问题。我创建了一个类 PhoneBook 并从中扩展了 2 个实体:FirstPhoneBook 和 SecondPhoneBook。结
我有一个映射父类(super class) @MappedSuperclass public class A{ . . . @Column(name="somethin
我想知道是否有办法在继承 MappedSuperClass 的子类中更改(或定义内部)注释,例如,假设我们有一个类 BaseUser(mappedsuperclass),一个子类 User :
我有一些表有一些公共(public)列,我认为在创建实体时我会让实体扩展一个定义了所有映射的类: @MappedSuperclass public class AbstractLogMaster im
假设我有一个 JPA 实体: @Entity @Table(name = "PARENT") public class Parent { // ... } 有没有办法,也许是特定于 Hiber
我正在努力创建一些抽象类来抽象每个实体将共享的属性,例如 ID。 我创建了一个 AbstractModel 类,如下所示。 import javax.persistence.*; import jav
我想与双方的同一个表创建ManyToMany关系。它应该代表两个用户之间的友谊。我现在有用户类别: @Entity @Table(name = "Users") public class User e
我的 Web 应用程序中有几个领域模型类,它们之间存在层次关系。其中一个例子是用于对用户帖子进行分类的分层类别结构。 有一些与这些类的层次结构相关的逻辑是常见的。因此,我尝试将逻辑移动到通用的 @Ma
问题 我有一个名为 Data 的 @MappedSuperclass 作为我数据库中每个实体的父级。它包含 Id 等公共(public)属性。然后我有一个扩展 Data 的实体,由于其子类的公共(pu
我有一个摘要TemporalModel向所有扩展模型添加 created 和 updated 字段的类(用 @MappedSuperclass 注释)。我想给它添加一个 getLatest() 静态方
我需要关联 @OneToMany 从 Country 到父类(super class) Place (@MappedSuperclass)。它可以是双向的。我需要像 @OneToAny 这样的东西。
有: @MappedSuperclass class Superclass { @Id @Column(name = "id") protected long id;
我有一个场景,我需要使用 Doctrine 的 MappedSuperclass 功能(使用 Symfony2),并在一些父类(super class)列上创建一个唯一约束。比方说: use Doct
我将 JPA2 与 Hibernate 一起使用,并尝试为我的实体引入一个公共(public)基类。到目前为止,它看起来像这样: @MappedSuperclass public abstract c
我可以在父类和子类中定义相同的 JPA 回调方法吗?如果是,我是否需要调用 super.onPrePersist();在子类的 onPrePersist() 方法中? @MappedSuperclas
我正在使用 JPA 2.0 和 EclipseLink 2.2.0。 我有一个 @MappedSuperclass,AbstractEntity,它是我所有提供 PK 和审计列的实体的基础。 我想让另
@MappedSuperclass public abstract class AbstractBaseModel{ } @MappedSuperclass public class Person e
我是一名优秀的程序员,十分优秀!