相关疑难解决方法(0)

JPA MappedSuperclass 与 ManyToOne

我面临着一个典型的问题。想象一下对象之间典型的 1-N 关系。准确地说,用户(U)和房间(R):[U]*---1[R]。

问题来了,Room 应该是带有实现的抽象基类,例如 BlueRoom、RedRoom。如何正确设置用户实体内的关系?

public interface Room { ... }
@MappedSuperclass
public abstract class RoomSuperclass implements Room { ... }
@Entity
public class BlueRoom extends RoomSuperclass { ... }
@Entity
public class RedRoom extends RoomSuperclass { ... }


@Entity
public class User {

  @ManyToOne(targetEntity = ???) // I don't know if it will be BlueRoom or RedRoom
  private Room room; // ManyToOne cannot be applied on mapped superclass
}
Run Code Online (Sandbox Code Playgroud)

我知道这可能可以通过在 RoomSuperclass 上使用 @Entity 而不是 @MappedSuperclass 来解决,但我不确定这是否是一个好的解决方案以及是否有更好的解决方案。

entity jpa mappedsuperclass

5
推荐指数
1
解决办法
8477
查看次数

超类的JPA OneToMany协会

我正在尝试映射超类LendingLine和子类Line和BlockLine的继承。LendingLine与Lending有ManyToOne关联。

当我尝试从数据库获取LendingLines而没有继承时,它工作正常。该协会也起作用。但是当我添加继承时,Lending中的lendingLines为空。我也无法通过继承从数据库获得任何LendingLines。

有谁能够帮助我?

(对不起,不好的解释)

提前致谢!

LendingLine:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="TYPE")
@DiscriminatorValue(value="Line")
@Table(name = "LendingLine")
public class LendingLine {
...
public LendingLine(){}
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER, targetEntity=Lending.class)
@JoinColumn(name = "LendingId")
private Lending lending;
...
Run Code Online (Sandbox Code Playgroud)

借出:

@Entity
@Table(name = "Lending")
public class Lending {
...
public Lending(){}

    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER, mappedBy = "lending")
private List<LendingLine> lendingLines;
...
Run Code Online (Sandbox Code Playgroud)

BlockDate:

@Entity
@DiscriminatorValue(value = "BlockLine")
public class BlockLine extends LendingLine {
public BlockLine(){
}
}
Run Code Online (Sandbox Code Playgroud)

LendingLineRepository:

此类仅从db读取,因为db是由另一个将对象添加到db的应用程序(C#)创建的。

public class LendingLineRepository extends JpaUtil implement …
Run Code Online (Sandbox Code Playgroud)

java inheritance jpa many-to-one

3
推荐指数
1
解决办法
3665
查看次数

标签 统计

jpa ×2

entity ×1

inheritance ×1

java ×1

many-to-one ×1

mappedsuperclass ×1