我正在使用spring和hibernate JPA。当我尝试保存实体时,额外的insert语句将与正常值一起执行。 我使用以下实体:
@Entity
@Table(name = "search_filters")
public class SearchFilter {
@Id
@GeneratedValue
@Column(name = "search_filter_id")
private int id;
@ManyToOne(optional = true, cascade = CascadeType.ALL)
@JoinColumn(name = "search_criteria_id",nullable=false)
private SearchCriteria searchCriteria;
@Column(name = "field_name")
private String fieldName;
@Column(name = "field_value")
private String fieldValue;
@Column(name = "filter_condition")
public FilterCondition filterCondition;
@Column(name = "time_stamp")
private Timestamp timestamp;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldValue() {
return fieldValue;
}
public void setFieldValue(String fieldValue) {
this.fieldValue = fieldValue;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
public FilterCondition getFilterCondition() {
return filterCondition;
}
public void setFilterCondition(FilterCondition filterCondition) {
this.filterCondition = filterCondition;
}
public SearchCriteria getSearchCriteria() {
return searchCriteria;
}
public void setSearchCriteria(SearchCriteria searchCriteria) {
this.searchCriteria = searchCriteria;
}
}
SearchCriteria.java
@Entity
@Table(name = "search_criteria")
public class SearchCriteria {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "search_criteria_id")
private int id;
@ManyToOne
@JoinColumn(name = "user_id", referencedColumnName = "user_id")
private User user;
@ManyToOne
@JoinColumn(name = "entity_id", referencedColumnName = "entity_id")
private EntityField entityField;
@Column(name = "group_condition")
private GroupCondition groupCondition;
@Column(name = "operator")
private String operator;
@Column(name = "time_stamp")
private Timestamp timestamp;
@OneToMany(cascade=CascadeType.ALL, mappedBy="searchCriteria", orphanRemoval=true, fetch = FetchType.LAZY)
private List<SearchFilter> searchFilters;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
public List<SearchFilter> getSearchFilters() {
return searchFilters;
}
public void setSearchFilters(List<SearchFilter> searchFilters) {
this.searchFilters = searchFilters;
}
public GroupCondition getGroupCondition() {
return groupCondition;
}
public void setGroupCondition(GroupCondition groupCondition) {
this.groupCondition = groupCondition;
}
public EntityField getEntityField() {
return entityField;
}
public void setEntityField(EntityField entityField) {
this.entityField = entityField;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
我的服务类:
public SearchCriteria saveSearchCriteria(SearchCriteriaBean searchBean) {
SearchCriteria searchCriteria = searchCriteriaTransformer.transform(searchBean);
System.out.println("searchCriteria:::::::::::::::: "+searchCriteria.getId());
//searchCriteria = searchCriteriaRepo.findOne(searchCriteria.getId());
return searchCriteriaRepo.save(searchCriteria);
}
结果: 执行后我得到以下结果
> **Hibernate: insert into search_criteria (entity_id, group_condition,
> operator, time_stamp, user_id) values (?, ?, ?, ?, ?)**
Hibernate: select entityfiel_.entity_field_id, entityfiel_.class_field_name as class_fi2_4_, entityfiel_.class_name as class_na3_4_, entityfiel_.entity_id as entity_12_4_, entityfiel_.field_desc as field_de4_4_, entityfiel_.field_name as field_na5_4_, entityfiel_.list_order as list_ord6_4_, entityfiel_.listable as listable7_4_, entityfiel_.search_order as search_o8_4_, entityfiel_.searchable as searchab9_4_, entityfiel_.summary as summary10_4_, entityfiel_.summary_order as summary11_4_ from entity_fields_mst entityfiel_ where entityfiel_.entity_field_id=?
Hibernate: insert into search_criteria (entity_id, group_condition, operator, time_stamp, user_id) values (?, ?, ?, ?, ?)
Hibernate: insert into search_filters (field_name, field_value, filter_condition, search_criteria_id, time_stamp) values (?, ?, ?, ?, ?)
Hibernate: insert into search_filters (field_name, field_value, filter_condition, search_criteria_id, time_stamp) values (?, ?, ?, ?, ?)
Hibernate: insert into search_filters (field_name, field_value, filter_condition, search_criteria_id, time_stamp) values (?, ?, ?, ?, ?)
我不知道为什么在开始时执行额外的insert语句?请帮忙