休眠查询 "or condition" 将不起作用
Hibernate query "or condition" won't work
我正在尝试以下休眠查询,但它总是给我一个错误的结果:
@SuppressWarnings("unchecked")
@Override
public List<Jcelulasmin> getAllOthers(int id, String username) {
Session session = this.sessionFactory.getCurrentSession();
List<Jcelulasmin> JcelulasList = session.createQuery("from Jcelulasmin where jgrelhas.id=? and (jconcorrentes is null or jconcorrentes.users.username <> ?) order by id").setParameter(0, id).setString(1, username).list();
for(Jcelulasmin p : JcelulasList){
logger.info("Jcelulas List::"+p);
}
return JcelulasList;
}
这个查询 return 给我 13 个值,但这应该是 "jconcorrentes.users.username <> ?"
的结果
"jconcorrentes is null" 的结果是 47 个值,所以我的查询应该 return 47+13=60...
我正在尝试实现以下 SQL 查询,实际上 returns 60 个值:
SELECT * FROM `jcelulas` WHERE GrelhasId=1 and (ConcorrentesId is null or ConcorrentesId<>1)
ps:jconcorrentes.id 是 1 所以 sql 和 hql 应该是一样的
Table/Entitiy定义:
@Entity
@Table(name = "jconcorrentes", catalog = "7jogos")
public class Jconcorrentes implements java.io.Serializable {
private Integer id;
....
private Users users;
@ManyToOne(fetch = FetchType.EAGER)
@JsonBackReference
@JoinColumn(name = "username", unique = true, nullable = false)
public Users getUsers() {
return this.users;
}
public void setUsers(Users users) {
this.users = users;
}
我尝试了以下方法并且确实有效:
session.createQuery("from Jcelulasmin where jgrelhas.id=? and (jconcorrentes is null or jconcorrentes <> 1) order by id").setParameter(0, id).list();
唯一的问题是我必须通过用户中的用户名来获取它。
用户:
@Entity
@Table(name = "users", catalog = "7jogos", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
public class Users implements java.io.Serializable {
@NotEmpty(message="Não se esqueça do Email")
@Email(message="Email Inválido")
private String username;
...
private Set<Jconcorrentes> jconcorrenteses = new HashSet<Jconcorrentes>(0);
@OneToMany(fetch = FetchType.EAGER, mappedBy = "users")
public Set<Jconcorrentes> getJconcorrenteses() {
return this.jconcorrenteses;
}
public void setJconcorrenteses(Set<Jconcorrentes> jconcorrenteses) {
this.jconcorrenteses = jconcorrenteses;
}
也许你应该尝试 jconcorrentes.id <> 1
.
而不是 jconcorrentes is null
您当前的查询转换为 Jcelulasmin 和 jconcorrentes 之间的内部连接(因为 jconcorrentes.users.username
),因此排除了空值。你必须明确地离开加入他们:
select j from Jcelulasmin j left join j.jconcorrentes jcon ...
我正在尝试以下休眠查询,但它总是给我一个错误的结果:
@SuppressWarnings("unchecked")
@Override
public List<Jcelulasmin> getAllOthers(int id, String username) {
Session session = this.sessionFactory.getCurrentSession();
List<Jcelulasmin> JcelulasList = session.createQuery("from Jcelulasmin where jgrelhas.id=? and (jconcorrentes is null or jconcorrentes.users.username <> ?) order by id").setParameter(0, id).setString(1, username).list();
for(Jcelulasmin p : JcelulasList){
logger.info("Jcelulas List::"+p);
}
return JcelulasList;
}
这个查询 return 给我 13 个值,但这应该是 "jconcorrentes.users.username <> ?"
的结果"jconcorrentes is null" 的结果是 47 个值,所以我的查询应该 return 47+13=60...
我正在尝试实现以下 SQL 查询,实际上 returns 60 个值:
SELECT * FROM `jcelulas` WHERE GrelhasId=1 and (ConcorrentesId is null or ConcorrentesId<>1)
ps:jconcorrentes.id 是 1 所以 sql 和 hql 应该是一样的
Table/Entitiy定义:
@Entity
@Table(name = "jconcorrentes", catalog = "7jogos")
public class Jconcorrentes implements java.io.Serializable {
private Integer id;
....
private Users users;
@ManyToOne(fetch = FetchType.EAGER)
@JsonBackReference
@JoinColumn(name = "username", unique = true, nullable = false)
public Users getUsers() {
return this.users;
}
public void setUsers(Users users) {
this.users = users;
}
我尝试了以下方法并且确实有效:
session.createQuery("from Jcelulasmin where jgrelhas.id=? and (jconcorrentes is null or jconcorrentes <> 1) order by id").setParameter(0, id).list();
唯一的问题是我必须通过用户中的用户名来获取它。
用户:
@Entity
@Table(name = "users", catalog = "7jogos", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
public class Users implements java.io.Serializable {
@NotEmpty(message="Não se esqueça do Email")
@Email(message="Email Inválido")
private String username;
...
private Set<Jconcorrentes> jconcorrenteses = new HashSet<Jconcorrentes>(0);
@OneToMany(fetch = FetchType.EAGER, mappedBy = "users")
public Set<Jconcorrentes> getJconcorrenteses() {
return this.jconcorrenteses;
}
public void setJconcorrenteses(Set<Jconcorrentes> jconcorrenteses) {
this.jconcorrenteses = jconcorrenteses;
}
也许你应该尝试 jconcorrentes.id <> 1
.
jconcorrentes is null
您当前的查询转换为 Jcelulasmin 和 jconcorrentes 之间的内部连接(因为 jconcorrentes.users.username
),因此排除了空值。你必须明确地离开加入他们:
select j from Jcelulasmin j left join j.jconcorrentes jcon ...