SchoolDto :
@Id
@Column(name = "c_sm_npk_id", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "school_sequence")
@SequenceGenerator(name = "school_sequence", sequenceName = "t_school_master_c_sm_npk_id_seq", initialValue = 1, allocationSize = 1)
private Long id;
@Column(name="c_sm_vnm_name")
private String name;
@ManyToOne(cascade = {CascadeType.MERGE},fetch=FetchType.LAZY)
UserDto owner;
/*Many other fileds omit for simplicity with getter/setter methods*/
UserDto
public class UserDto implements Serializable {
private static final long serialVersionUID = 1L;
public UserDto() {
}
@Id
@Column(name = "c_um_npk_id", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_sequence")
@SequenceGenerator(name = "user_sequence", sequenceName = "t_user_master_c_um_npk_id_seq", initialValue = 1, allocationSize = 1)
private Long id;
@Column(name = "c_um_vnm_username", nullable = false)
private String username;
@Column(name = "c_um_vnm_email_address")
private String emailId;
@JsonSerialize(using = JsonDateSerializer.class)
@Column(name = "c_um_dnm_birth_date",nullable=false)
@Temporal(TemporalType.DATE)
private Date birthdate;
@Column(name="c_um_nnm_age",nullable=false)
private int age;
@Column(name = "c_um_vnm_first_name")
private String firstName;
@Column(name = "c_um_vnm_last_name")
private String lastName;
@Column(name = "c_um_vnm_mobile_number")
private String mobileNumber;
@Column(name = "c_um_vnm_gender")
private String gender;
@JsonManagedReference("user-role")
@OneToMany(mappedBy = "userDto", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<RoleDto> roleDtos;
/* getter setter of properties */
}
在这里,我将所有者定义为Lazy集合。但是当我试图找到以下所有学校时:
public List<SchoolDto> getAllSchoolsWithAdvanceSearch(SchoolSearchDto schoolSearchDto) {
return getEntityManager().createQuery("SELECT s from SchoolDto s").setFirstResult(schoolSearchDto.getFrom()).setMaxResults(schoolSearchDto.getSize()).getResultList();
}
这个学校的返回列表,其所有者的所有者信息包括所有者的角色(这也是LaD集合形式UserDto),当我记录hibernate查询时,我只得到一个选择如下:
select
schooldto0_.c_sm_npk_id as c1_24_,
schooldto0_.c_sm_vnm_address_line_1 as c2_24_,
schooldto0_.owner_c_um_npk_id as owner17_24_
from
t_school_master schooldto0_
在搜索过程中,我发现它可能是杰克逊序列化问题,因此我根据jackson - do not serialize lazy objects&amp; Avoid Jackson serialization on non fetched lazy objects如下
应用servlet.xml中
<mvc:annotation-driven>
<mvc:message-converters>
<ref bean="jsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.util.HibernateAwareObjectMapper" />
</property>
</bean>
HibernateAwareObjectMapper.java
public HibernateAwareObjectMapper() {
this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
Hibernate4Module module = new Hibernate4Module();
module.configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, false);
module.disable(Hibernate4Module.Feature.USE_TRANSIENT_ANNOTATION);
registerModule(module);
}
注意:我稍后需要所有者详细信息,因此我无法将其标记为@JsonIgnore
。
请帮我解决这个问题。提前致谢
编辑:当前输出
0: {
"id": 1
"name": "school1"
"address1": null
owner : {
"id" : 1,
"firstName": "Rutesha",
"lastName" : "Patel",
"roles":[{
"name" : "SCHOOL_ADMIN"
}]
}
}
修改:预期输出
0: {
"id": 1
"name": "school1"
"address1": null
}
OR
0: {
"id": 1
"name": "school1"
"address1": null
owner : null
}
答案 0 :(得分:0)
进行以下更改
return getEntityManager().createQuery("SELECT s.id,s.name,s.address1 from SchoolDto s").setFirstResult(schoolSearchDto.getFrom()).setMaxResults(schoolSearchDto.getSize()).getResultList();
@ManyToOne
- 就像注释在文档中有相同的问题,但在这种情况下,JPA提供者更有可能会考虑FetchType提示。
@ManyToOne(cascade={CascadeType.MERGE},optional = false,fetch=FetchType.LAZY )
UserDto owner;
注释后也可以提供帮助。
@OneToMany(fetch = FetchType.LAZY, mappedBy = "owner")
@Cascade(CascadeType.ALL)
@Fetch(FetchMode.SELECT)
@BatchSize(size = 10)
应该使用@JoinColumn(name="other_entity_fk")
。
例如
@ManyToOne(fetch = FetchType.LAZY)
@Fetch(FetchMode.SUBSELECT)
@JoinColumn(name = "STOCK_ID", nullable = false)
public Stock getStock() {
return this.stock;
}