English 中文(简体)
使用负载而不是休眠时遇到的例外
原标题:LazyInitializationException encountered when using load instead of get with Hibernate

我使用 JPA、 Hibernate 和 Spring MVC 。 在控制器类中, 所有方法都非常有效。 当我在网络浏览器中测试它们时, 返回一个对象的 < code> public String 方法会得到 Module Formation( long id) 方法, 它给了我以下错误 :

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

但昨天我尝试过, 当地主机:45045/GestionModules/ detail/xx URL 没有问题。

是什么原因造成这一问题?

我的细节。 jsp:

<c:if test="${!empty detailModule}">

${detailModule.idModule}
${detailModule.libModule}
</c:if>

POJO 类+ JPA :

@Entity
@Table(name="ModuleFormation")
public class ModuleFormation {

private long idModule;
private String libModule;

public ModuleFormation() {
    // TODO Auto-generated constructor stub
}

public ModuleFormation(String libModule) {
    this.libModule = libModule;
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "seqModule")
@SequenceGenerator(name="seqModule", sequenceName = "seqModuleFormation")
@Column(name="idModule")
public long getIdModule() {
    return this.idModule;
}

public void setIdModule(long idModule) {
    this.idModule = idModule;
}

@Column(name="libModule", nullable=false, length = 100)
public String getLibModule() {
    return this.libModule;
}

public void setLibModule(String libModule) {
    this.libModule = libModule;
}

}

DAO 类:

@Repository
public class ModuleFormationDAOImpl implements ModuleFormationDAO {

@Autowired
private SessionFactory sessionFactory;


public void ajouterModuleFormation(ModuleFormation module) {
    sessionFactory.getCurrentSession().save(module);
}

public void supprimerModuleFormation(long idModule) {
    ModuleFormation module = (ModuleFormation) sessionFactory.getCurrentSession().load(ModuleFormation.class, idModule);
    if(module != null)
        sessionFactory.getCurrentSession().delete(module);
}

public List<ModuleFormation> listModuleFormation() {
    
    return sessionFactory.getCurrentSession().createQuery("from ModuleFormation")
            .list();
    
}

public ModuleFormation getModuleFormation(long idModule) {
     return (ModuleFormation) sessionFactory.getCurrentSession().load(ModuleFormation.class, idModule);
}

public void majModuleFormation(ModuleFormation module) {
    sessionFactory.getCurrentSession().merge(module);
}

}

服务等级:

@Service
public class ModuleFormationServiceImpl implements ModuleFormationService {

@Autowired
private ModuleFormationDAO moduleDao;

@Transactional
public void ajouterModuleFormation(ModuleFormation module) {
    moduleDao.ajouterModuleFormation(module);
}

@Transactional
public void supprimerModuleFormation(long idModule) {
    moduleDao.supprimerModuleFormation(idModule);
}

@Transactional
public List<ModuleFormation> listModuleFormation() {
    return moduleDao.listModuleFormation();
}

@Transactional
public ModuleFormation getModuleFormation(long idModule) {
    return moduleDao.getModuleFormation(idModule);
}

@Transactional
public void majModuleFormation(ModuleFormation module) {
    moduleDao.majModuleFormation(module);
}
}

主计长级 :

@Controller
public class ModuleFormationController {

@Autowired
private ModuleFormationService moduleService;

@RequestMapping("/module")
public String listModulesFormations(Map<String, Object> map) {
    
    map.put("module", new ModuleFormation());
    map.put("moduleList", moduleService.listModuleFormation());
    
    return "module";
}

@RequestMapping(value = "/ajouter", method = RequestMethod.POST )
public String ajouterModuleFormation(@ModelAttribute("module")
ModuleFormation module,BindingResult result) {
    
    moduleService.ajouterModuleFormation(module);
    
    return "redirect:/module";
}


@RequestMapping(value = "/supprimer/{idModule}")
public String supprimerModuleFormation(@PathVariable("idModule")
long idModule) {
    moduleService.supprimerModuleFormation(idModule);
    
    return "redirect:/module";
}


@RequestMapping(value= "/detail/{idModule}")
public String getModuleFormation(@PathVariable("idModule")
long idModule,Map<String, Object> map) {
    map.put("detailModule", moduleService.getModuleFormation(idModule));
    return "/detail";
}

    
@RequestMapping(value= "/detail/modifier", method = RequestMethod.POST )
public String majModuleFormation(@ModelAttribute("detailModule")
ModuleFormation module, BindingResult result) {
    moduleService.majModuleFormation(module);
    return "detail/{idModule}";
}

}
最佳回答

休眠 < code> Say#load(系列,可序列) 方法上的Javadoc说:

Return the persistent instance of the given entity class with the given identifier, assuming that the instance exists. This method might return a proxied instance that is initialized on-demand, when a non-identifier method is accessed.

当您在 JSP 中访问对象上的属性时,装入该对象的会话已被关闭。

使用 session#get( 序列、 序列) 以确保您不装入代理文件 。

问题回答

您是否尝试过 sessionFactory. getCorporateCenter Session () 。 getCorporation (ModuleFormation. class, idModule) ? 。 get (moduleFormation. classule, idModule) ?





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签