Spring+jpa 做单元测试时的延迟加载错误问题。麻烦来看看啊 Spring+jpa做单元测试,实体类配置了@oneToMany 出现“ no session or session was closed”的错误。要用什么方法解决啊,搜了一堆没有找到想要的答案。没上WEB不要给我复制一堆在web.xml下配置的代码啊~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 贴下那个实体类的代码。@Entity@Table(name = "Organization")public class Organization { @Id @GeneratedValue @Column(name = "OrganizationId") private int organizationId; @Column(name = "OrganizationName", length = 200) private String organizationName; @Column(name = "OrganizationDesc") private String organizationDesc; @Column(name = "OrganizationCode", length = 10) private String organizationCode; @ManyToOne(targetEntity = Organization.class) @JoinColumn(name = "Organization_ParentId") private Organization parent; @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.LAZY, mappedBy = "parent") private Set<Organization> children = new HashSet<Organization>();测试代码(部分)如下: @Test public void testGetUser() { Organization organization = organizationService.getOrganization(1); Set<Organization> set = organization.getChildren(); for (Organization o : set) { System.out.println(o.getOrganizationName()); } } 你這是多對多自關聯,但是lazy為true,延遲加載,所以, Organization organization = organizationService.getOrganization(1);調用Set<Organization> set = organization.getChildren();之前,session已經關閉。把fetch = FetchType.LAZY 改成fetch = FetchType.EAGER 回楼上 我就想用lazy加载怎么实现呢? 如果在web上的话可以加上“openSessionInview”这句话来实现,但是不在web上的话呢? 也可以,写hql的时候,加上join fetch 根据URL生成网页缩略图 JSONObject 多表关联后将list值显示到jsp页面上的问题? B/S、C/S下的FTP开发 一个sql相关的问题(数字补零或者补空)? 关于Sturst 该从那个方面下手 netbeans中对properties中乱码是怎么处理的? 请问在WEB-INF下的jsp页面如何关联也在WEB-INF下的xsl 急死人了,eclipse的java类库项目怎么引入j2ee类库? 请教关于EJB调用的问题 网上下了个SSH框架,能跑起来。然后我copy一下改了项目名之后就不能正常运行了 JAVA操作串口:多线程访问同一串口,效率提升方案讨论帖(modbus协议)
@Entity
@Table(name = "Organization")
public class Organization {
@Id
@GeneratedValue
@Column(name = "OrganizationId")
private int organizationId; @Column(name = "OrganizationName", length = 200)
private String organizationName; @Column(name = "OrganizationDesc")
private String organizationDesc; @Column(name = "OrganizationCode", length = 10)
private String organizationCode; @ManyToOne(targetEntity = Organization.class)
@JoinColumn(name = "Organization_ParentId")
private Organization parent; @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.REFRESH }, fetch = FetchType.LAZY, mappedBy = "parent")
private Set<Organization> children = new HashSet<Organization>();
测试代码(部分)如下: @Test
public void testGetUser() {
Organization organization = organizationService.getOrganization(1);
Set<Organization> set = organization.getChildren();
for (Organization o : set) {
System.out.println(o.getOrganizationName());
}
}
調用
Set<Organization> set = organization.getChildren();
之前,session已經關閉。
把fetch = FetchType.LAZY 改成fetch = FetchType.EAGER
回楼上 我就想用lazy加载怎么实现呢? 如果在web上的话可以加上“openSessionInview”这句话来实现,
但是不在web上的话呢?