当我使用Hibernate Session选择一组数据时,它会列出所有数据。之后,我将添加新记录,但我尝试再次获取数据,但最后我没有添加。这是我的代码。
的hibernate.cfg.xml
package com.isimple.intelijpos_lite.models;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
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.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/**
*
* @author Hashan Chamikara
*/
@Entity
@Table(name = "item")
@NamedQueries({
@NamedQuery(name = "Item.findAll", query = "SELECT i FROM Item i")})
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "barcode")
private String barcode;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "reference")
private String reference;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "name")
private String name;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "category")
private String category;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "unit")
private String unit;
@Size(max = 45)
@Column(name = "unit_description")
private String unitDescription;
@Basic(optional = false)
@NotNull
@Column(name = "expire")
private int expire;
@Basic(optional = false)
@NotNull
@Column(name = "warranty")
private int warranty;
@Basic(optional = false)
@NotNull
@Column(name = "status")
private int status;
@Basic(optional = false)
@NotNull
@Column(name = "created")
@Temporal(TemporalType.TIMESTAMP)
private Date created;
@Basic(optional = false)
@NotNull
@Column(name = "modify")
@Temporal(TemporalType.TIMESTAMP)
private Date modify;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "item", fetch = FetchType.LAZY)
private List<InvoiceDetail> invoiceDetailList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "item", fetch = FetchType.LAZY)
private List<Stock> stockList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "item", fetch = FetchType.LAZY)
private List<GrnDetail> grnDetailList;
public Item() {
}
public Item(Integer id) {
this.id = id;
}
public Item(Integer id, String barcode, String reference, String name, String category, String unit, int expire, int warranty, int status, Date created, Date modify) {
this.id = id;
this.barcode = barcode;
this.reference = reference;
this.name = name;
this.category = category;
this.unit = unit;
this.expire = expire;
this.warranty = warranty;
this.status = status;
this.created = created;
this.modify = modify;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBarcode() {
return barcode;
}
public void setBarcode(String barcode) {
this.barcode = barcode;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getUnitDescription() {
return unitDescription;
}
public void setUnitDescription(String unitDescription) {
this.unitDescription = unitDescription;
}
public int getExpire() {
return expire;
}
public void setExpire(int expire) {
this.expire = expire;
}
public int getWarranty() {
return warranty;
}
public void setWarranty(int warranty) {
this.warranty = warranty;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getModify() {
return modify;
}
public void setModify(Date modify) {
this.modify = modify;
}
public List<InvoiceDetail> getInvoiceDetailList() {
return invoiceDetailList;
}
public void setInvoiceDetailList(List<InvoiceDetail> invoiceDetailList) {
this.invoiceDetailList = invoiceDetailList;
}
public List<Stock> getStockList() {
return stockList;
}
public void setStockList(List<Stock> stockList) {
this.stockList = stockList;
}
public List<GrnDetail> getGrnDetailList() {
return grnDetailList;
}
public void setGrnDetailList(List<GrnDetail> grnDetailList) {
this.grnDetailList = grnDetailList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Item)) {
return false;
}
Item other = (Item) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.isimple.intelijpos_lite.models.Item[ id=" + id + " ]";
}
}
Item.java模型类
package com.isimple.intelijpos_lite.util;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
/**
* Hibernate Utility class with a convenient method to get Session Factory
* object.
*
* @author chamikara
*/
public class DatabaseUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
DatabaseUtil.java
public boolean saveItem(Item item, Stock stock) {
Session session = DatabaseUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
try {
session.save(item);
session.save(stock);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
} finally {
session.close();
}
return transaction.wasCommitted();
}
项目保存方法
public List<Item> searchItem(String text) {
Session session = DatabaseUtil.getSessionFactory().openSession();
List list = session.createCriteria(Item.class).add(
Restrictions.and(
Restrictions.or(
Restrictions.like("barcode", text, MatchMode.START),
Restrictions.like("reference", text, MatchMode.START),
Restrictions.like("name", text, MatchMode.ANYWHERE)),
Restrictions.eq("status", Const.ITEM_ACTIVE)))
.setMaxResults(20).list();
session.close();
return list;
}
物品清单方法
"reviewBody": "#{of:escapeJS(reviewBean.text)}"
应用程序在同一个线程中运行
添加测试2项目之前
添加项目后