gpt4 book ai didi

java - JPA 读取但不持久

转载 作者:行者123 更新时间:2023-11-30 09:44:40 25 4
gpt4 key购买 nike

我的 Java Web 应用程序从数据库中读取数据,但是当我尝试编写一些东西时,JPA 说可以,但数据库没有改变。我调用“合并”方法,数据没有保存在数据库中,只保存在内存中。我可以直接进入数据库并查看旧数据。但是 Java 控制台不会抛出任何异常,如您在上面所见。

Java 控制台:

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

正在调用的更改密码的方法:

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>

用户.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);
}

}

任何人都知道为什么会出现这种行为?或者对我可以做的测试有什么建议?

提前致谢!

最佳答案

在写入操作后尝试 entityManager.flush()。或者在你的写操作之前启动一个事务(entityManager.getTransaction().begin()),然后在结束时关闭它(entityManager.getTransaction().commit())

关于java - JPA 读取但不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7771874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com