Question

I have this situation where I map

@OneToMany
@OrderBy(value="id desc")
@JoinTable(name="EntityOne",
joinColumns = @JoinColumn(name="EntityTwo_id", referencedColumnName="id"), inverseJoinColumns = @JoinColumn(name="EntityThree_id", referencedColumnName="id"))
private List<EntityThree> samples = Lists.newArrayList();

This works fine, but, I was wondering if there's a way to map this to a sub-entity of EntityThree. For example, to get

 private List<SubEntity> samples = Lists.newArrayList();

where, SubEntity is got from EntityThree.getSubEntity basically, subEntity exists in EntityThree

Thanks in advance for the help, more than eager to clarify myself if am not clear.

Était-ce utile?

La solution

The fields mapped in the entity are supposed to match with what you really have in the database tables. It seems you have an association between the above entity and EntityThree, but not between this entity and SubEntity. So this non-existing association at the DB level should not exist at the entity level either.

But your entities may have methods, that can navigate into the graph of entities:

public List<SubEntity> getSubEntities() {
    List<SubEntity> result = Lists.newArrayListWithExpectedSize(samples.size());
    for (EntityThree sample : samples) {
        result.add(sample.getSubEntity());
    }
    return result;
}

or, in a functional way:

public Iterable<SubEntity> getSubEntities() {
    return Iterables.transform(samples, new Function<EntityThree, SubEntity>() {
        @Override
        public SubEntity apply(EntityThree input) {
            return input.getSubEntity();
        }
    });
}

You may also use a query:

select sub from MyEntity m 
inner join m.samples sample 
inner join sample.subEntity sub
where m.id = :theId
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top