简体   繁体   中英

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. Other functions of this class work such as 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. 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. In order to fix this I added cascade=CascadeType.ALL to the list of one to many annotations attribute like so.

@OneToMany(mappedBy = "user", cascade= CascadeType.ALL)
private List<UserHolding> holdings;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM