如何在没有关系的情况下连接多个表

How to join multiple tables without relationship

这是我在 Java/Hibernate 中的第一个项目。

我正在尝试连接两个表,但即使搜索了很多,我也找不到合适的解决方案来实现我想要的。

让我向您解释一下我目前拥有的东西。

我有两个实体:

@Entity
@Table(name = "admin")
public class AdminLogin {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String firstName;
    private String lastName;
    private String username;
    private String password;
    private String status;
    private String userRole;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUserRole() {
        return userRole;
    }

    public void setUserRole(String userRole) {
        this.userRole = userRole;
    }
}


@Entity
@Table(name = "user_balance_log")
public class UserBalanceLog {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private int userId;
    private int adminId;
    private Float balance;
    private String balanceType;
    private String message;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateCreated;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUserId() {
        return userId;
    }

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

    public int getAdminId() {
        return adminId;
    }

    public void setAdminId(int adminId) {
        this.adminId = adminId;
    }

    public Float getBalance() {
        return balance;
    }

    public void setBalance(Float balance) {
        this.balance = balance;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

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

    public String getBalanceType() {
        return balanceType;
    }

    public void setBalanceType(String balanceType) {
        this.balanceType = balanceType;
    }
}

我想将 adminId 从用户余额日志实体加入到管理员登录实体。

这是我目前拥有的。

Session session = this.sessionFactory.getCurrentSession();

Criteria criteria = session.createCriteria(UserBalanceLog.class, "ubl");

criteria.setProjection(Projections.projectionList().add(Projections.property("balance"),"balance").add(Projections.property("balanceType"),"balanceType").add(Projections.property("message"),"message").add(Projections.property("dateCreated"),"dateCreated"));
criteria.addOrder(Order.desc("id"));       

if (parameters.get("userId") != null) {
    criteria.add(Restrictions.eq("userId", new Integer(parameters.get("userId"))));
}

if (parameters.get("balanceType") != null) {
    criteria.add(Restrictions.eq("balanceType", parameters.get("balanceType")));
}

if (parameters.get("dateCreated") != null) {
    criteria.add(Restrictions.eq("dateCreated", parameters.get("dateCreated")));
}
List userBalanceList = criteria.list();

请指导我。

谢谢

最后我当然是用@vc73 的 help/guidance 弄明白了。

这是我必须在模型中做的更新:

@Entity
@Table(name = "user_balance_log")
public class UserBalanceLog {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private int userId;

    @ManyToOne
    @JoinColumn(name="adminId")
    private AdminLogin adminId;

    private Float balance;
    private String balanceType;
    private String message;
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dateCreated;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUserId() {
        return userId;
    }

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

    public AdminLogin getAdminId() {
        return adminId;
    }

    public void setAdminId(AdminLogin adminId) {
        this.adminId = adminId;
    }

    public Float getBalance() {
        return balance;
    }

    public void setBalance(Float balance) {
        this.balance = balance;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

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

    public String getBalanceType() {
        return balanceType;
    }

    public void setBalanceType(String balanceType) {
        this.balanceType = balanceType;
    }
}

在 DAO 中:

Session session = this.sessionFactory.getCurrentSession();

Criteria criteria = session.createCriteria(UserBalanceLog.class, "ubl");
criteria.createAlias("ubl.adminId", "a");

criteria.setProjection(Projections.projectionList().add(Projections.property("balance").as("balance")).add(Projections.property("balanceType").as("balanceType")).add(Projections.property("message").as("message")).add(Projections.property("dateCreated").as("dateCreated")).add(Projections.property("a.username").as("username")));
criteria.addOrder(Order.desc("id"));

if (parameters.get("userId") != null) {
    criteria.add(Restrictions.eq("userId", new Integer(parameters.get("userId"))));
}

if (parameters.get("balanceType") != null) {
    criteria.add(Restrictions.eq("balanceType", parameters.get("balanceType")));
}

if (parameters.get("dateCreated") != null) {
    criteria.add(Restrictions.eq("dateCreated", parameters.get("dateCreated")));
}

criteria.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

criteria.setFirstResult(start);
criteria.setMaxResults(Pagination.limitPerPage);
List userBalanceList = criteria.list();

希望对其他人有所帮助。