[英]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: 根据您的需要选择您的超类类型:
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.
查询时,它只返回自身的实例而没有子类。
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 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 - 实体继承教程
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.