Java 如何使用hibernate通过添加一个子行来更新记录?
在这里,我将更新记录。通过为激动人心的家长再添加一个孩子,在这里我通过迭代孩子的来删除孩子,然后我更新记录,但它给出了一个例外。 异常:org.hibernate.hibernateeexception:找到同一集合的两个表示形式:com.treamis.entity.LibraryBookListEntity.chield 这是我的密码Java 如何使用hibernate通过添加一个子行来更新记录?,java,hibernate,Java,Hibernate,在这里,我将更新记录。通过为激动人心的家长再添加一个孩子,在这里我通过迭代孩子的来删除孩子,然后我更新记录,但它给出了一个例外。 异常:org.hibernate.hibernateeexception:找到同一集合的两个表示形式:com.treamis.entity.LibraryBookListEntity.chield 这是我的密码 LibraryBookForm lb = (LibraryBookForm) form; int bookid = lb.getBooklis
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);
Set<LibraryBarCodeEntity> s1 = lbl.getChield();
for (LibraryBarCodeEntity lbb : s1) {
System.out.println("inside delte");
ses.delete(lbb);
}
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]);
mb.setParent(lbl);
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);
ses.saveOrUpdate(lbl);
}
// lbl.setChield(s);
tx.commit();
你组成了一个新的团队。必须将其设置为父对象(即lb1)。然后,如果保存lb1,则将保存子集中的更改。(确保在映射集中为集合指定了cascede save update或all属性)
for(int i=0;i
你是指现有的父母吗?是的,对于令人兴奋的父母,我需要再增加一个孩子。这是什么?请你解释一下。拿出你的英语词典,查一下“令人兴奋”和“现有”两个词。请注意,它们的含义不同…为什么级联类型为SAVE_UPDATE将不允许。您也可以提供SAVE_UPDATE。当父集合正在保存或更新时,它将保存或更新内部集合现在我遇到这样的异常:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:无法添加或更新子行:外键约束失败(treamisdemo
librarybarcodetable
,CONSTRAINTFKC205D3495D63121A
外键(BookListId
)引用librarybooklisttable
(BookListId
).你能在问题中发布映射文件、实体、数据库结构吗?那么只有这个问题才能解决。我认为问题在以下行中:(LibraryBarCodeEntity lbb:s1){System.out.println(“inside delte”);ses.delete(lbb);}只需注释for循环并检查
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.treamis.entity;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
/**
*
* @author admin
*/
@Entity
@Table(name = "LibraryBookListTable")
public class LibraryBookListEntity implements Serializable {
@Id
@GeneratedValue
@Column(name = "BookListId")
private int booklistid;
@Column(name = "ISBN", nullable = false)
private String isbn;
@Column(name = "edition", nullable = false)
private String edition;
@Column(name = "publisher", nullable = false)
private String publisher;
@Column(name = "place", nullable = false)
private String place;
@Column(name = "page", nullable = false)
private String page;
@Column(name = "source", nullable = false)
private String source;
@Column(name = "billno", nullable = false)
private String billno;
@Column(name = "callno", nullable = false)
private String callno;
@Column(name = "BookTitle", nullable = false)
private String booktitle;
@Column(name = "BookAuthor", nullable = false)
private String author;
@Column(name = "BookPrice", nullable = false)
private float price;
@Column(name = "RackNumber", nullable = false)
private String rack;
@Column(name = "PublishedYear", nullable = false)
private String publishedyear;
@Column(name = "NoofCopies", nullable = false)
private int tcopies;
@Column(name = "DateAdded", nullable = false)
private java.sql.Date dateAdded;
@Column(name = "billdate", nullable = false)
private java.sql.Date billdate;
@ManyToOne(fetch = FetchType.LAZY, targetEntity = CategoryEntity.class)
@JoinColumn(name = "category_Id", referencedColumnName = "category_Id", nullable = true)
private CategoryEntity categoryid;
@OneToOne
private UserEntity addedBy;
@OneToOne
private UserEntity modifiedBy;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "parent")
@JoinColumn(name = "BookListId", referencedColumnName = "BookListId")
@Cascade({CascadeType.SAVE_UPDATE})
private Set< LibraryBarCodeEntity> chield;
public Set<LibraryBarCodeEntity> getChield() {
return chield;
}
public void setChield(Set<LibraryBarCodeEntity> chield) {
this.chield = chield;
}
//@Column(name = "AddedDate", nullable = false)
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private java.util.Date addedate;
// @Column(name = "ModifiedDate", nullable = false)
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private java.util.Date modifiedDate;
public int getBooklistid() {
return booklistid;
}
public void setBooklistid(int booklistid) {
this.booklistid = booklistid;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getBooktitle() {
return booktitle;
}
public void setBooktitle(String booktitle) {
this.booktitle = booktitle;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getRack() {
return rack;
}
public void setRack(String rack) {
this.rack = rack;
}
public int getTcopies() {
return tcopies;
}
public void setTcopies(int tcopies) {
this.tcopies = tcopies;
}
public java.sql.Date getDateAdded() {
return dateAdded;
}
public void setDateAdded(java.sql.Date dateAdded) {
this.dateAdded = dateAdded;
}
public CategoryEntity getCategoryid() {
return categoryid;
}
public void setCategoryid(CategoryEntity categoryid) {
this.categoryid = categoryid;
}
public UserEntity getAddedBy() {
return addedBy;
}
public void setAddedBy(UserEntity addedBy) {
this.addedBy = addedBy;
}
public UserEntity getModifiedBy() {
return modifiedBy;
}
public void setModifiedBy(UserEntity modifiedBy) {
this.modifiedBy = modifiedBy;
}
public java.util.Date getAddedate() {
return addedate;
}
public void setAddedate(java.util.Date addedate) {
this.addedate = addedate;
}
public java.util.Date getModifiedDate() {
return modifiedDate;
}
public void setModifiedDate(java.util.Date modifiedDate) {
this.modifiedDate = modifiedDate;
}
// public String getAccessionnumber() {
// return accessionnumber;
// }
//
// public void setAccessionnumber(String accessionnumber) {
// this.accessionnumber = accessionnumber;
// }
public String getEdition() {
return edition;
}
public void setEdition(String edition) {
this.edition = edition;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getBillno() {
return billno;
}
public void setBillno(String billno) {
this.billno = billno;
}
public String getCallno() {
return callno;
}
public void setCallno(String callno) {
this.callno = callno;
}
public java.sql.Date getBilldate() {
return billdate;
}
public void setBilldate(java.sql.Date billdate) {
this.billdate = billdate;
}
public String getPublishedyear() {
return publishedyear;
}
public void setPublishedyear(String publishedyear) {
this.publishedyear = publishedyear;
}
// public Set< LibraryBarCodeEntity> getChield() {
// return chield;
// }
//
// public void setChield(Set< LibraryBarCodeEntity> chield) {
// this.chield = chield;
// }
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.treamis.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
/**
*
* @author admin
*/
@Entity
@Table(name = "LibraryBarCodeTable")
public class LibraryBarCodeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "S_no", nullable = false)
private int S_no;
@Column(name = "BookBarCode", nullable = false)
private String barCode;
private String accessno;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "BookListId", referencedColumnName = "BookListId")
@Cascade({CascadeType.ALL})
private LibraryBookListEntity parent;
public LibraryBookListEntity getParent() {
return parent;
}
public void setParent(LibraryBookListEntity parent) {
this.parent = parent;
}
public int getS_no() {
return S_no;
}
public void setS_no(int S_no) {
this.S_no = S_no;
}
public String getBarCode() {
return barCode;
}
public void setBarCode(String barCode) {
this.barCode = barCode;
}
public String getAccessno() {
return accessno;
}
public void setAccessno(String accessno) {
this.accessno = accessno;
}
}
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]);
mb.setParent(lbl);
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.saveOrUpdate(lbl);
tx.commit();