来自 superClass 的 JPA OneToMany 关联
JPA OneToMany Association from superClass
我正在尝试映射从 superclass LendingLine 和 subclasses Line 和 BlockLine 的继承。 LendingLine 与 Lending 有 ManyToOne 关联。
当我尝试在没有继承的情况下从数据库中获取 LendingLines 时,它工作正常。该协会也有效。但是当我添加继承时,Lending 中的 lendingLines 是空的。我也无法通过继承从数据库中获取任何 LendingLines。
有人可以帮助我吗?
(不好意思解释)
提前致谢!
借贷行:
@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;
...
贷款:
@Entity
@Table(name = "Lending")
public class Lending {
...
public Lending(){}
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER, mappedBy = "lending")
private List<LendingLine> lendingLines;
...
封锁日期:
@Entity
@DiscriminatorValue(value = "BlockLine")
public class BlockLine extends LendingLine {
public BlockLine(){
}
}
LendingLineRepository:
此 class 仅从数据库中读取,因为数据库是由另一个应用程序 (C#) 创建的,其中对象被添加到数据库中。
public class LendingLineRepository extends JpaUtil implement LendingLineRepositoryInterface {
@Override
protected Class getEntity() {
return LendingLine.class;
}
@Override
public Collection<LendingLine> findAll() {
Query query = getEm().createQuery("SELECT l FROM LendingLine l");
System.out.println(query.getResultList().size());
return (Collection<LendingLine>) query.getResultList();
}
Table 贷款行:
根据需要选择超级类型class:
混凝土Class
public class SomeClass {}
将您的 superclass 定义为一个具体的 class,当您想要查询它时以及当您使用 new
运算符进行进一步逻辑时。你总是可以直接坚持下去。在鉴别器列中,该实体有自己的名称。查询它时,它 returns 只是它自己的实例,没有子 class。
摘要Class
public abstract class SomeClass {}
当你想查询它时,将你的 superclass 定义为抽象 class,但实际上不要使用 new
运算符,因为所有处理的逻辑都是由它完成的子classes。那些 classes 通常由其子 classes 持久化,但仍然可以直接持久化。你可以预定义任何 subclass 必须实现的抽象方法(几乎像一个接口)。在鉴别器列中,该实体没有名称。查询时,它returns本身和所有子class,但没有那些的附加定义信息。
MappedSuperclass
@MappedSuperclass
public abstract class SomeClass {}
无法查询接口为@MappedSuperclass
的superclass。它为其所有子class 提供预定义的逻辑。这就像一个接口。您将无法保留映射的 superclass.
更多信息:JavaEE 7 - Entity Inheritance Tutorial
原始消息
您的 SuperClass LendingLine
也需要定义一个 @DiscriminatorValue
,因为它可以实例化并且您使用现有的 db-sheme,其中应该定义。
我正在尝试映射从 superclass LendingLine 和 subclasses Line 和 BlockLine 的继承。 LendingLine 与 Lending 有 ManyToOne 关联。
当我尝试在没有继承的情况下从数据库中获取 LendingLines 时,它工作正常。该协会也有效。但是当我添加继承时,Lending 中的 lendingLines 是空的。我也无法通过继承从数据库中获取任何 LendingLines。
有人可以帮助我吗?
(不好意思解释)
提前致谢!
借贷行:
@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;
...
贷款:
@Entity
@Table(name = "Lending")
public class Lending {
...
public Lending(){}
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER, mappedBy = "lending")
private List<LendingLine> lendingLines;
...
封锁日期:
@Entity
@DiscriminatorValue(value = "BlockLine")
public class BlockLine extends LendingLine {
public BlockLine(){
}
}
LendingLineRepository:
此 class 仅从数据库中读取,因为数据库是由另一个应用程序 (C#) 创建的,其中对象被添加到数据库中。
public class LendingLineRepository extends JpaUtil implement LendingLineRepositoryInterface {
@Override
protected Class getEntity() {
return LendingLine.class;
}
@Override
public Collection<LendingLine> findAll() {
Query query = getEm().createQuery("SELECT l FROM LendingLine l");
System.out.println(query.getResultList().size());
return (Collection<LendingLine>) query.getResultList();
}
Table 贷款行:
根据需要选择超级类型class:
混凝土Class
public class SomeClass {}
将您的 superclass 定义为一个具体的 class,当您想要查询它时以及当您使用 new
运算符进行进一步逻辑时。你总是可以直接坚持下去。在鉴别器列中,该实体有自己的名称。查询它时,它 returns 只是它自己的实例,没有子 class。
摘要Class
public abstract class SomeClass {}
当你想查询它时,将你的 superclass 定义为抽象 class,但实际上不要使用 new
运算符,因为所有处理的逻辑都是由它完成的子classes。那些 classes 通常由其子 classes 持久化,但仍然可以直接持久化。你可以预定义任何 subclass 必须实现的抽象方法(几乎像一个接口)。在鉴别器列中,该实体没有名称。查询时,它returns本身和所有子class,但没有那些的附加定义信息。
MappedSuperclass
@MappedSuperclass
public abstract class SomeClass {}
无法查询接口为@MappedSuperclass
的superclass。它为其所有子class 提供预定义的逻辑。这就像一个接口。您将无法保留映射的 superclass.
更多信息:JavaEE 7 - Entity Inheritance Tutorial
原始消息
您的 SuperClass LendingLine
也需要定义一个 @DiscriminatorValue
,因为它可以实例化并且您使用现有的 db-sheme,其中应该定义。