Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA OneToMany Association from superClass

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

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?

(Sorry for the bad explanation)

Thanks in advance!

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:

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

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.

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:

enter image description here

like image 445
Ben Gabriëls Avatar asked Jan 07 '23 04:01

Ben Gabriëls


1 Answers

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

@MappedSuperclass public abstract class SomeClass {}

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.

For further information: JavaEE 7 - Entity Inheritance Tutorial


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.

like image 50
XiCoN JFS Avatar answered Jan 08 '23 17:01

XiCoN JFS