简体   繁体   English

在SpringBoot 2.0.4应用程序中刷新之前保存临时实例

[英]save the transient instance before flushing in SpringBoot 2.0.4 app

I have a basic SpringBoot 2.0.4.RELEASE app. 我有一个基本的SpringBoot 2.0.4.RELEASE应用程序。 using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. 使用Spring Initializer,JPA,嵌入式Tomcat,Thymeleaf模板引擎并将其打包为可执行JAR文件。

I have this method: 我有这种方法:

@Transactional
    public User createUser(User user, Set<UserRole> userRoles) {

        User localUser = userRepository.findByEmail(user.getEmail());

        if (localUser != null) {
            LOG.info("User with username {} and email {} already exist. Nothing will be done. ",
                    user.getUsername(), user.getEmail());
        } else {

            String encryptedPassword = passwordEncoder.encode(user.getPassword());
            user.setPassword(encryptedPassword);


            for (UserRole ur : userRoles) {

                LOG.info("Saving role " + ur);

                roleRepository.save(ur.getRole());
            }

            user.getUserRoles().addAll(userRoles);

            localUser = userRepository.save(user);

        }

        return localUser;
    }

but when I create a User: 但是当我创建一个用户时:

 User user = new User();



        Set<UserRole> userRoles = new HashSet<>();
        userRoles.add(new UserRole(user, new Role(RolesEnum.ADMIN)));
        userService.createUser(user, userRoles);

I got this error: 我收到此错误:

object references an unsaved transient instance - save the transient instance before flushing : com.tdk.backend.persistence.domain.backend.UserRole.role -> com.tdk.backend.persistence.domain.backend.Role

Include cascade="all" (if using xml) or cascade=CascadeType.ALL (if using annotations) on your collection mapping. 在集合映射中包括cascade =“ all” (如果使用xml)或cascade = CascadeType.ALL (如果使用注释)。

Because you have a collection in your entity, and that collection has one or more items which are not present in the database. 因为您的实体中有一个集合,并且该集合具有数据库中不存在的一项或多项。 Using above option you tell hibernate to save them to the database when saving their parent. 使用以上选项,您告诉hibernate在保存其父级时将其保存到数据库。

This happens when saving an object when Hibernate thinks it needs to save an object that is associated with the one you are saving. 当Hibernate认为需要保存与要保存的对象关联的对象时,在保存对象时会发生这种情况。

also before saving the Role into database, you are saving the user which is wrong because hibernate session is not aware of that object till that point of time.So to get rid of this error use CascadeType.ALL on mappings. 同样,在将角色保存到数据库之前,您正在保存用户,这是错误的,因为休眠会话直到该时间点才知道该对象。因此要消除此错误,请在映射上使用CascadeType.ALL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在刷新之前保存临时实例 - save the transient instance before flushing TransientObjectException:在刷新之前保存临时实例 - TransientObjectException: save the transient instance before flushing “在刷新之前保存瞬态实例”错误 - “Save the transient instance before flushing ” error ManyToMany关系:刷新之前保存临时实例 - ManyToMany relation: save the transient instance before flushing JPA在刷新之前保存临时实例 - JPA save the transient instance before flushing 对象引用未保存的瞬态实例-在刷新之前保存瞬态实例 - object references an unsaved transient instance - save the transient instance before flushing 对象引用一个未保存的瞬态实例,在刷新之前保存瞬态实例: - Object references an unsaved transient instance save the transient instance before flushing: Spring JPA ManyToOne - 在刷新之前保存瞬态实例 - Spring JPA ManyToOne - Save the transient instance before flushing 使用 Hibernate 保存 object object 引用未保存的瞬态实例 在刷新之前保存瞬态实例 - save the object using Hibernate object references an unsaved transient instance save the transient instance before flushing “对象引用了一个未保存的瞬态实例-在刷新之前保存该瞬态实例”导致没有INSERT - “object references an unsaved transient instance - save the transient instance before flushing” Caused no INSERT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM