我有JPA加入的问题。正如数据库查询这样的工作:
SELECT *
FROM votingstats
INNER JOIN enginename ON enginename.id = votingstats.engine_name_id
WHERE engine_name LIKE 'zz'
AND type_of_game_used_by_that_engine LIKE 'yy'
AND vote_pack_name = 'xx'
然而,在Java JPA中,我遇到了一个问题,因为工作查询看起来像这样,并且工作正常:
CriteriaQuery<VotingStats> criteria=
criteriaBuilder.createQuery(VotingStats.class);
Root<VotingStats> currentElo = criteria.from(VotingStats.class);
Join<VotingStats, EngineName> join = currentElo.join("engineNameId");
CriteriaQuery<VotingStats> eloJoinQuerry = criteria
.where(
criteriaBuilder.and(
criteriaBuilder.equal(
join.get("engineName"), engineName),
criteriaBuilder.equal(
join.get("typeOfGameUsedByThatEngine"), typeOfGame.getTypeOfGame())
)
);
但是,在添加votePackName
后,当我尝试引用VotingStats
实体时它会中断:
CriteriaQuery<VotingStats> criteria=
criteriaBuilder.createQuery(VotingStats.class);
Root<VotingStats> currentElo = criteria.from(VotingStats.class);
Join<VotingStats, EngineName> join = currentElo.join("engineNameId");
CriteriaQuery<VotingStats> eloJoinQuerry = criteria
.where(
criteriaBuilder.and(
criteriaBuilder.equal(
join.get("engineName"), engineName),
criteriaBuilder.equal(
join.get("typeOfGameUsedByThatEngine"), typeOfGame.getTypeOfGame()),
criteriaBuilder.equal(
join.get("votePackName"), votePackName)
)
);
实体看起来像这样(但如果是普通连接,它们可以正常工作):
@Entity
@Data
public class VotingStats {
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="votingstats_seq")
@SequenceGenerator(name="votingstats_seq", sequenceName="votingStats_id_seq", allocationSize=1)
@Access(AccessType.PROPERTY)
private Integer id;
@Basic
@Column(name = "vote_pack_name")
private String votePackName;
@Basic
@Column(name = "vote_amount")
private Integer voteAmount;
@ManyToOne(cascade = CascadeType.REFRESH)
@JoinColumn(name = "engine_name_id")
private EngineName engineNameId;
}
和第二个实体是:
@Entity
@Data
public class EngineName {
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="engineName_seq")
@SequenceGenerator(name="engineName_seq", sequenceName="enginename_id_seq", allocationSize=1)
@Access(AccessType.PROPERTY)
private Integer id;
@Basic
@Column(name = "engine_name")
private String engineName;
@Basic
@Column(name = "type_of_game_used_by_that_engine")
private String typeOfGameUsedByThatEngine;
}
这是我得到的错误:
java.lang.IllegalArgumentException:无法在此ManagedType上找到具有给定名称[votePackName]的Attribute [chess.database.entities.EngineName]
at org.hibernate.metamodel.internal.AbstractManagedType.checkNotNull(AbstractManagedType.java:128)
在org.hibernate.metamodel.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:113)
at org.hibernate.query.criteria.internal.path.AbstractFromImpl.locateAttributeInternal(AbstractFromImpl.java:116)
在org.hibernate.query.criteria.internal.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:204)
在org.hibernate.query.criteria.internal.path.AbstractPathImpl.get(AbstractPathImpl.java:177)
at chess.database.dao.VotingStatsDAO.findByEngineName(VotingStatsDAO.java:32)