这里先贴出主要代码:
这是多的一方
public class BookBarCode {
private String bookID;
private BookInfo bookInfo;
public String getBookID() {
return bookID;
}
public void setBookID(String bookID) {
this.bookID = bookID;
}
public BookInfo getBookInfo() {
return bookInfo;
}
public void setBookInfo(BookInfo bookInfo) {
this.bookInfo = bookInfo;
}
@Override
public String toString() {
return “BookBarCode{” +
“bookID=”” + bookID + “\”” +
“, bookInfo=” + bookInfo +
“}”;
}
}
一的一方
public class BookInfo {
private String ISBN;
private String bookName;
private String press; //出版社
private String writer;
private String content;
private double price;
private Integer volumeOfConpies;
private Integer bookStock;
private String photo;
private Integer bookType;
private Set bookBarCode = new HashSet<>();
省略getter和setter
一下为配置文件:
一方:<class name=”com.uiyllong.bms.javabean.BookInfo” table=”bms_bookinfo”>
<id name=”ISBN”>
<generator class=”assigned”/>
</id>
<property name=”bookName”/>
<property name=”press”/>
<property name=”writer”/>
<property name=”content”/>
<property name=”price”/>
<property name=”volumeOfConpies”/>
<property name=”bookStock”/>
<property name=”photo”/>
<property name=”bookType”/>
<set name=”bookBarCode” inverse=”false” cascade=”all”>
<key column=”ISBN”></key>
<one-to-many class=”com.uiyllong.bms.javabean.BookBarCode”/>
</set>
</class>
多方:<class name=”com.uiyllong.bms.javabean.BookBarCode” table=”bms_bookbarcode”>
<id name=”bookID”>
<generator class=”assigned”/>
</id>
<many-to-one name=”bookInfo” class=”com.uiyllong.bms.javabean.BookInfo” column=”ISBN” cascade=”all”/>
</class>
删除处理:public String deleteBook() {
if (bookDao.deleteBook(bookInfo.getISBN())) {
addFieldError(“delete_success”, “删除成功!”);
} else {
addFieldError(“delete_error”, “删除失败!”);
}
return SUCCESS;
}
public boolean deleteBook(String ISBN) {
Transaction ts = BaseDao.getSession().beginTransaction();
try {
//BaseDao.getclose();
BookInfo bookInfo = BaseDao.getSession().get(BookInfo.class, ISBN);
BaseDao.getSession().delete(bookInfo);
ts.commit();
return true;
} catch (Exception e) {
ts.commit();
return false;
} finally {
if (ts != null) {
ts = null;
BaseDao.getclose();
}
}
本人本人debug了很多次出现异常:Illegal attempt to associate a collection with two open sessions. Collection : [com.uiyllong.bms.javabean.BookInfo.bookBarCode#978-8-121-25234-9]
求各位高手看看,指点一下
这是多的一方
public class BookBarCode {
private String bookID;
private BookInfo bookInfo;
public String getBookID() {
return bookID;
}
public void setBookID(String bookID) {
this.bookID = bookID;
}
public BookInfo getBookInfo() {
return bookInfo;
}
public void setBookInfo(BookInfo bookInfo) {
this.bookInfo = bookInfo;
}
@Override
public String toString() {
return “BookBarCode{” +
“bookID=”” + bookID + “\”” +
“, bookInfo=” + bookInfo +
“}”;
}
}
一的一方
public class BookInfo {
private String ISBN;
private String bookName;
private String press; //出版社
private String writer;
private String content;
private double price;
private Integer volumeOfConpies;
private Integer bookStock;
private String photo;
private Integer bookType;
private Set bookBarCode = new HashSet<>();
省略getter和setter
一下为配置文件:
一方:<class name=”com.uiyllong.bms.javabean.BookInfo” table=”bms_bookinfo”>
<id name=”ISBN”>
<generator class=”assigned”/>
</id>
<property name=”bookName”/>
<property name=”press”/>
<property name=”writer”/>
<property name=”content”/>
<property name=”price”/>
<property name=”volumeOfConpies”/>
<property name=”bookStock”/>
<property name=”photo”/>
<property name=”bookType”/>
<set name=”bookBarCode” inverse=”false” cascade=”all”>
<key column=”ISBN”></key>
<one-to-many class=”com.uiyllong.bms.javabean.BookBarCode”/>
</set>
</class>
多方:<class name=”com.uiyllong.bms.javabean.BookBarCode” table=”bms_bookbarcode”>
<id name=”bookID”>
<generator class=”assigned”/>
</id>
<many-to-one name=”bookInfo” class=”com.uiyllong.bms.javabean.BookInfo” column=”ISBN” cascade=”all”/>
</class>
删除处理:public String deleteBook() {
if (bookDao.deleteBook(bookInfo.getISBN())) {
addFieldError(“delete_success”, “删除成功!”);
} else {
addFieldError(“delete_error”, “删除失败!”);
}
return SUCCESS;
}
public boolean deleteBook(String ISBN) {
Transaction ts = BaseDao.getSession().beginTransaction();
try {
//BaseDao.getclose();
BookInfo bookInfo = BaseDao.getSession().get(BookInfo.class, ISBN);
BaseDao.getSession().delete(bookInfo);
ts.commit();
return true;
} catch (Exception e) {
ts.commit();
return false;
} finally {
if (ts != null) {
ts = null;
BaseDao.getclose();
}
}
本人本人debug了很多次出现异常:Illegal attempt to associate a collection with two open sessions. Collection : [com.uiyllong.bms.javabean.BookInfo.bookBarCode#978-8-121-25234-9]
求各位高手看看,指点一下
解决方案
10
:Illegal attempt to associate a collection with two open sessions.
有俩session,看看你的session是怎么管理的
有俩session,看看你的session是怎么管理的
10
Transaction ts = BaseDao.getSession().beginTransaction(); 这句去掉 或 直接 ts.get(BookInfo.class, ISBN);