[英]Data not persisting to Database Spring Boot hibernate
I'm attempting to update an Entity called User and although the changes are made to the user object successfully, when I attempt to save the user (via the update method) to the database it does not persist. 我正在尝试更新一个名为User的实体,尽管成功地对用户对象进行了更改,但是当我尝试将用户(通过update方法)保存到数据库时,它不会持久存在。 Other functions of this class work such as get(). 该类的其他功能也可以使用,例如get()。 No exceptions appear. 没有例外出现。
-----------------------------AbstractDAOImpl.class-----------------------------------
@Transactional
@Repository
public abstract class AbstractDaoImpl<T> {
private static final Logger LOG = Logger.getLogger(StockController.class);
private Class currentClass;
@PersistenceContext(type = PersistenceContextType.EXTENDED)
protected EntityManager entityManager;
protected void setThisClass(Class currentClass) {
this.currentClass = currentClass;
}
@SuppressWarnings("unchecked")
public T get(String id) {
return (T) entityManager.find(currentClass, id);
}
public void delete(String id) {
entityManager.remove(get(id));
}
public void update(T t) {
entityManager.merge(t);
entityManager.flush();
}
@SuppressWarnings("unchecked")
public List list(String tableName) {
return entityManager.createQuery("from " + tableName, currentClass).getResultList();
}
}
------------------------Application.properties------------------------
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.username=SYSTEM
spring.datasource.password=password
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.jpa.database-platform=Oracle11gDialect
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle8iDialect
spring.resources.static-locations=file:src/main/resources/
spring.resources.cache-period=0
spring.thymeleaf.cache=false
spring.http.converters.preferred-json-mapper=jackson
security.basic.enabled=false
security.headers.content-type=true
security.enable-csrf=true
security.basic.path=/**
I have made various attempts to implement other fixes listed on here with no luck, including getting the transaction from the entity manager beginning and ending it with the current contents of update() in between. 我已经进行了各种尝试来实现此处列出的其他修复程序,但是没有碰到任何麻烦,包括从实体管理器开始进行事务处理,并以介于两者之间的update()当前内容结束事务。 If other information is needed such as the entity classes please let me know and I'll edit my post. 如果需要其他信息(例如实体类),请告诉我,我将对其进行编辑。
The issue wasn't that my User object wasn't being persisted, it was that the List of another entity inside it called holdings wasn't. 问题不是我的User对象没有被保留,而是它里面的另一个实体馆藏的列表不是。 In order to fix this I added cascade=CascadeType.ALL
to the list of one to many annotations attribute like so. 为了解决这个问题,我像这样在一对多注释属性列表中添加了cascade=CascadeType.ALL
。
@OneToMany(mappedBy = "user", cascade= CascadeType.ALL)
private List<UserHolding> holdings;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.