EntityManager合并/持久问题

发布于 2024-08-17 16:40:22 字数 2014 浏览 9 评论 0原文

我遇到了这个奇怪的问题,我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉黛浅 2024-08-24 16:40:22

尝试在EntityManager persist()之后调用flush()方法。

public void persist(Product product){
    getEntityManager().persist(product);
    getEntityManager().flush();
}

try to call flush() method after EntityManager persist().

public void persist(Product product){
    getEntityManager().persist(product);
    getEntityManager().flush();
}
薆情海 2024-08-24 16:40:22

这可能不是您需要的答案,但是,由于您已经在使用 Spring,为什么不通过使用 Spring 持久性模板(例如 jpaTemplate)来简化您的 DAO 生活。我的 DAO 看起来像这样:

    @Override
@SuppressWarnings("unchecked")
public List<Sample> findAll() {
    return jpaTemplate.find("from Sample");
}

    @Override
public Sample findById(Long sampleId) {
    return jpaTemplate.find(Sample.class, sampleId);
}

    @Override
public Sample store(Sample dp) {
    return jpaTemplate.merge(dp);
}

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:

    @Override
@SuppressWarnings("unchecked")
public List<Sample> findAll() {
    return jpaTemplate.find("from Sample");
}

    @Override
public Sample findById(Long sampleId) {
    return jpaTemplate.find(Sample.class, sampleId);
}

    @Override
public Sample store(Sample dp) {
    return jpaTemplate.merge(dp);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文