0

When I am saving the entity the foreign key in the child table is coming as null

the parent class is Appointment consisting child class slotdetails mapped with one to many relationships

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

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

please help

AzizShaw
  • 91
  • 1
  • 4
  • 10

1 Answers1

0

You haven't specified @JoinColumn for appointment in SlotDetails

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="appoinment_id", nullable = false)
private Appoinment appoinment;
JavaLearner
  • 527
  • 1
  • 5
  • 16
  • tried that but not working, I followed this blog https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/ in this I tried to do Bidirectional @OneToMany given there – AzizShaw Feb 21 '21 at 08:32
  • That's seems weird. Can you paste the code where you build the entity and call save? – JavaLearner Feb 21 '21 at 08:46
  • here are the classes - https://github.com/spb722/testing – AzizShaw Feb 21 '21 at 09:35