假设我有一个名为BusinessCategory的类:
@Entity
public class BusinessCategory implements Serializable{
@Id
private int id;
@Column(nullable = false)
private String name;
@ManyToOne
private BusinessCategory parentCategory;
...
}
其中要延迟加载parentCategory。另一个名为Transaction
的类,其中包含BusinessCategory
:
@Entity
public class Transaction implements Serializable{
@Id
private long id;
@Column(nullable = false)
private String description;
@ManyToOne
private BusinessCategory businessCategory;
...
}
在@Service
中,我检索一个列表,该列表依次处理其中一个步骤涉及:
BusinessCategory category = transaction.getBusinessCategory();
if(transaction.getBusinessCategory().getParentCategory()!=null){
...
/** do stuff **/
}
真正奇怪的是,即使我的测试事务有一个带有父级的BusinessCategory,transaction.getBusinessCategory().getParentCategory()
只返回null
(在调试器中看作handler
对象)。甚至更奇怪的事实是我能够直接访问parentCategory
的属性。观看窗口截图:
我还确认,当访问parentCategory的某个属性时,会立即触发MySQL查询(这很好)。我打开了MySQL的general_log&尾随它。
1933 Query select businessca0_.id as id1_3_0_, businessca0_.dateCreated as dateCrea2_3_0_, businessca0_.dateUpdated as dateUpda3_3_0_, businessca0_.categoryType as category4_3_0_, businessca0_.depth as depth5_3_0_, businessca0_.mappedIds as mappedId6_3_0_, businessca0_.name as name7_3_0_, businessca0_.parentCategory_id as parentCa8_3_0_ from BusinessCategory businessca0_ where businessca0_.id=10013
这似乎是一个Spring框架漏洞?