problem using embeddables with inheritance in hibernate quries

1,075 views
Skip to first unread message

Lutz Strobel

unread,
Jul 30, 2014, 7:33:35 AM7/30/14
to quer...@googlegroups.com
I have an entity containing an embeddable like this:

@Entity
@Table(name = "region_name")
public class RegionName implements KeyedValue{

    private Integer id;
    private RegionKey regionKey;
    private String name;

    @Id
    @Column(name = "id", nullable = false, insertable = true, updatable = true)
    public Integer getId() {
        return id;
    }

    @Embedded
    public RegionKey getRegionKey() {
        return regionKey;
    }
    @Basic
    @Column(name = "name", nullable = false, insertable = true, updatable = true, length = 100)
    public String getName() {
        return name;
    }
...
}

and the class RegionKey like this:

@Embeddable
public class RegionKey extends CountryKey {

    protected Integer regionId;
...

    @Basic
    @Column(name = "region_id", nullable = true, insertable = true, updatable = true)
    public Integer getRegionId() {
        return regionId;
    }
 ...
}

and the CountryKey class like this:

@Embeddable
public class CountryKey implements Key {
    private Integer countryId;

...

    @Basic
    @Column(name = "country_id", nullable = false, insertable = true, updatable = true)
    public Integer getCountryId() {
        return countryId;
    }
...
}

All of the Q-classes are well generated. But If I try to find all RegionNames for a given country the  I create an hibernate query like this:

    public Stream<RegionName> allRegionsForCountry(CountryKey countryKey) {
        QRegionName qRegionName = QRegionName.regionName;
        HibernateQuery hq = new HibernateQuery(getSessionFactory().getCurrentSession());
        return hq.from(qRegionName).where(qRegionName.regionKey.countryId.eq(countryKey.getCountryId())).list(qRegionName).stream();
    }


and get the following error :

org.hibernate.QueryException: could not resolve property: regionKey.countryId of: de.lstrobel.js_eval.geo.model.RegionName [select regionName
from de.lstrobel.js_eval.geo.model.RegionName regionName
where regionName.regionKey.countryId = ?1]

Can anybody give me a hint how to get rid of this?
Thanx
Lutz


Lutz Strobel

unread,
Jul 30, 2014, 9:09:54 AM7/30/14
to quer...@googlegroups.com
Problem is solved.

Hibernate and JPA too do not support inheritance in embeddable classes.

So the embeddables must be implemented not as isA but as hasA (RegionKey has a CountryKey.

timowest

unread,
Jul 30, 2014, 12:54:54 PM7/30/14
to quer...@googlegroups.com
Hi Lutz.

Good you got it solved. Inheritance mapping is indeed only available for entities.

Br
Timo

Sasha Gorelikov

unread,
Aug 17, 2015, 3:48:04 AM8/17/15
to Querydsl
Hi!
I have same problem, but without inheritance.
SQL string is
from invoice i where i.key.id=1 instead from invoice i where i.id=1
here key is composite key.
Any suggestions?
Reply all
Reply to author
Forward
0 new messages