我遇到了这个奇怪的问题,我的 merge() 或 persist() 函数没有反映在数据库中。我的 JdbcProductDao.java:
@Repository("productDao")
public class JdbcProductDao implements ProductDao {
@PersistenceContext
private EntityManager entityManager;
public JdbcProductDao(){
}
public Product getReference(Product product){
return getEntityManager().getReference(product.getClass(),product.getId());
}
public void persist(Product product){
getEntityManager().persist(product);
}
public EntityManager getEntityManager(){
return entityManager;
}
public void setEntityManager(EntityManager entityManager){
this.entityManager = entityManager;
}
@SuppressWarnings("unchecked")
public List<Product> getProductList(){
return getEntityManager().createQuery("from Product").getResultList();
}
public void saveProduct(Product product){
persist(product);
}
}
我的getProductList()
工作正常,但是我的 saveProduct 对数据库没有任何作用。
你能想到会发生这种情况的任何原因吗?
我将发布我的 Product.java 文件:
@Entity
@Table(name="products")
public class Product implements Serializable {
@Id
@Column(name = "id")
private int id;
@Column(name = "description")
private String description;
@Column(name = "price")
private Double price;
public void setId(int i) {
id = i;
}
public int getId() {
return id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("Description: " + description + ";");
buffer.append("Price: " + price);
return buffer.toString();
}
}