@AbhishekYadav sorry but this is not a Spring forum; the only thing I can say is your Author entity looks fine, even though you’re using Lombok which is not a good idea to mix to your entity mappings, but unless you have a problem specific to Hibernate ORM I doubt anyone here will be able to help you.
Hi, I’m facing the same issue after migrating to spring-boot 3.5.4.
Token entity:
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@Entity
@Table(name = "tbl_tokens")
public class Token {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private User user;
}
User entity:
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@Entity
@Table(name = "tbl_users")
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "user", cascade = CascadeType.REMOVE)
private Set<Token> tokens;
}
Service implementation (incomplete but you’ll get the idea.):
@Override
public ResponseEntity<Something> login() {
Token accessToken, refreshToken;
accessToken = service.generateAccessToken();
refreshToken = service.generateRefreshToken();
accessToken.setUser(user);
refreshToken.setUser(user);
tokenRepository.saveAll(List.of(accessToken, refreshToken));
}
PS: I’m still in the development of the project I’m currently working on, and I truncated the tbl_tokens table hoping it would fix the issue.
Are you by any chance assigning a value to the generated id attribute/field and expect your save method to just magically store the data? If that is the case, then I have to disappoint you, you can’t assign values to a generated attribute as that would pose a risk of producing unique constraint violations.
I already fixed the issue; thanks for the reply. To answer your question, I did assign a value to the generated id and changing it to null fix the issue.
Yeah, so this was discussed multiple times here already and is mentioned in the migration guide: hibernate-orm/migration-guide.adoc at 6.6 · hibernate/hibernate-orm · GitHub