使用Java中的休眠模式将mySQL存储过程映射到多个实体

问题描述

我在MysqL数据库中有下面的存储过程:

    create
    definer = root@localhost procedure GetAllAvailableTentsAndTimeSlots()
BEGIN
    select *from tentslots
        left join timeslots t on t.PM = tentslots.timeslots_fk
        left join tents t2 on tentslots.tent_id_fk = t2.tent_id
    where TENT_BOOKING_ID is null;
END;

我的实体是: Tentslots.class

import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.persistence.*;

@Entity
@ApiModel
@Table(name = "tentslots")
public class Tentslots implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "TENT_SLOT_ID")
    @ApiModelProperty(hidden = true)
    private Long tentSlotId;

    @Column(name = "timeslots_fk")
    @ApiModelProperty(value = "The timeslot id")
    private Integer timeSlotIdFk;

    @Column(name = "tent_id_fk")
    @ApiModelProperty(value = "The tent id")
    private Integer tentIdFk;

    @Column(name = "TENT_BOOKING_ID")
    @ApiModelProperty(value = "The tent booking id")
    private Integer tentBookingId;

    public Long getTentSlotId() {
        return tentSlotId;
    }

    public void setTentSlotId(Long tentSlotId) {
        this.tentSlotId = tentSlotId;
    }


    public Integer getTimeSlotIdFk() {
        return timeSlotIdFk;
    }

    public void setTimeSlotIdFk(Integer slotId) {
        this.timeSlotIdFk = slotId;
    }


    public Integer getTentIdFk() {
        return tentIdFk;
    }

    public void setTentIdFk(Integer tentIdFk) {
        this.tentIdFk = tentIdFk;
    }


    public Integer getTentBookingId() {
        return tentBookingId;
    }

    public void setTentBookingId(Integer tentBookingId) {
        this.tentBookingId = tentBookingId;
    }
}

Tent.class

import java.util.List;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.persistence.*;

@Entity
@ApiModel
@Table(name = "tents")
public class Tent {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "tent_id")
    @ApiModelProperty(hidden = true)
    private Integer tentId;

    @Column(name = "LATITUDE")
    @ApiModelProperty(value = "The Latitude of a tent")
    private Long latitude;

    @Column(name = "LONGITUDE")
    @ApiModelProperty(value = "The longitude of a tent")
    private Long longitude;

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

    @OnetoMany (targetEntity = Tentslots.class,cascade = CascadeType.ALL)
    @JoinColumn(name = "tent_id_fk",referencedColumnName = "tent_id")
    private List<Tentslots> tentslots;

    public int getTentId() {
            return tentId;
    }
   

    public void setTentId(int tentId) {
        this.tentId = tentId;
    }

    public Long getLatitude() {
        return latitude;
    }

    public void setLatitude(Long latitude) {
        this.latitude = latitude;
    }

    public Long getLongitude() {
        return longitude;
    }

    public void setLongitude(Long longitude) {
        this.longitude = longitude;
    }

    public String getFriendlyName() {
        return friendlyName;
    }

    public void setFriendlyName(String friendlyName) {
        this.friendlyName = friendlyName;
    }

    public List<Tentslots> getTentslots() {
        return tentslots;
    }

    public void setTentslots(List<Tentslots> tentslots) {
        this.tentslots = tentslots;
    }
}

Timeslots.class

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;


import javax.persistence.*;

import java.util.Date;

    @Entity
    @ApiModel
    @Table(name = "timeslots")
    public class Timeslots {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "PM")
        @ApiModelProperty(hidden = true)
        private Integer pm;
    
        @Column(name = "TIMESLOT")
        @ApiModelProperty(value = "The Latitude of a tent")
        private Date timeslot;
    
        public Integer getPm() {
            return pm;
        }
    
        public void setPm(Integer pm) {
            this.pm = pm;
        }
    
        public Date getTimeslot() {
            return timeslot;
        }
    
        public void setTimeslot(Date timeslot) {
            this.timeslot = timeslot;
        }

然后我从TentslotsRepository.class调用存储过程

import com.stavros.camping.entity.Tentslots;
import org.springframework.data.jpa.repository.query.Procedure;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

import javax.transaction.Transactional;

public interface TentslotsRepository extends CrudRepository<Tentslots,Long> {

    List<Tentslots> findAllByTentBookingIdisNull();

    List<Tentslots> findAllByTentSlotId(Integer slotId);

    List<Tentslots> findAllByTentSlotIdAndAndTentBookingIdisNull(Integer slotId);

    List<Tentslots> findAllByTentSlotIdAndAndTentBookingIdisNotNull(Integer slotId);

    @Procedure
    @Transactional
    void GetAllAvailableTentsAndTimeSlots();
}

我的问题是,如何在三个不同的Entity对象中映射从过程(3个联接表)返回的ResultSet。甚至有可能还是我应该创建一个新的对象(投影)。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)