Hello, when saving the entity User ( login != null ) I get an error, tell me what's wrong?
There is a users table and login , in the users table there is a login_id column (many users may have one login_id value)
Entity and communication
@Entity(name = "users") public class User { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.AUTO) private long id; @MapsId("uuid") @JoinColumn(name = "login") @ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER) private Login login; } @Entity(name = "login") public class Login { @Id @Column(name = "uuid") @GeneratedValue(strategy = GenerationType.AUTO) private long id; } I CrudRepository through CrudRepository , an error:
org.hibernate.PersistentObjectException: detached entity passed to persist: com.entity.Login
CrudRepository? Something I do not remember in JPA such a thing. How your user and login are created, how they are saved by JPA. Can you bring a piece ofCrudRepositorythat saves? In general, your login = new Login ()? Then probably you need to make Cascade = {CascadeType.MERGE, CascadeType.PERSIST} And even better, do everything manually, without relying on JPA magic, as it is accepted in RDBMS. Magic does not always work as it should, for the most part it is quackery. Created, saved login, Created user, assigned to him already saved login, saved. - SergeyCascadeType.MERGEthen a newUsercan be saved ifLoginwith the specifiedidis in the database, but if not - an error, the same behavior forCascadeType.DETACH & CascadeType.REFRESHIf you save the newUserand the newLogin(do not specifyid) everything will be saved to the database (only withCascadeType.ALLorCascadeType.PERSIST), but will not work if you specifyidforLogin- Bohdan Korinnyi