简体   繁体   English

如何将存储过程的结果 map 存储到 spring-boot/hibernate 中的实体

[英]How to map the results of a stored procedure to an entity in spring-boot/hibernate

I have an Entity class as seen below我有一个实体 class 如下所示

NB: The checkNumber is unique.注意:checkNumber 是唯一的。

package tz.go.ega.biometic.entity;


import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import lombok.ToString;
import org.hibernate.annotations.NaturalId;
import org.hibernate.validator.constraints.SafeHtml;
import org.springframework.data.annotation.Transient;
import org.springframework.format.annotation.DateTimeFormat;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "employee", uniqueConstraints = {
        @UniqueConstraint(columnNames = {"check_number"})
})
@Data
@AllArgsConstructor
@ToString
@NoArgsConstructor
public class Employee implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Long id;

    @Basic(optional = true)
    //@NotEmpty(message = "Please enter first name")
    @Column(name = "first_name")
    private String firstName;

    @Basic(optional = true)
    //@NotEmpty(message = "Please enter middle name")
    @Column(name = "middle_name")
    private String middleName;

    @Basic(optional = true)
    //@NotEmpty(message = "Please enter last name")
    @Column(name = "last_name")
    private String lastName;

    private String status;

    @Basic(optional = true)
    // @Pattern(regexp ="^[a-zA-Z0-9_]*$",message = "{field.validation.voteCode}")
    // @SafeHtml(message = "{field.validation.voteCode}")
    @Column(name = "vote_code", length = 50)
    private String voteCode;

    @NaturalId
    @Basic(optional = true)
    //@NotNull(message = "Please enter check number")
    @Column(name = "check_number")
    private long checkNumber;

    private Boolean isActive = true;


    @Basic(optional = false)
    @Column(name = "created_at", updatable = false)
    @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private LocalDateTime createdAt = LocalDateTime.now();

    @Column(name = "updated_at")
    @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private LocalDateTime updatedAt = LocalDateTime.now();


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

I then have a stored procedure that calculates each employee's working hours then returns the results as seen below.然后我有一个存储过程来计算每个员工的工作时间,然后返回结果,如下所示。

+--------------+-------+
| checkNumber  | Time  |
+--------------+-------+
| 1122334455   | 29893 |
| 1234567890   | 15427 |
| 2233445566   | 19745 |
| 6655443322   | 12578 |
+--------------+-------+

What I am trying to achieve is, to map the results ( as seen above ) of the stored procedure to an entity (let's call it EmployeeWorkHours ) and then create a relationship between this Entity and the Employee entity using the checkNumber.我想要实现的是,map 将存储过程的结果(如上所示)存储到一个实体(我们称之为 EmployeeWorkHours ),然后使用 checkNumber 在该实体和 Employee 实体之间创建关系。

I want the EmployeeWorkHours object to be able to reference it's employee directly like in normal hibernate relationships.我希望 EmployeeWorkHours object 能够像在正常的 hibernate 关系中一样直接引用它的员工。

How can I go about this, any help will be much appreciated.我怎么能 go 关于这个,任何帮助将不胜感激。 Thank you.谢谢你。

On your EmployeeWorkHours entity you need a OneToOne relationship with Employee entity在您的 EmployeeWorkHours 实体上,您需要与 Employee 实体建立 OneToOne 关系

@OneToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "checkNumber", unique = true, nullable = false)
private Employee employee;

In your repository you can write a sql query like this:在您的存储库中,您可以编写一个 sql 查询,如下所示:

@Query(value = "{CALL yourStoredProcedure (:var1, :var2, ..., :varn)}", nativeQuery = true)
int getWorkHours(@Param("var1") String var1, @Param("var2") String var2,...,
        @Param("varn") String varn);

And then in your service layer you will just call this method do what else you want and persist it.然后在您的服务层中,您只需调用此方法做您想做的其他事情并将其持久化。 Hope it helps希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM