简体   繁体   English

外键在 springboot jpa 一对多中以 null 的形式出现

[英]foreign key coming as null in springboot jpa one to many

When I am saving the entity the foreign key in the child table is coming as null当我保存实体时,子表中的外键是 null

the parent class is Appointment consisting child class slotdetails mapped with one to many relationships父 class 是由子 class slotdetails 映射到一对多关系的约会

package com.inskade.inkflow.appoinments;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@Entity()
@Table(name = "appoinment_detail")

public class Appointment  {

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

    @CreationTimestamp
    @Column(name = "created_at", insertable = true, updatable = false)
    private Date createdAt;
    @UpdateTimestamp
    private Date updatedAt;


    
    private String userId;
    private LocalDate appointmentDate;

    
      @OneToMany(
                mappedBy = "appoinment",
                cascade = CascadeType.ALL,
                orphanRemoval = true
            )
            private List<SlotDetails> slotDetails = new ArrayList<>();
      
        public void addSlots(SlotDetails slot) {
            slotDetails.add(slot);
            slot.setAppoinment(this);
        }
     
        public void removeSlot(SlotDetails slot) {
            slotDetails.remove(slot);
            slot.setAppoinment(null);
        }
     
    }
    

The child class is Slotdetails having many to one relationship with parent Appoinment子 class 是与父约会具有多对一关系的 Slotdetails

import java.sql.Time;
import java.util.UUID;

import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Data
@Entity
public class SlotDetails {
    
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long slotId;
     @ManyToOne(fetch = FetchType.LAZY)
     
        private Appoinment appoinment;
     private Time appointmentStartTime;
        private Time appointmentEndTime;
    private String bookingStatus
    private String bookedClientId;
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SlotDetails )) return false;
        return slotId != null && slotId.equals(((SlotDetails) o).getSlotId());
    }
 
    @Override
    public int hashCode() {
        return getClass().hashCode();
    }

}

the repository存储库

import java.time.LocalDate;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface AppoinmentsRepository extends JpaRepository<Appoinment, Long> {


    List<Appoinment> findAllByAppointmentDateBetween(LocalDate startDate, LocalDate endDate);

}

The table Information of the child shows孩子的表格信息显示

FOREIGN KEY (`appoinment_id`) REFERENCES `appoinment_detail` (`id`)

here the appoinment_id in the child table is coming as null when I call appoinmentRepository.save, in the parent table everything is fine当我调用 appoinmentRepository.save 时,子表中的 appoinment_id 以 null 的形式出现,在父表中一切都很好

please help请帮忙

You haven't specified @JoinColumn for appointment in SlotDetails您尚未在SlotDetails中指定@JoinColumn进行appointment

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="appoinment_id", nullable = false)
private Appoinment appoinment;

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

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