title: Spring JPA-I get error that object is not persisted even though i have persisted it
body: <p>I am using Spring boot-I have 3 classes <code>User</code>,<code>Role</code> and <code>UserRole</code>.I have pesisted both role object and user object but i get error that role object is not persisted.The mappings- between <code>User</code> and <code>UserRole</code> is OneToMany ,between <code>Role</code> and  <code>UserRole</code> OneToMany.In the <code>UserServiceImpl</code> class i have persisted Role object   <code>roleRepository.save(userRole.getRole());</code>
  Error is-</p>
  <pre><code>Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.bookstore.domain.security.UserRole.role -&gt; com.bookstore.domain.security.Role
  
  
  @Entity
  public class User implements UserDetails,Serializable {
      
      private static final long serialVersionUID=157954L;
      
      
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = &quot;Id&quot;,nullable = false,updatable = false)
      private Long id;
      
      private String userName;
      private String password;
      private String firstName;
      private String lastName;
      private String email;
      private String phone;
      private boolean enabled;
      
      @OneToMany(mappedBy = &quot;user&quot;,cascade = CascadeType.ALL,fetch = FetchType.EAGER)
      @JsonIgnore
      private Set&lt;UserRole&gt; userRoles=new HashSet&lt;UserRole&gt;();
      
      
      public Long getId() {
          return id;
      }
      public void setId(Long id) {
          this.id = id;
      }
      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;
      }
      public String getFirstName() {
          return firstName;
      }
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
      public String getLastName() {
          return lastName;
      }
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
      public String getEmail() {
          return email;
      }
      public void setEmail(String email) {
          this.email = email;
      }
      public String getPhone() {
          return phone;
      }
      public void setPhone(String phone) {
          this.phone = phone;
      }
      public void setEnabled(boolean enabled) {
          this.enabled = enabled;
      }
      public Set&lt;UserRole&gt; getUserRoles() {
          return userRoles;
      }
      public void setUserRoles(Set&lt;UserRole&gt; userRoles) {
          this.userRoles = userRoles;
      }
      
      @Override
      public Collection&lt;? extends GrantedAuthority&gt; getAuthorities() {
          
          Set&lt;GrantedAuthority&gt; authorities=new HashSet&lt;GrantedAuthority&gt;();
          userRoles.forEach(userRole-&gt;{
              authorities.add(new Authority(userRole.getRole().getRoleName()));
          });
          return authorities;
      }
      
      @Override
      public boolean isAccountNonExpired() {
          return true;
      }
      @Override
      public boolean isAccountNonLocked() {
          return true;
      }
      @Override
      public boolean isCredentialsNonExpired() {
          return true;
      }
      @Override
      public boolean isEnabled() {
          return enabled;
      }
      @Override
      public String getUsername() {
          return userName;
      }
      
      
  }
  
  
  @Entity
  public class Role implements Serializable{
      
      private static final long serialVersionUID=68678L;
      
      @Id
      private Long roleId;
      
      private String roleName;
  
      @OneToMany(mappedBy = &quot;role&quot;,cascade = CascadeType.ALL,fetch = FetchType.LAZY)
      @JsonIgnore
      private Set&lt;UserRole&gt; userRoles=new HashSet&lt;UserRole&gt;();
      
      public Role() {
  
      }
  
      public Long getRoleId() {
          return roleId;
      }
  
      public void setRoleId(Long roleId) {
          this.roleId = roleId;
      }
  
      public String getRoleName() {
          return roleName;
      }
  
      public void setRoleName(String roleName) {
          this.roleName = roleName;
      }
  
      public Set&lt;UserRole&gt; getUserRoles() {
          return userRoles;
      }
  
      public void setUserRoles(Set&lt;UserRole&gt; userRoles) {
          this.userRoles = userRoles;
      }
      
      
  }
  
  @Entity
  public class UserRole implements Serializable {
      
      private static final long serialVersionUID=456874L;
      
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long userRoleId;
      
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = &quot;roleId&quot;)
      private Role role;
      
      @ManyToOne(fetch = FetchType.EAGER)
      @JoinColumn(name = &quot;userId&quot;)
      private User user;
  
      public UserRole(User user,Role role) {
          this.role = role;
          this.user = user;
      }
      
  
      public UserRole() {
          super();
      }
  
  
      public Long getUserRoleId() {
          return userRoleId;
      }
  
      public void setUserRoleId(Long userRoleId) {
          this.userRoleId = userRoleId;
      }
  
      public Role getRole() {
          return role;
      }
  
      public void setRole(Role role) {
          this.role = role;
      }
  
      public User getUser() {
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
      
      
      
  }
  
  
  @Service
  public class UserServiceImpl implements UserService {
  
      private static final Logger LOG=LoggerFactory.getLogger(UserServiceImpl.class);
      
      @Autowired
      UserRepository userRepository;
      
      @Autowired
      RoleRepository roleRepository;
      
      @Transactional
      @Override
      public User CreateUser(User user, Set&lt;UserRole&gt; userRoles) {
          User localUser=userRepository.findByUserName(user.getUserName());
          
          if(localUser!=null) {
              LOG.warn(&quot;Username {} already exists&quot;,user.getUserName());
          }
          else {
      
                for(UserRole userRole:userRoles) { 
                    roleRepository.save(userRole.getRole());
                    LOG.error(&quot;inside for {}&quot;,userRole.getRole().getRoleName());
                }
                user.getUserRoles().addAll(userRoles);
          
              localUser=userRepository.save(user);
          }
          
          return  localUser;
      }
  
      
      
  }
  
  @SpringBootApplication
  public class BookStoreApplication implements CommandLineRunner {
  
      @Autowired
      UserService userService;
      
      public static void main(String[] args) {
          SpringApplication.run(BookStoreApplication.class, args);
      }
  
      @Override
      public void run(String... args) throws Exception {
          
            User user1=new User();
            user1.setUserName(&quot;test&quot;);
            user1.setPassword(SecurityUtility.passwordEncoder().encode(&quot;test&quot;));
            user1.setEmail(&quot;test@test.com&quot;);
            user1.setEnabled(true);
            user1.setFirstName(&quot;testFirstName&quot;);
            user1.setLastName(&quot;testLastName&quot;);
            user1.setPhone(&quot;123456789&quot;); 
            Role role1=new Role();
            role1.setRoleId((long)1); 
            role1.setRoleName(&quot;ROLE_USER&quot;);
            UserRole userRole1=new
            UserRole(user1,role1);
            Set&lt;UserRole&gt; userRoles1=new HashSet&lt;UserRole&gt;();
            userRoles1.add(userRole1); 
      
            userService.CreateUser(user1, userRoles1);
            
            User user2=new User(); 
            user2.setUserName(&quot;admin&quot;);
            user2.setPassword(SecurityUtility.passwordEncoder().encode(&quot;admin&quot;));
            user2.setEmail(&quot;admin@admin.com&quot;);
            user2.setEnabled(true);
            user2.setFirstName(&quot;adminFirstName&quot;); 
            user2.setLastName(&quot;adminLastName&quot;);
            user2.setPhone(&quot;223456789&quot;);
            Role role2=new Role();
            role2.setRoleId((long) 2);
            role2.setRoleName(&quot;ROLE_ADMIN&quot;); 
            UserRole userRole2=new UserRole(user2,role2);
            Set&lt;UserRole&gt; userRoles2=new HashSet&lt;UserRole&gt;();
            userRoles2.add(userRole2);
      
            
            userService.CreateUser(user2, userRoles2);
           
      }
      
      
  
  }
  </code></pre>