来自superClass的JPA OneToMany协会 [英] JPA OneToMany Association from superClass

查看:123
本文介绍了来自superClass的JPA OneToMany协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m trying to map the inheritance from the superclass LendingLine and the subclasses Line and BlockLine. LendingLine has an ManyToOne association with Lending.

当我尝试从没有继承的数据库中获取LendingLines时,它可以正常工作。该协会也有效。但是当我添加继承时,Lending中的lendingLines是空的。我也无法从继承的DB中获取任何LendingLines。

When I try to get the LendingLines from the database without the inheritance it works fine. The association works also. But when i add the inheritance, lendingLines in Lending is empty. I also can't get any LendingLines from the DB with the inheritance.

任何人都可以帮助我吗?

Can anybody help me?

(对不起,不好解释)

提前致谢!

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

贷款:

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

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

BlockDate:

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

LendingLineRepository:

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

This class only reads from the db because the db was created by another application ( C#) where the objects are added to the db.

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();
}

表LendingLine:

推荐答案

选择您的类型超类根据您的需要:

Choose your type of superclass according to your needs:

公共类SomeClass {}

当你要查询它时以及当你使用 new 运算符用于进一步的逻辑。您将始终能够直接保留它。在鉴别器列中,该实体具有自己的名称。查询时,它只返回自身的实例而没有子类。

Define your superclass as a concrete class, when you want to query it and when you use a new operator for further logic. 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.

公共抽象类SomeClass {}

当你要查询它时,将你的超类定义为抽象类,但实际上并没有使用它一个 new 运算符,因为所有处理的逻辑都是由它的子类完成的。这些类通常由其子类保留,但仍可以直接保留。 U可以预定义任何子类必须实现的抽象方法(几乎像一个接口)。在鉴别器列中,该实体将没有名称。查询时,它会返回所有子类,但没有其他定义的信息。

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. 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). 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
公共抽象类SomeClass {}

接口<$ c $的超类c> @MappedSuperclass 无法查询。它为其所有子类提供预定义逻辑。这就像一个界面。您将无法持久保存映射的超类。

A superclass with the interface @MappedSuperclass cannot be queried. 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.

有关详细信息: JavaEE 7 - 实体继承教程

您的SuperClass LendingLine 需要定义 @DiscriminatorValue 同样,因为它可以被实例化并使用现有的db-sheme,这应该被定义。

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的JPA OneToMany协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆