Shpytyack Artem
Shpytyack Artem

Reputation: 306

How to get child data of an entity without loading the entity in Hibernate

I have entities User and Report

class User {
    @JoinColumn(name = "user_id")
    protected Set<BattleReport> reports;
}

class Report {
    byte data;
}

I know user id and I need to update report data for a User without loading user. How to do it?

I've tried this way, but it doesn't work:

Query reportsQuery = session.createQuery("SELECT reports FROM User WHERE id = :id");
    reportsQuery.setParameter("id", id);
    List<?> list = reportsQuery.list();

    for (Object o : list) {
        Report report = (Report) o;
        report.setData(42);
        session.update(report);
    }

Upvotes: 0

Views: 167

Answers (3)

Shpytyack Artem
Shpytyack Artem

Reputation: 306

The correct answer is:

SELECT u.reports FROM User u WHERE u.id = :id

Upvotes: 0

Nuno Neto
Nuno Neto

Reputation: 303

Have you tried doing a Criteria query on the Report table with the user_id as a constraint? I don't know youyr database relationship or fields and, as such, can't give you a real example, but something as follow can be userfull perhaps:

Criteria Example

Upvotes: 0

Bohemian
Bohemian

Reputation: 425053

Match on the id of the user of the report:

Query reportsQuery = session.createQuery(
    "FROM Report r WHERE r.user.id = :id");

Upvotes: 1

Related Questions