Hello, dear collegues. Let me introduce my classes (in code) first to understand the problem
This is abstract class with authorUuid field. That field is about user identifier, who creates/updates/deletes entity. This field must be filled before execution query to database.
public abstract class BaseVersionedEntity extends BaseArchivableEntity implements Versioned {
/**
* Author UUID.
*/
@ExportIgnore
@Column(name = "author_uuid")
private UUID authorUuid;
}
We have User, which may be in many groups. Group class doesn’t have @ManyToMany annotation to users
@Entity
@Table(name = AmUser.TABLE_NAME)
public class AmUser extends BaseVersionedEntity {
public static final String TABLE_NAME = "am_user";
@Id
@SequenceGenerator(name = AM_SEQ_GENERATOR, sequenceName = AM_SEQUENCE_NAME, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = AM_SEQ_GENERATOR)
@Column(nullable = false, updatable = false)
private Long id;
@ManyToMany
@JoinTable(name = "am_user_group",
joinColumns = @JoinColumn(name = "am_user_id"),
inverseJoinColumns = @JoinColumn(name = "am_group_id"))
private Set<AmGroup> amGroups;
}
And we have entity for cross table
@Entity
@Table(name = AmUserGroup.TABLE_NAME)
public class AmUserGroup extends BaseVersionedEntity {
public static final String TABLE_NAME = "am_user_group";
@Id
@SequenceGenerator(name = AM_SEQ_GENERATOR, sequenceName = AM_SEQUENCE_NAME, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = AM_SEQ_GENERATOR)
@Column(nullable = false, updatable = false)
private Long id;
@Column(name = "am_user_id", nullable = false, updatable = false)
private Long amUserId;
@Column(name = "am_group_id", nullable = false, updatable = false)
private Long amGroupId;
}
The main problem is to fill ‘authorUuid’ field in ‘am_user_group’ table, because right now it fills using directional request to DB (Postgresql) by hibernate like "insert into am_user_group values(?,?).
Ideas, that works:
- Remove @ManyToMany link and send requests to database by ourselves. But it leads to fetching groups in additional requests, when we have users
- Add Sql Inresceptor, that add author id to sql prepared by hibernate
Also, I assume that you can turn off the automatic creation of an entity in cross table (am_user_group) and don’t remove @ManyToMany annotation. But how?
Maybe more relevant solution exists for this case. Could you please advise?