gpt4 book ai didi

java - JPA/Hibernate 注解不懂继承?

转载 作者:行者123 更新时间:2023-12-02 07:41:13 25 4
gpt4 key购买 nike

当我尝试使用注释设置模式时,我在 Hibernate 中遇到了一些令人沮丧的行为。假设我有一个与其他几种类型具有一对多关系的实体。例如:

@Entity
@Table(name = "clubs")
public class Club {
private long id;
private String name;
//...

private Collection<ClubLocation> locations;

public Club() {
this.name = null;
//...

this.locations = Collections.emptyList();
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}

@Column(nullable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@OneToMany(mappedBy = "club")
public Collection<ClubLocation> getLocations() {
return locations;
}
public void setLocations(Collection<ClubLocation> locations) {
this.locations = locations;
}
}

现在,正如我提到的,我想将此实体与许多其他实体相关联,因此定义一个父类(super class)实体来管理与 Club 的关系似乎是个好主意,这样我就不会不需要在每个关联的类中设置关系。我这样做是使用:

@Entity
@Table(name = "clubItems")
@Inheritance(strategy = InheritanceType.JOINED)
public class ClubItem {
protected Club club;

public ClubItem() {
this.club = null;
}

@ManyToOne(optional = false)
public Club getClub() {
return club;
}
public void setClub(Club club) {
this.club = club;
}
}

然后我扩展该类,如下所示:

@Entity
@Table(name = "clubLocations")
public class ClubLocation extends ClubItem {
private String title;
//...

public ClubLocation() {
this.title = null;
//...
}

@Column(nullable = false)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

一切似乎都很好,除了当我尝试运行然后 Hibernate 爆炸时:

org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: ClubLocation.club in Club.locations
[WARNING] [talledLocalContainer] at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:543)
[WARNING] [talledLocalContainer] at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:508)
[WARNING] [talledLocalContainer] at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
[WARNING] [talledLocalContainer] at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1127)
[WARNING] [talledLocalContainer] at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
[WARNING] [talledLocalContainer] at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1112)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1233)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:154)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:869)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:183)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:240)
[WARNING] [talledLocalContainer] at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:120)
[WARNING] [talledLocalContainer] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
[WARNING] [talledLocalContainer] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:60)

令人沮丧的是,如果我在 ClubLocation 类中复制 club 属性的 getter 和 setter 声明,一切都很好。具体来说,如果我添加:

@Override
@ManyToOne(optional = false)
public Club getClub() {
return super.getClub();
}
public void setClub(Club club) {
super.setClub(club);
}

...进入 ClubLocation 类。但这是没有意义的,因为它所做的只是委托(delegate)给父类(super class)。

Hibernate 是否不够复杂,无法在子类中重新声明 getter/setter 的情况下处理继承的映射字段?如果 Hibernate 可以更优雅地处理这种情况,那么需要如何设置/我做错了什么?

最佳答案

我在一个项目中有类似的设置。我认为解决这个问题的一个简单且或多或少不错的方法是改变

private Collection<ClubLocation> locations;

进入

private Collection<ClubItem> items;

(以及相关的 getter、setter)。这样肯定有效。

如果您出于方便原因需要在 Club 中使用 getLocations 方法,您可以自由创建该方法。如果您确定该项目仅包含位置,则可以按原样返回集合,否则您需要在 Java 代码中对其进行过滤。

缺点是,如果项目还包含非位置元素,则 Java 中的过滤会占用内存和 CPU,因此可能会很慢。

关于java - JPA/Hibernate 注解不懂继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11553603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com