Home » Java » java – JPA, Hibernate + HikariCP – Query#getResultList = null-Exceptionshub

java – JPA, Hibernate + HikariCP – Query#getResultList = null-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’ve created the following entity:

@Entity
@Table(name = "anima_players")
@NamedQueries({
        @NamedQuery(name = "AnimaPlayer.findAll", query = "SELECT p FROM AnimaPlayer AS p"),
        @NamedQuery(name = "AnimaPlayer.findByUuid", query = "SELECT p FROM AnimaPlayer p WHERE p.uuid = :uuid"),
})
public class AnimaPlayer {

    @Id
    private UUID uuid;
    private String name;

    /*@OneToOne
    @JoinColumn(name = "rank")
    private AnimaRank rank;

    @OneToMany(mappedBy = "player", cascade = CascadeType.ALL)
    private List<Punishment> punishments;*/

    public UUID getUuid() {
        return uuid;
    }

    public AnimaPlayer setUuid(UUID uuid) {
        this.uuid = uuid;
        return this;
    }

    public String getName() {
        return name;
    }

    public AnimaPlayer setName(String name) {
        this.name = name;
        return this;
    }

    /*public AnimaRank getRank() {
        return rank;
    }

    public AnimaPlayer setRank(AnimaRank rank) {
        this.rank = rank;
        return this;
    }

    public List<Punishment> getPunishments() {
        return punishments;
    }

    public AnimaPlayer setPunishments(List<Punishment> punishments) {
        this.punishments = punishments;
        return this;
    }*/

    public boolean hasPermission(String permission) {
        return false;
        //return getRank().getPermissions().contains(permission);
    }

    @Override
    public String toString() {
        return "AnimaPlayer{" +
                "uuid=" + uuid +
                ", name='" + name + '\'' +
                //", rank=" + rank +
                //", punishments=" + punishments +
                '}';
    }
}

(I have commented out some fields to ensure that the error does not come from incorrect mappings)

Now I created a simple method to retrieve a user by its uuid:

public Optional<AnimaPlayer> findByUuid(UUID uuid) {
    final TypedQuery<AnimaPlayer> query = entityManager.createNamedQuery("AnimaPlayer.findByUuid", AnimaPlayer.class)
            .setParameter("uuid", uuid);
    List<AnimaPlayer> players = query.getResultList();
    return players.stream().findFirst();
}

But getResultList() throws a NullPointerException, and I have absolutly no clue why:

Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.query.sqm.sql.internal.BasicValuedPathInterpretation.from(BasicValuedPathInterpretation.java:50)
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitBasicValuedPath(BaseSqmToSqlAstConverter.java:747)
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitBasicValuedPath(BaseSqmToSqlAstConverter.java:158)
    at org.hibernate.query.sqm.tree.domain.SqmBasicValuedSimplePath.accept(SqmBasicValuedSimplePath.java:87)
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitComparisonPredicate(BaseSqmToSqlAstConverter.java:1545)
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitComparisonPredicate(BaseSqmToSqlAstConverter.java:158)
    at org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate.accept(SqmComparisonPredicate.java:67)
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQuerySpec(BaseSqmToSqlAstConverter.java:364)
    at org.hibernate.query.sqm.sql.internal.StandardSqmSelectTranslator.visitSelectStatement(StandardSqmSelectTranslator.java:169)
    at org.hibernate.query.sqm.sql.internal.StandardSqmSelectTranslator.translate(StandardSqmSelectTranslator.java:111)
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.buildCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:224)
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.resolveCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:191)
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:153)
    at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:413)
    at org.hibernate.query.spi.AbstractQuery.list(AbstractQuery.java:1356)
    at org.hibernate.query.Query.getResultList(Query.java:136)
    at de.pierreschwang.anima.core.database.PlayerRepository.findByUuid(PlayerRepository.java:22)
    at de.pierreschwang.anima.core.Anima.<init>(Anima.java:39)
    at de.pierreschwang.anima.core.bootstrap.AnimaBootstrap.main(AnimaBootstrap.java:8)

The hibernate library I’m using:

<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-hikaricp</artifactId>
    <version>6.0.0.Alpha4</version>
    <scope>compile</scope>
</dependency>

(HikariCP for connection pooling)

Thanks in advance!

// UPDATE

I’ve discovered that my findByName Query and Method works perfect, but its kinda the same logic. So I guess it has something to do with the UUID type or binary column type?

public Optional<AnimaPlayer> findByName(String name) {
    final TypedQuery<AnimaPlayer> query = entityManager.createNamedQuery("AnimaPlayer.findByName", AnimaPlayer.class)
        .setParameter("name", name);
    List<AnimaPlayer> players = query.getResultList();
    return players.stream().findFirst();
}

// The Query
@NamedQuery(name = "AnimaPlayer.findByName", query = "SELECT p FROM AnimaPlayer p WHERE p.name = :name")
How to&Answers: