Hibernate spring数据jpa@OneToMany子id未更新
使用Spring data&boot 1.3.0.BUILD-SNAPSHOT和MySQL,我有一个父子关系: 家长:Hibernate spring数据jpa@OneToMany子id未更新,hibernate,spring-data-jpa,Hibernate,Spring Data Jpa,使用Spring data&boot 1.3.0.BUILD-SNAPSHOT和MySQL,我有一个父子关系: 家长: @Entity public class CustomerSearchResults { @Id @GeneratedValue(strategy = GenerationType.AUTO) public Long customerSearchResultsId; @OneToMany(mappedBy = "customerSearchResul
@Entity
public class CustomerSearchResults {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long customerSearchResultsId;
@OneToMany(mappedBy = "customerSearchResults", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<CustomerResult> customerAAAAList;
@OneToMany(mappedBy = "customerSearchResults", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<CustomerResult> customerBBBBList;
@OneToMany(mappedBy = "customerSearchResults", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<CustomerResult> customerCCCCList;
....
}
父级可以有多个相同子类型的列表。我没有在此或任何其他成功的@ManyToOne示例中定义任何外键
我创建的结果如下所示:
CustomerSearchResults customerSearchResults = new CustomerSearchResults();
customerSearchResults.setCustomerAAAAList(anArrayList);
customerSearchResults.setCustomerBBBBList(anArrayList);
customerSearchResults.setCustomerCCCCCList(anArrayList);
customerResultsRepository.save(customerSearchResults);
没有抛出错误,我将CustomerSearchResults插入到该表中,并将正确的CustomerResult记录插入到该表中。唯一的问题是CustomerResults.customerSearchResultsId为null,尽管它被声明为非null
以下是我如何通过liquibase创建表:
<createTable tableName="CustomerSearchResults">
<column name="customerSearchResultsId" type="bigint" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
...
</createTable>
<createTable tableName="CustomerResult">
<column name="id" type="bigint" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
...
<column name="customerSearchResultsId" type="bigint"/>
</createTable>
...
...
为什么子字段中的ref-id customerSearchResultsId不能像这样填充?
nullable=false
仅用于模式生成,在插入数据时不执行任何操作。您需要在关系的两端设置属性,因此“anArrayList”中的每个条目都必须设置您刚刚创建的customerSearchResults。我在父类的setter上添加了代码,以迭代正在添加的子类列表,并手动将customerSearchResults设置为“this”。检查数据库中的记录时,我在CustomerResults中看到了正确的CustomerSearchResults.customerSearchResultsId。customerSearchResultsId字段。这似乎解决了问题,但这是正确的,还是我正在建立一个巨大的依赖关系树或其他副作用?另一种解决方法是逐个添加数组,而不是一次添加整个数组。然后您可以使用一个方法lilke:public void addCustomerAareSult(CustomerResult CustomerResult){CustomerAaaList.add(CustomerResult);CustomerResult.setCustomerSearchResults(this);}
<createTable tableName="CustomerSearchResults">
<column name="customerSearchResultsId" type="bigint" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
...
</createTable>
<createTable tableName="CustomerResult">
<column name="id" type="bigint" autoIncrement="true">
<constraints primaryKey="true"/>
</column>
...
<column name="customerSearchResultsId" type="bigint"/>
</createTable>