简体   繁体   中英

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

I have an Entity class as seen below

NB: The checkNumber is unique.

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.

I want the EmployeeWorkHours object to be able to reference it's employee directly like in normal hibernate relationships.

How can I go about this, any help will be much appreciated. Thank you.

On your EmployeeWorkHours entity you need a OneToOne relationship with Employee entity

@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:

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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