简体   繁体   English

来自superClass的JPA OneToMany协会

[英]JPA OneToMany Association from superClass

I'm trying to map the inheritance from the superclass LendingLine and the subclasses Line and BlockLine. 我正在尝试映射超类LendingLine和子类Line和BlockLine的继承。 LendingLine has an ManyToOne association with Lending. LendingLine与Lending有一个ManyToOne关联。

When I try to get the LendingLines from the database without the inheritance it works fine. 当我尝试从数据库中获取LendingLines而没有继承时,它可以正常工作。 The association works also. 该协会也有效。 But when i add the inheritance, lendingLines in Lending is empty. 但是当我添加继承时,Lending中的lendingLines是空的。 I also can't get any LendingLines from the DB with the inheritance. 我也无法通过继承从DB获取任何LendingLines。

Can anybody help me? 有谁能够帮我?

(Sorry for the bad explanation) (抱歉,不好的解释)

Thanks in advance! 提前致谢!

LendingLine: 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;
...

Lending: 贷款:

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

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

BlockDate: BlockDate:

@Entity
@DiscriminatorValue(value = "BlockLine")
public class BlockLine extends LendingLine {
public BlockLine(){
}
}

LendingLineRepository: LendingLineRepository:

This class only reads from the db because the db was created by another application ( C#) where the objects are added to the db. 此类仅从db读取,因为db是由另一个将对象添加到db的应用程序(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 LendingLine: 表LendingLine:

在此输入图像描述

Choose your type of superclass according to your needs: 根据您的需要选择您的超类类型:

Concrete Class 混凝土类

public class SomeClass {}

Define your superclass as a concrete class, when you want to query it and when you use a new operator for further logic. 当您想要查询它时以及使用new运算符进一步逻辑时,将您的超类定义为具体类。 You will always be able to persist it directly. 你将永远能够直接坚持它。 In the discriminator column this entity has it's own name. 在鉴别器列中,该实体具有自己的名称。 When querying it, it returns just instances of itself and no subclasses. 查询时,它只返回自身的实例而没有子类。

Abstract Class 抽象类

public abstract class SomeClass {}

Define your superclass as an abstract class when you want to query it, but don't actually use a new operator, because all logic handled is done by it's subclasses. 当您想要查询它时,将您的超类定义为抽象类,但实际上并不使用new运算符,因为所有处理的逻辑都是由它的子类完成的。 Those classes are usually persisted by its subclasses but can still be persisted directly. 这些类通常由其子类保留,但仍可以直接保留。 U can predefine abstract methods which any subclass will have to implement (almost like an interface). U可以预定义任何子类必须实现的抽象方法(几乎像一个接口)。 In the discriminator column this entity won't have a name. 在鉴别器列中,该实体将没有名称。 When querying it, it returns itself with all subclasses, but without the additional defined information of those. 查询时,它会返回所有子类,但没有其他已定义的信息。

MappedSuperclass MappedSuperclass

@MappedSuperclass public abstract class SomeClass {}

A superclass with the interface @MappedSuperclass cannot be queried. 无法查询具有@MappedSuperclass接口的超类。 It provides predefined logic to all it's subclasses. 它为其所有子类提供预定义逻辑。 This acts just like an interface. 这就像一个界面。 You won't be able to persist a mapped superclass. 您将无法持久保存映射的超类。

For further information: JavaEE 7 - Entity Inheritance Tutorial 有关详细信息,请参阅: JavaEE 7 - 实体继承教程


Original message 原始信息

Your SuperClass LendingLine needs to define a @DiscriminatorValue as well, since it can be instantiated and u use an existing db-sheme, where this should be defined. 你的SuperClass LendingLine需要定义一个@DiscriminatorValue ,因为它可以被实例化并使用现有的db-sheme,这应该被定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @Formula 从 spring 数据 jpa 中的 @OneToMany 关联中计算 object - @Formula to count object from a @OneToMany association in spring data jpa 单向@OneToMany 关联未通过 JPA 中的相等性测试 - Unidirectional @OneToMany association fails equality test in JPA 与 JPA OneToMany 映射的只读关联 - Read-only association with JPA OneToMany mapping 双向 JPA OneToMany/ManyToOne 关联中的“关联的反面”是什么? - What is “the inverse side of the association” in a bidirectional JPA OneToMany/ManyToOne association? JPA @OneToMany - Set vs List - 使用 Set 时无法从双向关联中删除一个子实体 - JPA @OneToMany - Set vs List - Not able to delete one child entity from bidirectional association when using Set JPA / JDO的OneToMany对象化 - OneToMany from JPA/JDO to Objectify JPA-使用中间表在同一张表之间映射OneToMany关联 - JPA - Mapping OneToMany association between the same table using an intermediate table 为什么 @OneToMany 在 JPA 与 springboot 中与 @JoinColumn 的单向关联会产生额外的查询 - why @OneToMany Unidirectional association with @JoinColumn in JPA with springboot generates extra queries JPA - 从OneToMany关系中删除子项 - JPA - delete a child from OneToMany relationship JPA-从查询中强制转换@OneToMany字段 - JPA - Casting @OneToMany field from Query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM