gpt4 book ai didi

java - Hibernate条件查询不遵守多重限制

转载 作者:行者123 更新时间:2023-12-01 12:23:09 25 4
gpt4 key购买 nike

我正在尝试使用条件查询 2 个表。方法如下:

   public UserEntity getUserOverView(long userId, String receiptMonth) {

Session session = HibernateUtil.getSessionFactory().openSession();
UserEntity user = new UserEntity();

try {
session.beginTransaction();

Criteria criteria = session.createCriteria(UserEntity.class, "user")
.createAlias("user.receiptEntitySet", "receipt")
.add(Restrictions.eq("receipt.dateCreated", receiptMonth))
.add(Restrictions.eq("user.userId", userId));


user = (UserEntity) criteria.uniqueResult();

} catch (Exception ex) {

System.out.print(ex.getMessage());
} finally {

session.getTransaction().commit();
session.close();

}

return user;

}

上面成功地尊重了 userId,但是,我想过滤另一个表中的“receiptMonth”。它似乎完全忽略了“receiptMonth”限制。

用户实体:

    package za.co.skizzel.infrastructure.entities;

import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name="user")
public class UserEntity {

@Id
@GeneratedValue
@Column(name="userId", unique = true, nullable = false, updatable = false )
private Long userId;

@Column(name="email")
private String email;

@Column(name="password")
private String password;

@Column(name="name")
private String name;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "userEntity" , cascade = { CascadeType.ALL } )
private Set<ReceiptEntity> receiptEntitySet;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "userEntity" , cascade = { CascadeType.ALL } )
private Set<CategoryEntity> categoryEntitySet;

public Set<CategoryEntity> getCategoryEntitySet() {
return categoryEntitySet;
}

public void setCategoryEntitySet(Set<CategoryEntity> categoryEntitySet) {
this.categoryEntitySet = categoryEntitySet;
}

public Long getUserId() {
return userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Set<ReceiptEntity> getReceiptEntitySet() {
return receiptEntitySet;
}

public void setReceiptEntitySet(Set<ReceiptEntity> receiptEntitySet) {
this.receiptEntitySet = receiptEntitySet;
}

public UserEntity(){};

public UserEntity(long userId){
this.userId = userId;
};

}

收据实体:

    package za.co.skizzel.infrastructure.entities;


import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name="receipt")
public class ReceiptEntity {

@Id
@GeneratedValue
@Column(name="ReceiptId", unique = true, nullable = false, updatable = false )
private Long receiptId;

@Column(name="alias")
private String alias;

@Column(name="categoryId")
private long categoryId;

@Column(name="DateCreated")
private String dateCreated;

@ManyToOne
@JoinColumn(name="userId", nullable=false, insertable = false, updatable = false)
private UserEntity userEntity;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "receiptEntity" , cascade = { CascadeType.ALL } )
private Set<ImageEntity> imageEntitySet;

private long userId;

public long getUserId() {
return userId;
}

public void setUserId(long userId) {
this.userId = userId;
}

public long getCategoryId() {
return categoryId;
}

public void setCategoryId(long categoryId) {
this.categoryId = categoryId;
}

public Long getReceiptId() {
return receiptId;
}

public void setReceiptId(Long receiptId) {
this.receiptId = receiptId;
}

public String getAlias() {
return alias;
}

public void setAlias(String alias) {
this.alias = alias;
}

public String getDateCreated() {
return dateCreated;
}

public void setDateCreated(String dateCreated) {
this.dateCreated = dateCreated;
}

public UserEntity getUserEntity() {
return userEntity;
}

public void setUserEntity(UserEntity userEntity) {
this.userEntity = userEntity;
}

public ReceiptEntity() {};

}

不确定我做错了什么?任何指导将不胜感激?

谢谢卢克

编辑

我期待类似的事情:

select * from user, receipt where user.userId = '8' and receipt.dateCreated = 'January 2014'

最佳答案

这有效吗?

      Criteria criteria = session.createCriteria(UserEntity.class)
.createAlias("receiptEntitySet", "receipt")
.add(Restrictions.eq("receipt.dateCreated", receiptMonth))
.add(Restrictions.eq("userId", userId));

编辑

此查询将返回与条件匹配的整体 UserEntity。例如,如果您有一个 userEntity,在 receiptEntitySet 中有两个 ReceiptEntity,其中一个具有 dateCreated =receiptMonth ,因此您将获得 userEntity 以及 receiptEntitySet 中的两个收据实体。该userEntity 将是附加实体(绑定(bind)到Hibernate session )并将表示该对象的数据库状态,其中包括两个接收实体。

您的选项取决于您的用例,但通常您可以将用户和收据保存在单独的字段中,您可以通过循环来填充收据

List<ReceiptEntity> filteredReceipts = new ArrayList<ReceiptEntity>();
for (ReceiptEntity receipt : user.getReceiptEntitySet()) {
if (receipt.getDateCreated().equals(receiptMonth) {
filteredReceipts.add(receipt);
}
}

或者,创建一个查询来获取它们

Criteria criteria = session.createCriteria(ReceiptEntity.class)
.createAlias("user", "user")
.add(Restrictions.eq("dateCreated", receiptMonth))
.add(Restrictions.eq("user.userId", userId));

希望这有帮助。

关于java - Hibernate条件查询不遵守多重限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26525487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com