gpt4 book ai didi

java - 在 Hibernate 中通过 ManyToOne 关系获取 Order

转载 作者:行者123 更新时间:2023-12-02 06:30:51 27 4
gpt4 key购买 nike

我有 2 张 table

1.用户

2.公司

每个用户都有一个公司。对于每个公司,它可以有多个用户。

UserBean.java

import java.io.Serializable;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "tab_user")
public class UserBean implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "user_id")
private int user_id;
@Column(name="user_login_pwd")
private String user_login_pwd;


@ManyToOne
@JoinColumn(name="comp_id")
private CompanyBean companyBean

我的 CompanyBean 是

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "tab_company")

public class CompanyBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "comp_id")
private Integer comp_id;
@Column(name = "comp_code")
private String comp_code;
@OneToMany(mappedBy = "companyBean" , fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@Fetch(value = FetchMode.SUBSELECT)
private List<UserBean> companyUserList;

这是我的 CompanyBean 映射类。

现在我需要显示按“comp_code”排序的用户列表。我的用户列表 DAO 实现是

@SuppressWarnings("unchecked")
@Override
public List<UserBean> getUserList( String orderBy, String orderField) throws Exception{
List<UserBean> userList = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try{
Criteria crit = session.createCriteria(UserBean.class);

if(orderBy.equals("asc")){
crit.addOrder(Order.asc(orderField));
}else{
crit.addOrder(Order.desc(orderField));
}

crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

userList = crit.list();
}finally{
session.close();
}
return userList;
}

那么我如何通过usersList中CompanyBean的comp_code获取订单呢?请帮忙。

最佳答案

没有 Hibernate 解决方案是创建自己的 Comparator 并使用 Collections.sort 进行排序;

Hibernate 解决方案使用 @OrderBy 注释。 Using hibernate with annotations, i want a one-many relationship to be sorted

关于java - 在 Hibernate 中通过 ManyToOne 关系获取 Order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20069173/

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