Я создал следующую сущность:
@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 +
'}';
}
}
(я закомментировал некоторые поля, чтобы убедиться, что ошибка не возникает из-за неправильных сопоставлений)
Теперь я создал простую метод извлечения пользователя по его 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();
}
Но getResultList () генерирует исключение NullPointerException, и я абсолютно не понимаю, почему:
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)
Библиотека гибернации, которую я использую :
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-hikaricp</artifactId>
<version>6.0.0.Alpha4</version>
<scope>compile</scope>
</dependency>
(HikariCP для пула соединений)
Заранее спасибо!
// ОБНОВЛЕНИЕ
Я обнаружил, что мой запрос findByName и Метод работает идеально, но это своего рода логика c. Так что я думаю, это как-то связано с типом UUID или типом двоичного столбца?
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")