我正在使用hibernate一对多的双向映射,同时更新记录。在子表中为新子记录父引用变量在子表中存储null。请任何人帮我解决这个问题。如果它不可理解请问我会发布我所有实体的与此主题有关。 Plz解决了这个问题。提前谢谢。
我的编辑代码:
LibraryBookForm lb = (LibraryBookForm) form;
int bookid = lb.getBooklistid();
System.out.println("bookidbookidbookid" + bookid);
String[] bar = lb.getBarcode();
String[] accission = lb.getAccessno();
System.out.println("accissionaccission" + bar);
UserEntity msg;
HttpSession session = request.getSession(false);
msg = (UserEntity) session.getAttribute("user");
int catid = lb.getCategory();
Session ses = HibernateUtil.getSessionFactory().openSession();
String d1 = lb.getBilldate();
java.util.Date temp = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(d1);
java.sql.Date date = new java.sql.Date(temp.getTime());
String d2 = lb.getDateAdded();
java.util.Date temp1 = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).parse(d2);
java.sql.Date submissionDate = new java.sql.Date(temp1.getTime());
CategoryEntity ce = (CategoryEntity) ses.get(CategoryEntity.class, catid);
LibraryBookListEntity lbl = (LibraryBookListEntity) ses.load(LibraryBookListEntity.class, bookid);
Transaction tx = ses.beginTransaction();
lbl.setEdition(lb.getEdition());
lbl.setPublisher(lb.getPublisher());
lbl.setPlace(lb.getPlace());
lbl.setPage(lb.getPag());
lbl.setSource(lb.getSource());
lbl.setBillno(lb.getBillno());
lbl.setBilldate(date);
lbl.setPublishedyear(lb.getPublishedyear());
lbl.setCallno(lb.getCallno());
lbl.setIsbn(lb.getIsbn());
lbl.setBooktitle(lb.getBooktitle());
lbl.setRack(lb.getRack());
lbl.setAuthor(lb.getAuthor());
lbl.setPrice(lb.getPrice());
lbl.setCategoryid(ce);
lbl.setDateAdded(submissionDate);
lbl.setTcopies(lb.getTcopies());
lbl.setAddedBy(msg);
lbl.setAddedate(new Date());
lbl.setModifiedBy(null);
lbl.setModifiedDate(null);
try {
java.util.List list = Arrays.asList(bar);
Document document = new Document(new Rectangle(PageSize.A4));
File file = new File("d://" + lb.getBooktitle() + ".pdf");
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file, false));
document.open();
Paragraph para = new Paragraph();
Set<LibraryBarCodeEntity> s = new HashSet<LibraryBarCodeEntity>();
LibraryBarCodeEntity mb = null;
for (int i = 0; i < list.size(); i++) {
System.out.println("inside library model edit");
mb = new LibraryBarCodeEntity();
System.out.println("bar[i]" + bar[i]);
System.out.println("bar[i]" + accission[i]);
mb.setBarCode(bar[i]);
mb.setAccessno(accission[i]);
para.add(new Phrase(new Chunk(BarcodeDao.genarateBarcode(bar[i], writer), 0, 0, true)));
para.add(new Phrase(Chunk.NEWLINE));
para.add(new Phrase(Chunk.NEWLINE));
s.add(mb);
}
lbl.setChield(s);
ses.update(lbl);
tx.commit();
document.add(para);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:1)
试试这个 你忘了在孩子中设置父引用(在for循环中mb.setParent(lbl);)
for (int i = 0; i < list.size(); i++) {
System.out.println("inside library model edit");
mb = new LibraryBarCodeEntity();
System.out.println("bar[i]" + bar[i]);
System.out.println("bar[i]" + accission[i]);
mb.setBarCode(bar[i]);
mb.setAccessno(accission[i]);
para.add(new Phrase(new Chunk(BarcodeDao.genarateBarcode(bar[i], writer), 0, 0, true)));
para.add(new Phrase(Chunk.NEWLINE));
para.add(new Phrase(Chunk.NEWLINE));
mb.setParent(lbl);
s.add(mb);
}