使用Spring数据&启动1.3.0.BUILD-SNAPSHOT与MySQL我有一个父母&孩子的关系:
父:
@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;
....
}
子:
@Entity
public class CustomerResult {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, optional = false)
@JoinColumn(name = "customerSearchResultsId", nullable = false)
private CustomerSearchResults customerSearchResults;
....
}
父级可以有多个相同子类型的列表。我没有在这个或任何其他成功的@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?
答案 0 :(得分:2)
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
仅用于模式生成,它在插入数据时不做任何事情。您需要在关系的两端设置属性,因此您的&#39; anArrayList&#39;中的每个条目都是如此。必须设置刚刚创建的customerSearchResults。