Question

Je vais avoir ce problème étrange où mes fonctions de fusion () ou mes persistent () ne sont pas pris en compte dans la base de données. Mon 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);
    }
}

Mon getProductList() fonctionne très bien mais mon saveProduct ne fait rien à la base de données.

Pouvez-vous penser à des raisons pour lesquelles cela se produire?

Je posterai mon fichier 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();
    }
}
Était-ce utile?

La solution

essayer d'appeler la méthode flush () après EntityManager persistent ().

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

Autres conseils

cela peut ne pas être la réponse dont vous avez besoin mais, puisque vous utilisez déjà le printemps, pourquoi ne pas simplifier votre vie DAO en utilisant les modèles de persistance de printemps (par exemple jpaTemplate). Le regard de mon DAO comme ceci:

    @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);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top