简体   繁体   English

数据无法持久保存到数据库Spring Boot休眠中

[英]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.

相关问题 “BasicBatchConfigurer”具有受保护的访问权限 - Spring Batch/Spring Boot 未将数据持久保存到数据库 - "BasicBatchConfigurer" has protected access - Spring Batch/Spring Boot not persisting data to database 使用Spring Framework,Hibernate和JPA坚持使用MySQL失败,无法真正持久化到数据库 - Persisting to MySQL using Spring Framework, Hibernate and JPA failed, not really persisting to the database Spring + Hibernate - 持久/提交数据不起作用 - Spring + Hibernate - Persisting/Committing data doesn't work 在持久化、Hibernate、Spring数据之前获取生成的实体Id - Get generated entity Id before persisting, Hibernate, Spring data 休眠:持续存在数据问题 - Hibernate: Persisting data issue Spring Boot + Postgres + JPA + Heroku无法持久存储数据并且无法记录日志 - Spring boot + Postgres + JPA + Heroku not persisting data and not logging 如何使用Hibernate处理Spring Boot中的数据库迁移? - How to handle database migrations in Spring Boot with Hibernate? 在 spring 引导和 hibernate 中使用@Transactional 获取数据库死锁 - Getting database deadlock with @Transactional in spring boot and hibernate Spring 引导测试上下文未加载:Flyway 和 Spring 引导数据/休眠 - Spring Boot test context not loading: Flyway and Spring Boot Data/Hibernate Java Spring boot hibernate 删除级联数据 - Java Spring boot hibernate delete cascade data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM