开发者

JPA reads but doesn't persists

My Java Web App reads data from database but when I try to write something, JPA says ok, but the database does not change. I call "merge" method and the data are not being saved on database, only in memory. I can do a SELECT direct into database and see old data. But the Java Console does not throw any Exception as you can see above.

Java Console:

INFO: [EL Finest]: 2011-10-14 15:02:41.847--UnitOfWork(13027895)--Thread(Thread[http-thread-pool-8080-(6),10,Grizzly])--Merge clone with references user1

Change Password's method that is being called:

public static User changePassword(String username, String oldPassword, String newPassword){
        User user = userFacade.find(username);
        if(user != null && user.getPassword().equals(oldPassword)){
            user.setPassword(newPassword);
            userFacade.edit(user); // supposed to save the new password on database, but it's not
            // at this point, the user has a new password on memory, but on database the password is still the old one
            return user;
        }
        return null;
    }

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="APP1PU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.company.User</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/app1"/>
            <property name="javax.persistence.jdbc.password" value="12345"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="eclipselink.logging.level" value="FINEST"/>
        </properties>
    </persistence-unit>
</persistence&开发者_开发技巧gt;

User.java:

@Entity
@Table(name = "user")
@Cache (
     type=CacheType.NONE
     )
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Basic(optional = false)
    private String username;

    @Basic(optional = false)
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

UserFacade.java

public class UserFacade {
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("APP1PU");

    private EntityManager em;

    protected EntityManager getEntityManager() {
        if( em == null ){
            em = emf.createEntityManager();
        }
        return em;
    }

    public void create(User entity) {
        getEntityManager().persist(entity);
    }

    public void edit(User entity) {
        getEntityManager().merge(entity);
    }

    public void remove(User entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public User find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

}

Anyone can figure why this behavior? Or has some suggestion of a test I could do?

Thanks in advance!


Try entityManager.flush() after a write operation. Or start a transaction (entityManager.getTransaction().begin()) before your write operation, and then close it at the end (entityManager.getTransaction().commit())

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜