当我尝试运行简单测试时,我收到此错误:
org.springframework.dao.InvalidDataAccessApiUsageException:传递给persist的分离实体:com.Company;嵌套异常是org.hibernate.PersistentObjectException:传递给persist的分离实体:com.Company
我在SpringData和Hibernate中使用H2和Postgre数据库。
以下是我的实体:
@Entity
@Proxy(lazy = false)
public class Invoice {
@ElementCollection(fetch = FetchType.EAGER)
private List<InvoiceEntry> products = new ArrayList<>();
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "ID", unique = true, nullable = false)
private Long id;
private String name;
@JoinColumn
@ManyToOne(cascade = CascadeType.ALL)
private Company buyer;
@JoinColumn
@ManyToOne(cascade = CascadeType.ALL)
private Company seller;
公司课程:
@Entity
@Proxy(lazy = false)
public class Company{
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
private Long id;
private String name;
private LocalDate issueDate;
private String address;
private String city;
private String zipCode;
private String nip;
private String bankAccoutNumber;
简单测试如下:
@Test
public void shouldAddAndGetSingleInvoice() {
//given
long invoiceId = database.addEntry(testInvoice);
//when
String output = mapper.toJson(database.getEntryById(invoiceId));
String expected = mapper.toJson(testInvoice);
//then
assertThat(output, is(equalTo(expected)));
}
尝试将级联类型更改为Merge但它不起作用。我会很感激任何建议。