Hibernate 产生损坏的查询
Hibernate produces broken query
我有一个通过 Hibernate 映射到数据库 table 的实体:
@Entity
@NoArgsConstructor
@Getter
@Setter
@FieldNameConstants
public class Set extends AbstractEntity {
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name = "owner_id")
private User owner;
private LocalDate date;
private String password;
@Enumerated(EnumType.STRING)
private SetStatus status;
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@Fetch(value = FetchMode.SUBSELECT)
@JoinTable(name = "set_user", joinColumns = @JoinColumn(name = "set_id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
private List<User> users;
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@Fetch(value = FetchMode.SUBSELECT)
@JoinTable(name = "cocktail_set", joinColumns = @JoinColumn(name = "set_id"), inverseJoinColumns = @JoinColumn(name = "cocktail_id"))
private List<Cocktail> cocktails;
}
并在此 Spring 服务中完成映射:
@Service
public class SetService {
@Autowired
private SetRepository setRepository;
public Page<Set> findAll(HttpSession session, SetSearch search, Pageable pageable) {
Specification<Set> specification = SetSpecifications.empty();
UUID id = (UUID)session.getAttribute("userId");
UserRole role = (UserRole)session.getAttribute("userRole");
if (role == UserRole.BARMEN)
specification = specification.and(SetSpecifications.setsForBarmen(id));
else if (role == UserRole.USER)
specification = specification.and(SetSpecifications.setsForUser(id));
if (search.getDate() != null)
specification = specification.and(SetSpecifications.dateEquals(search));
if (search.getStatus() != null)
specification = specification.and(SetSpecifications.statusEquals(search));
Sort sortByDate = Sort.by(Sort.Direction.DESC, "date");
return setRepository.findAll(specification, ((PageRequest)pageable).withSort(sortByDate));
}
}
但是,当我向该服务发送请求时,Hibernate 会生成一个查询,该查询会导致 SQLSyntaxErrorException 和一个很长的堆栈跟踪。
查询是:
select
set0_.id as id1_5_,
set0_.date as date2_5_,
set0_.owner_id as owner_id5_5_,
set0_.password as password3_5_,
set0_.status as status4_5_
from
set
set0_
where
1=1
order by
set0_.date desc limit ?
异常消息以:
开头
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set set0_ where 1=1 order by set0_.date desc limit 10' at line 1
除了 from 之后的这个愚蠢的换行符外,查询看起来完全没问题,从异常消息来看,这似乎是原因。在我看来,Hibernate 这次真的拉屎了。
我想得到一个答案,严格到第i点。 e.提供了一个可行的解决方案。如果你没有,就跳过这个,不要浪费你的时间写,我的时间看。
Table 名称设置导致您出现问题。 set
是 MySQL 的保留关键字。看看 MySQL documentation
我有一个通过 Hibernate 映射到数据库 table 的实体:
@Entity
@NoArgsConstructor
@Getter
@Setter
@FieldNameConstants
public class Set extends AbstractEntity {
@ManyToOne(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name = "owner_id")
private User owner;
private LocalDate date;
private String password;
@Enumerated(EnumType.STRING)
private SetStatus status;
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@Fetch(value = FetchMode.SUBSELECT)
@JoinTable(name = "set_user", joinColumns = @JoinColumn(name = "set_id"), inverseJoinColumns = @JoinColumn(name = "user_id"))
private List<User> users;
@ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@Fetch(value = FetchMode.SUBSELECT)
@JoinTable(name = "cocktail_set", joinColumns = @JoinColumn(name = "set_id"), inverseJoinColumns = @JoinColumn(name = "cocktail_id"))
private List<Cocktail> cocktails;
}
并在此 Spring 服务中完成映射:
@Service
public class SetService {
@Autowired
private SetRepository setRepository;
public Page<Set> findAll(HttpSession session, SetSearch search, Pageable pageable) {
Specification<Set> specification = SetSpecifications.empty();
UUID id = (UUID)session.getAttribute("userId");
UserRole role = (UserRole)session.getAttribute("userRole");
if (role == UserRole.BARMEN)
specification = specification.and(SetSpecifications.setsForBarmen(id));
else if (role == UserRole.USER)
specification = specification.and(SetSpecifications.setsForUser(id));
if (search.getDate() != null)
specification = specification.and(SetSpecifications.dateEquals(search));
if (search.getStatus() != null)
specification = specification.and(SetSpecifications.statusEquals(search));
Sort sortByDate = Sort.by(Sort.Direction.DESC, "date");
return setRepository.findAll(specification, ((PageRequest)pageable).withSort(sortByDate));
}
}
但是,当我向该服务发送请求时,Hibernate 会生成一个查询,该查询会导致 SQLSyntaxErrorException 和一个很长的堆栈跟踪。
查询是:
select
set0_.id as id1_5_,
set0_.date as date2_5_,
set0_.owner_id as owner_id5_5_,
set0_.password as password3_5_,
set0_.status as status4_5_
from
set
set0_
where
1=1
order by
set0_.date desc limit ?
异常消息以:
开头You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set set0_ where 1=1 order by set0_.date desc limit 10' at line 1
除了 from 之后的这个愚蠢的换行符外,查询看起来完全没问题,从异常消息来看,这似乎是原因。在我看来,Hibernate 这次真的拉屎了。
我想得到一个答案,严格到第i点。 e.提供了一个可行的解决方案。如果你没有,就跳过这个,不要浪费你的时间写,我的时间看。
Table 名称设置导致您出现问题。 set
是 MySQL 的保留关键字。看看 MySQL documentation