我想用hibernate-self join设计父子逻辑。
Office Model Class
@Entity
@Table(name = "tbl_office")
public class Office implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "OFFICE_ID")
private Integer officeId;
@Column(name = "OFFICE_NAME")
private String officeName;
@Column(name = "OFFICE_FAX")
private String officeFax;
@Column(name = "OFFICE_PHONE")
private String officePhone;
@Column(name = "OFFICE_STATUS")
private String officeStatus;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="PARENT_OFFICE_ID")
private Office parent;
@OneToMany(mappedBy = "parent")
private Set<Office> childOffice = new HashSet<Office>();
//Getters & Setters
}
Office DAO Impl Class 我有类似那种方法的dao类。道是一个界面。
@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();
}
}
Office Controller Class
@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;
}
Office Service Impl Class 我有一个服务类。这个类与Dao类相同,它是一个接口
@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查看页面
<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>
我想保存&#34;总经理&#34;父值为null。第二个记录包括&#34;总经理助理&#34;并选择&#34;总经理&#34;来自父列表。它给加载&#34;加载&#34; id我尝试保存常规管理器父值时出错是null。但是,当我手动插入数据库总经理与父级是空值。它将保存,但它将保存总经理助理,但我不希望它手动插入数据库。