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