Issue updating Entity with jpa / hibernate

I have this entity class called "Pagina" and I want to update a record in the database based on the changes made to the object. This does not work. I get no errors, but the db doesn't change.

@Entity
@Table(name = "PAGINA")
@NamedQueries({@NamedQuery(name = "Pagina.findAll", query = "SELECT p FROM Pagina p"), 
@NamedQuery(name = "Pagina.findHashByURL", query= "SELECT p.chash FROM Pagina p WHERE p.url = :url"),
@NamedQuery(name = "Pagina.findTimestampByURL", query= "SELECT p.timestamp FROM Pagina p WHERE p.url = :url"),
@NamedQuery(name = "Pagina.findByUrl", query = "SELECT p FROM Pagina p WHERE p.url = :url"),
@NamedQuery(name = "Pagina.findByProfondita", query = "SELECT p FROM Pagina p WHERE p.profondita = :profondita"),
@NamedQuery(name = "Pagina.findByIntervallo", query = "SELECT p FROM Pagina p WHERE p.intervallo = :intervallo"),
@NamedQuery(name = "Pagina.findByTimestamp", query = "SELECT p FROM Pagina p WHERE p.timestamp = :timestamp"), 
@NamedQuery(name="Pagina.findAllURL", query="SELECT p.url FROM Pagina p"),
@NamedQuery(name="Pagina.findDelayByURL", query="SELECT p.intervallo FROM Pagina p WHERE p.url = :url"),
@NamedQuery(name = "Pagina.findByUpdated", query = "SELECT p FROM Pagina p WHERE p.updated = :updated")})
public class Pagina implements Serializable, Delayed{
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "URL")
    private String url;
    @Column(name = "PROFONDITA")
    private Integer profondita;
    @Column(name = "INTERVALLO")
    private Integer intervallo;
    @Lob
    @Column(name = "CHASH")
    private String chash;
    @Column(name = "TIMESTAMP")
    private Integer timestamp;
    @Column(name = "UPDATED")
    private Boolean updated;


[cut]

      

In another class, I retrieve the objects stored in the database (mysql) using the methods defined in the class that contains the methods that work with the entity. This method uses the "findAll" request. The method code is as follows:

public List<Pagina> findAll(){
EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("CrawlerPU");
        EntityManager em = emf.createEntityManager();  
List o = (List) em.createNamedQuery("Pagina.findAll").getResultList();
        return o;
    }

      

I am using this method, as you can see in the code:

 PaginaMethods p = new PaginaMethods();

        List<Pagina> l = p.findAll();
   while (it.hasNext()) {
            Pagina s = (Pagina) it.next();
            s.setUpdated(Boolean.TRUE);

      

If I look at the db, nothing has changed after this operation. My persistence.xml has an entry "" Can you help me? I don't know how to solve this ... I can change the situation if necessary.

+2


source to share


2 answers


Think of Hibernate as a large cache that can use the DB as "storage" where it doesn't fit into the cache anymore. Hibernation will not dump everything in the DB as it changes, it will wait. Most likely you can change more than one field in an object.



So, you need to clear the session ( em.flush()

), or you have to start a request, or you have to commit the current transaction (not an easy option when using Spring).

+6


source


Changes are not flushed to the database immediately after setting the property value. This happens at the time of the transaction (or earlier in some cases). Try to execute a transaction and then check the database.



0


source







All Articles