@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString
public class Lawyer extends ID{
@EqualsAndHashCode.Exclude
@ToString.Exclude
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "lawyer")
private Set<Appointment> appointments = new HashSet<>();
public void addAppointment(Client client, LocalDateTime data) {
Appointment app = new Appointment (client,this,data);
this.consultas.add(app);
app.getClient().getAppointments().add(app);
}
}
@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString
public class Appointment extends ID{
@EqualsAndHashCode.Exclude
@ToString.Exclude
@ManyToOne
private Client client;
@EqualsAndHashCode.Exclude
@ToString.Exclude
@ManyToOne
private Lawyer lawyer;
}
@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString
public class Client extends ID{
@EqualsAndHashCode.Exclude
@ToString.Exclude
@JsonIgnore
@OneToMany
private Set<Appointment> appointments = new HashSet<>();
}
@MappedSuperclass
@Getter
@Setter
@NoArgsConstructor
public class ID{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
BootStrap Class
@Component
public class Bootstrap implements ApplicationListener<ContextRefreshedEvent> {
private LaywerRepoI LaywerService;
public Bootstrap(LaywerRepoI LaywerService) {
this.LaywerService = LaywerService;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
Client c1 = new Client("Lukz", LocalDate.of(1971, 11, 26));
Client c2 = new Client("Adrian", LocalDate.of(1956, 01, 28));
Client c3 = new Client("Danny", LocalDate.of(1936, 1, 11));
Laywer l1 = new Laywer("Morgan", LocalDate.of(1941, 1, 1));
Laywer l2 = new Laywer("Ana", LocalDate.of(1931, 10, 1));
l1.addAppointment(c1,LocalDateTime.of(2018, 11, 22,18, 25));
l1.addAppointment(c1,LocalDateTime.of(2018, 11, 22, 10, 15));
LawyerService.save(l1);
LawyerService.save(l2);
}
}
When I'm making a new Appointment on my class Lawyer I'm trying to propagate de data from Lawyer to Client, but I can only reach it to Appointment. From Appointment to Client, I can't propagate it.... I get this error:
Caused by: java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing
How do I propagate from Appointment to Client ? I already read some articles about this types of cases, but still I havent understood them.