Spring Roo不会为.aj文件中的字段添加FetchType.LAZY,我应该手动执行它吗? [英] Spring Roo doesn't add FetchType.LAZY for fields in .aj files, Should I do it manually?

查看:115
本文介绍了Spring Roo不会为.aj文件中的字段添加FetchType.LAZY,我应该手动执行它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是一个问题,如果Spring Roo通过反向工程为.aj文件中的Set字段添加FetchType.LAZY,或者我应该手动执行它?



如果FetchType.LAZY不存在于.aj文件中,我可以通过查询SELECT st1 FROM parentTable t1 JOIN FETCH t1.childTable st1进入select,对吗?



这里的要点是,我可以手动添加FetchType.LAZY到文件(重构.aj文件>推入..),然后在.java文件(如果我想让Roo将控件保留在我的域类上,则风险高) 。



关于通过查询来完成它,我无法做到这一点,因为我得到了:

 >查询指定的连接提取,但获取的关联的所有者不在选择列表中[FromElement {显式,不是集合连接,访存连接,获取非惰性属性,classAlias = ii,role = ... domain.Industry .i18nIndustries,
tableName = .. I18nIndustry,
tableAlias = i18nindust3_,
origin = ..行业industry2_,
列= {industry2_.IdIndustry,className = .. domain.I18nIndustry }}]
[选择u.idUserName,u.isActiveInd,u.employeeNbr,u.name,c.name作为CompanyName,ii.name作为IndustryName来自用户u JOIN u.idCompany c JOIN c.idIndustry i JOIN FETCH i.i18nIndustries ii WHERE u.idUserName =:username]

这里由Roo生成的.aj文件:

 特权方面Users_Roo_DbManaged {
...
@ManyToOne
@JoinColumn(name =IdCompany,referencedColumnName =IdCompany,nullable = false)
私人公司Users.idCompany; ...

..

 特权方面Company_Roo_DbManaged {
...
@ManyToOne
@JoinColumn(name =IdCity,referencedColumnName =IdCity,nullable = false)
私人城市Company.idCity;

@ManyToOne
@JoinColumn(name =IdIndustry,referencedColumnName =IdIndustry,nullable = false)
私有行业Company.idIndustry; ..

..

 特权方面Industry_Roo_DbManaged {.. 
@OneToMany(mappedBy =idIndustry)
private Set< I18nIndustry> Industry.i18nIndustries; ...

您能否给我一个线索,了解这里发生的事情?



Spring MVC 3.2,
Roo 1.2.4,
MSSql数据库



谢谢大家!
--JR

解决方案

关于


这些是一些问题,如果Spring Roo通过反向工程为.aj文件中的Set字段添加FetchType.LAZY,或者应该手动执行它?

这不是必需的,因为这是这种关系的默认设置(请参阅 https:// stackoverflow


如果FetchType.LAZY不存在于.aj文件中,我可以通过查询SELECT st1 FROM parentTable t1 JOIN FETCH t1.childTable st1进入select,对吗?

这使得加载关系数据。我认为你对LAZY和EAGER模式的含义有误:



另一方面,加载一个LAZY属性需要一个数据库连接当任何东西试图获取数据时都是活着的,所以,你必须在一个非静态的 @Transactional 注解的方法中执行这段代码(读取 @Transactional JavaDoc获取更多信息)。



我建议您查看JPA规范。根据我的经验,我非常有用。

These are a couple of question, should Spring Roo through reverse engineering adding FetchType.LAZY for Set fields in .aj files or should I do it manually?

If FetchType.LAZY is not present at .aj files, I could make it through the queries " SELECT st1 FROM parentTable t1 JOIN FETCH t1.childTable st1" into the select, right ?

The point here is that I can add FetchType.LAZY to the files manually (Refactor .aj file > Push In..) and then on .java file (high risk if I want Roo to keep the control on my domain classes).

About to make it through queries, I can't do this because I'm getting:

> query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=ii,role=...domain.Industry.i18nIndustries,
tableName=..I18nIndustry,
tableAlias=i18nindust3_,
origin=..Industry industry2_,
columns={industry2_.IdIndustry ,className=..domain.I18nIndustry}}] 
[select u.idUserName, u.isActiveInd, u.employeeNbr, u.name, c.name as CompanyName, ii.name as IndustryName from Users u JOIN u.idCompany c JOIN c.idIndustry i JOIN FETCH i.i18nIndustries ii WHERE u.idUserName = :username ]

Here the .aj files generated by Roo:

privileged aspect Users_Roo_DbManaged {
...    
@ManyToOne
@JoinColumn(name = "IdCompany", referencedColumnName = "IdCompany", nullable = false)
private Company Users.idCompany;  ...

..

privileged aspect Company_Roo_DbManaged {
...
@ManyToOne
@JoinColumn(name = "IdCity", referencedColumnName = "IdCity", nullable = false)
private City Company.idCity;

@ManyToOne
@JoinColumn(name = "IdIndustry", referencedColumnName = "IdIndustry", nullable = false)
private Industry Company.idIndustry; ..

..

    privileged aspect Industry_Roo_DbManaged {    ..
@OneToMany(mappedBy = "idIndustry")
private Set<I18nIndustry> Industry.i18nIndustries; ...

Can you please give me a clue, about what is happening here ?

Spring MVC 3.2, Roo 1.2.4, MSSql database

Thanks folks! --JR

解决方案

About

These are a couple of question, should Spring Roo through reverse engineering adding FetchType.LAZY for Set fields in .aj files or should I do it manually?

This is not needed because this is the default for this kind of relationship (see https://stackoverflow.com/a/13765381/2295657).

If FetchType.LAZY is not present at .aj files, I could make it through the queries " SELECT st1 FROM parentTable t1 JOIN FETCH t1.childTable st1" into the select, right ?

This makes a eager loading of the relationship data. I think you are in a mistake about the meaning of LAZY and EAGER modes:

  • EAGER: Requires to load value when instance is loaded into memory (default for basic and no-collections-relationship properties)
  • LAZY: when instance is loaded a proxy is stored in property and, when anything tries to get data, loads it form DB (default for collections-relationship properties)

On the other hand, to load a LAZY property requires a DB Connection alive when anything tries to get the data, so, you must execute this code inside a no-static, @Transactional annotated, method (read @Transactional JavaDoc for more info).

I advice you to make a look in the JPA specification. In my experience, I so useful.

这篇关于Spring Roo不会为.aj文件中的字段添加FetchType.LAZY,我应该手动执行它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆