我正在使用 JPA、Hibernate 和 Spring MVC。在控制器类中,所有方法都非常有效。当我在网络浏览器中测试它们时,该public String getModuleFormation(long id)
方法返回一个对象,它给了我以下错误:
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
作为根本原因,但昨天我尝试了它,它在 localhost:45045/GestionModules/detail/xx URL 中没有问题。
什么可能导致这个问题?
我的detail.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}";
}
}