views:

870

answers:

2

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();
    }
}
+1  A: 

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);
}
darren
+3  A: 

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

public void persist(Product product){
    getEntityManager().persist(product);
    getEntityManager().flush();
}
Yuri.Bulkin
thank you for the comment but I decided to just switch to using jpadaosupport
Albinoswordfish