【发布时间】:2019-05-05 02:34:18
【问题描述】:
@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;
}
引导类
@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);
}
}
当我在我的类 Lawyer 上进行新的约会时,我试图将 de 数据从 Lawyer 传播到 Client,但我只能将其传递给 Appointment。从约会到客户,我无法传播它....我收到此错误:
原因:java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: 对象引用了未保存的瞬态实例 - 在刷新之前保存瞬态实例
如何从 Appointment 传播到 Client ? 我已经看过一些关于这类案例的文章,但我仍然没有理解。
【问题讨论】:
-
能贴出create-persist代码吗?
-
@EugenCovaci 我添加了有关我的代码的更多信息。我不明白你的第二个问题,但我的目标是创建一个单一的存储库(LawyerRepoI),允许我一次存储所有传播的数据
-
什么是
m1、m2? -
misstyped...l1 和 l2 将律师及其相关信息保存在我的存储库中
-
方法
addAppointment在Appointment中声明但在Lawyer中使用?!?
标签: java spring spring-boot spring-data-jpa