EntityManager合并/持久问题
我遇到了这个奇怪的问题,我的 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();
}
}
I'm having this strange problem where my merge() or my persist() functions are not being reflected in the database.
My 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);
}
}
My getProductList()
works fine however my saveProduct doesn't do anything to the database.
Can you think of any reasons why this would be happening?
I'll post my Product.java file:
@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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在EntityManager persist()之后调用flush()方法。
try to call flush() method after EntityManager persist().
这可能不是您需要的答案,但是,由于您已经在使用 Spring,为什么不通过使用 Spring 持久性模板(例如 jpaTemplate)来简化您的 DAO 生活。我的 DAO 看起来像这样:
this may not be the answer you need but, since you are using Spring already, why not simplify your DAO life by using the Spring persistence templates (e.g. jpaTemplate). My DAO's look like so: