Problem with Entity update with jpa/hibernate

守給你的承諾、 提交于 2021-02-07 20:44:36

问题


I have this entity class, called "Pagina" and I want to update the entry in the database based on the changes made to the entity. This isn't working. I get no errors, but the db is not changed.

@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 a methods defined in a class that contains methods that work on the entity. This method use the query "findAll". The method's code is the following:

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'm 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 watch the db, nothing has changed after this operations. My persistence.xml has the entry " " Can you help me? I don't know how to solve this... I can change things if needed.


回答1:


Think of Hibernate as a big cache that can use a DB as a "store" where it puts things that don't fit into the cache anymore. Hibernate will not flush everything to the DB as you change it, it will wait. Chances are that you might change more than a single field in an object.

So you need to flush the session (em.flush()), or you must run a query, or you must commit the current transaction (not an easy option when using Spring).




回答2:


Changes are not flushed to the database immediately when you set the value of a property. It happens at the time the transaction is committed (or earlier in some cases). Try committing the transaction and then check the database.



来源:https://stackoverflow.com/questions/1328935/problem-with-entity-update-with-jpa-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!