Home > database >  cascade = CascadeType.ALL doesn´t work on OneToOne relation
cascade = CascadeType.ALL doesn´t work on OneToOne relation

Time:08-14

I have one problem with One to One relation.

I have one USER that has only one TEAM

User Entity


@Entity
@Table(name = "USER")
@Data @NoArgsConstructor @AllArgsConstructor @Builder
public class User implements Serializable {

    private static final long serialVersionUID = 3233149207833106460L;
    
    @Id
    @GeneratedValue
    @Column(name = "USERID")
    private Long id;
    
    ...
 
    @OneToOne(mappedBy = "user")
    private Team team;

}

Team Entity


    

@Entity
@Table(name = "TEAM")
@Data @NoArgsConstructor @AllArgsConstructor @Builder
public class Team implements Serializable {

    private static final long serialVersionUID = 3233149207833106460L;
    
    @Id
    @GeneratedValue
    @Column(name = "TEAMID")
    private Long id;

...
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "USERID")
    private User user;
    
    
}

This is the way I´m trying to create object.


    
        //create Team and add to User
        Team team = Team
                .builder()
                .value(TeamConstants.INITIAL_VALUE)
                .name(userDetailsForm.getUsername() " " TeamConstants.TEAM)
                .country(userDetailsForm.getUsername() " " TeamConstants.COUNTRY)
                .build();

        
        User user = User
                .builder()
                .username(userDetailsForm.getUsername())
                .password(this.passwordEncoder.encode(userDetailsForm.getPassword()))
                .email(userDetailsForm.getEmail())
                .registerDate(DateUtil.getNow())
                .role(userDetailsForm.getRole())
                .team(team)
                .build();
        
        team.setUser(user);
        
        this.userRepository.save(user);

And when I try to save user (and create automatically the son team), it gaves me the error

Blockquote org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.soccermanager.business.entities.user.User.team -> com.soccermanager.business.entities.team.Team;

Any idea with the solution?

This is my first question here, I hope its correct.

Thanks for your time

CodePudding user response:

please interact with Team entity then user will be change follow CascadeType

or you need add @OneToOne(mappedBy = "user", cascade = CascadeType.ALL) private Team team; in User class.

Please try both and you can see the different. Hope this post answer can help with you

CodePudding user response:

You should save your team entity before your user. You should do as you do only if you are working in the same JPA Transaction unit. If not, it will not work and you get that exception. Try adding @Transactional annotation on the method, with optional params like propagation = Propagation.REQUIRED and readonly=false because you are creating/updating objects here :

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
protected User saveTeamWithUser(UserDetailsForm form) {
    //    ...
}
  • Related