0
嗨,我寫了下面的Criteria-API查詢,由於多選,創建一個損壞的sql-select語句。如果我取消註釋multiselect查詢按預期工作,但事情是我不想擁有所有數據。在我的門戶對象中有幾種關係,並且在我目前的情況下加載它們都是絕對沒有必要的。休眠打亂了我的查詢
的方法是這樣的:
@Override
public Optional<Portal> loadPortalData(long clientId)
{
log.trace("loading data for client with id '{}'", clientId);
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Portal> criteriaQuery = criteriaBuilder.createQuery(Portal.class);
Root<Portal> root = criteriaQuery.from(Portal.class);
criteriaQuery.multiselect(root.get(Portal_.codes),
root.get(Portal_.certificate))
.where(criteriaBuilder.equal(root.get(Portal_.id), clientId));
try
{
return Optional.of(entityManager.createQuery(criteriaQuery).getSingleResult());
}
catch (NoResultException noResult)
{
return Optional.empty();
}
}
和破碎的查詢看起來是這樣的:
30 Mai 2017 07:12:56,305 [main] TRACE mypackage.repository.PortalDaoImpl (PortalDaoImpl.java:39) - loading data for client with id '1'
Hibernate:
select
. as col_0_0_,
portal0_.certificate as col_1_0_
from
portal portal0_
inner join
Code authorisat1_
on portal0_.id=authorisat1_.client_id
inner join
certificate certificat2_
on portal0_.certificate=certificat2_.id
where
portal0_.id=1
任何建議,爲什麼,如果我使用多選Hibernate是搞亂了我這樣的查詢?
編輯:
@Entity
@Table(name = "portal")
public class Portal
{
...
@Valid
@OneToMany(cascade = {CascadeType.ALL}, fetch = FetchType.EAGER, mappedBy = "client")
private Set<Code> codes = new HashSet<>();
@Valid
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "certificate", referencedColumnName = "id")
private Certificate certificate;
...
}
和代碼級
@Entity
public class Code
{
@Id
@GeneratedValue
private long id;
@NotNull
@Column(nullable = false, unique = true)
private String code;
@NotNull
@ManyToOne(fetch = FetchType.EAGER, targetEntity = Portal.class)
@JoinColumn(name = "client_id", referencedColumnName = "id", nullable = false)
private Portal client;
@NotNull
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
private Date creation_time;
@Column(nullable = false)
private int expires_in;
...
}
門戶實體的代碼是什麼?爲什麼不使用JPQL進行這樣的靜態查詢? –
爲什麼門戶網站的代碼很重要? JPQL產生相同的錯誤 – Goldfish
嘿,你是需要幫助的人。投票結束,因爲你不想提供所需的信息讓我幫你免費。 –