Java SpringMVC+Hibernate自连接给出了类似“加载需要id”的错误
我想使用hibernate自连接的父子逻辑进行设计 办公室模型课 办公服务Impl类 我有一个服务班。这个类与Dao类相同,它是一个接口Java SpringMVC+Hibernate自连接给出了类似“加载需要id”的错误,java,spring,hibernate,spring-mvc,self-join,Java,Spring,Hibernate,Spring Mvc,Self Join,我想使用hibernate自连接的父子逻辑进行设计 办公室模型课 办公服务Impl类 我有一个服务班。这个类与Dao类相同,它是一个接口 @Service @Transactional(propagation = Propagation.SUPPORTS, readOnly = true) public class OfficeServiceImpl implements OfficeService { @Autowired private OfficeDao officeDa
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class OfficeServiceImpl implements OfficeService {
@Autowired
private OfficeDao officeDao;
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addOffice(Office office) {
officeDao.addOffice(office);
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List<Office> getAllOffice() {
return officeDao.getAllOffice();
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public Birim getOfficeById(Integer officeId) {
return officeDao.getOfficeById(officeId);
}
}
面板办公室jsp查看页面
我想用父值为null保存General Manager。第二条记录后包括总经理助理和从父列表中选择总经理。当我试图保存General Manager父值为null时,加载错误需要加载id。但是当我手动插入数据库时,总经理的父项为空值。它将被保存,但它也将保存总经理助理,但我不希望它手动插入数据库
@Repository
public class OfficeDaoImpl implements OfficeDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public void addOffice(Office office) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(office);
session.flush();
}
@Override
public Office getOfficeById(Integer officeId) {
Session session = sessionFactory.getCurrentSession();
Office office = (Office) session.get(Office.class, officeId);
return office;
}
@SuppressWarnings("unchecked")
@Override
public List<Office> getAllOffices() {
Session session = sessionFactory.getCurrentSession();
return session.createQuery("FROM Office").list();
}
}
@Controller
@RequestMapping(value = "/panel/office")
public class OfficeController {
@Autowired
private OfficeService officeService;
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addOfficeGET(@RequestParam(required = false) Integer officeId){
ModelAndView mav = new ModelAndView("panel/panel-offices");
Office office = null;
if (officeId != null) {
office = officeService.getOfficeById(officeId);
}
if (office == null) {
office = new Office();
}
mav.addObject("office", new Office());
mav.addObject("offices", officeService.getAllOffice());
return mav;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ModelAndView addOfficePOST(@Valid @ModelAttribute("office") Office office, BindingResult result){
ModelAndView mav1 = new ModelAndView();
ModelAndView mav2 = new ModelAndView("panel/panel-offices");
mav1.setViewName("redirect:/panel/office/add");
Office parent = null;
parent= officeService.getOfficeById(office.getParent().getOfficeId());
office.setParent(parent);
if (result.hasErrors()) {
return mav2;
}
if (office.getOfficeId() != null) {
officeService.officeGuncelle(office);
} else {
officeService.OfficeEkle(office);
}
return mav1;
}
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class OfficeServiceImpl implements OfficeService {
@Autowired
private OfficeDao officeDao;
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addOffice(Office office) {
officeDao.addOffice(office);
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List<Office> getAllOffice() {
return officeDao.getAllOffice();
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public Birim getOfficeById(Integer officeId) {
return officeDao.getOfficeById(officeId);
}
}
<div class="form-group">
<label for="parent">Parent Office Name:</label>
<form:select path="parent.officeId" class="form-control chosen-full-select">
<form:option value="">--Select--</form:option>
<c:forEach items="${offices}" var="office">
<form:option value="${office.officeId}">${office.officeName}</form:option>
</c:forEach>
</form:select>
</div>