I have a Spring Boot application written in Java and following entities (tried to simplify to most important parts):
@Getter
@Setter
@Entity
@Table(name = "USR_USERS")
public class User implements Serializable {
private static final long serialVersionUID = 3034262744304428805L;
@Id
@Column(name = "USR_ID")
private Long id;
@BatchFetch(BatchFetchType.IN)
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "ADD_USR_ID", referencedColumnName = "USR_ID")
private List<UserAddress> userAddresses;
}
and
@Getter
@Setter
@Entity
@Table(name = "ADD_ADDRESSES")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserAddress implements Serializable {
private static final long serialVersionUID = 3477102783604432455L;
@Id
@Column(name = "ADD_ID")
private Long id;
@Column(name = "ADD_USR_ID", nullable = false)
private Long userId;
}
I create new user entity with address. During the transaction I call entityManager.flush()
. Sometimes it fails with the following error:
org.postgresql.util.PSQLException: ERROR: column "add_usr_id" specified more than oncen Position: 104nError Code: 0nCall: INSERT INTO ADD_ADDRESSES (ADD_USR_ID, ADD_ID, ADD_USR_ID) VALUES (?, ?, ?)ntbind => [3 parameters bound]nQuery: InsertObjectQuery(...)
The error is not always reproducible. For example, it starts occuring and then disappears on application restart. In the database the column is not duplicated. What can be a reason for such error?