我无法解决这个问题。我得到了“线程中的异常”main”org.hibernate.LazyInitializationException:未能延迟初始化角色集合:org.sandy.domain.Location.items,没有会话或会话被关闭”我知道会话已关闭,但tx: notionation-drive@Transactional应该确保打开会话。它可以很好地与EAGER获取一起工作。哦,是的——这是通过apress pro Spring 3示例。
但是,也许我不明白这里的概念-我的意思是,当我调试"getFirst()"中的一些服务,我可以看到集合中的所有项目,但一旦返回命中-LazyInit异常抛出...
package org.sandy.main;
import org.sandy.domain.Item;
import org.sandy.domain.Location;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Service;
@Service(value = "main")
public class EntryPoint {
@Autowired
private SomeService ss;
public static void main(String args[]) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:*-beans.xml");
ctx.refresh();
EntryPoint entryPoint = (EntryPoint) ctx.getBean("main");
Item item = new Item();
Location location = new Location();
item.setLocation(location);
location.getItems().add(item);
entryPoint.getSs().save(location);
System.out.println(entryPoint.getSs().findFirst());
ctx.registerShutdownHook();
}
public SomeService getSs() {
return ss;
}
public void setSs(SomeService ss) {
this.ss = ss;
}
}
以及服务
package org.sandy.main;
import org.sandy.domain.Item;
import org.sandy.domain.Location;
import org.sandy.repo.ItemRepo;
import org.sandy.repo.LocationRepo;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service(value = "ss")
@Repository
@Transactional
public class SomeService {
@Autowired
private ItemRepo itemRepo;
@Autowired
private LocationRepo locationRepo;
@Transactional
public void save(Location location) {
locationRepo.save(location);
}
@Transactional(readOnly = true)
public List<Item> findFirst() {
System.out.println("ONE");
Iterable<Location> it = locationRepo.findAll();
System.out.println("TWO");
Location l = it.iterator().next();
System.out.println("THREE");
List<Item> r = l.getItems();
return r;
}
@Transactional
public Iterable finAll() {
return locationRepo.findAll();
}
public void setItemRepo(ItemRepo itemRepo) {
this.itemRepo = itemRepo;
}
public void setLocationRepo(LocationRepo locationRepo) {
this.locationRepo = locationRepo;
}
}
和位置回购
package org.sandy.repo;
import org.sandy.domain.Location;
import org.springframework.data.repository.CrudRepository;
public interface LocationRepo extends CrudRepository<Location, Long> {
}
实体:
@Entity
@Table(name = "Locations")
public class Location extends Entry {
private List<Item> items = new ArrayList<Item>();
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "location")
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
}
package org.sandy.domain;
import javax.persistence.*;
@Entity
@Table(name = "Items")
public class Item extends Entry {
private Location location;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn()
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
}
和全能的xml bean配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jdbc:embedded-database id="dataSource" type="H2" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="org.sandy.domain" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.H2Dialect
</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
</props>
</property>
</bean>
<context:annotation-config/>
<jpa:repositories base-package="org.sandy.repo" entity-manager-factory-ref="emf" transaction-manager-ref="transactionManager" />
<context:component-scan base-package="org.sandy" />
</beans>
这段代码
System.out.println(entryPoint.getSs().findFirst());
是相当于
/* 1 */ SomeService ss = entryPoint.getSs();
/* 2 */ List<Item> items = ss.findFirst(); // Session opens and closes for @Transactional
/* 3 */ String toPrint = items.toString(); // no more Session
/* 4 */ System.out.println(toPrint);
因此,您可以看到会话
边界仅包装findFirst()
。如果延迟加载实体,则在第3行,项
中的元素未初始化。当您尝试在<code>列表#toString()</code>中调用<code>toString()。
在使用实体之前,应该完全初始化它们。这必须在会话边界内完成。
问题内容: 异常存储在哪里?堆,堆。如何为异常分配和释放内存?现在,如果您有多个需要处理的异常,是否创建了所有这些异常的对象? 问题答案: 我假设为异常分配的内存分配方式与所有其他对象(在堆上)分配方式相同。 这曾经是个问题,因为您不能为OutOfMemoryError分配内存,这就是直到Java 1.6之前 才没有堆栈跟踪的原因。现在,它们也为stacktrace预分配了空间。 如果您想知道在抛
在从DB表检索记录时,我们得到了异常。我在另一张桌子上也做了同样的尝试,它起作用了,但在这张桌子上,它不起作用了。我使用的是 在这里,我创建了一个具有 get 映射的 Controller 类 我收到此异常 SEVERE:Servlet.service()的servlet[调度服务器]在上下文中与路径[]抛出异常(请求处理失败;嵌套异常org.springframework.core.conver
线程“main”java.lang.error:未解决的编译问题:类型不匹配:无法从java.sql.statement转换为com.mysql.jdbc.statement 我是java初学者,我正在尝试使用mysql数据库,我已经从mysql.com下载了mysql-connector-java-5.1.23-bin.jar文件,并且我已经将这个jar文件添加到我的项目的构建路径中,但是线程“
2018-02-28 13:18:20.062警告15208--[restartedMain]ationConfigEmbeddedWebApplicationContext:上下文初始化过程中遇到异常-取消刷新尝试:org.springFramework.Beans.Factor.UnsatistifiedDependencyException:创建类路径资源[org/springFramewo
1,极值分析 通过scatterplots,histogramas, box和whisker plot分析极值。 查看样本分布(假设高斯分布),去距离1/4和3/4值2-3倍标准差数值的样本。 2,临近方法 基于k-means分析样本质心,去掉离质心特别远的样本。 3,投影方法 通过PCA,SOM,sammon mapping去掉不重要特征。
我是Jface数据绑定的新手。我正在尝试使用数据绑定来生成表。当任何一个人单击row时,在映射的文本字段中显示vales。当我这样做时,我得到异常。(“java.lang.IllegalArgumentException:Could not find property with name in class class com.swt.pro.model.employee”)下面是类结构。 我有3个