我有 4 个实体一对多消息实体:
@Entity
@Table(name = "Messages")
public class Message {
...
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "messagesounds_id")
private Sound sound;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "messagevibrations_id")
private Vibration vibration;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "messagecolors_id")
private Color color;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "messageplatforms_id")
private Platform platform;
...
}
而其他 4 个实体如下所示:
@Entity
@Table( name = "MessageSounds" , uniqueConstraints=@UniqueConstraint(columnNames = {"name"}))
public class Sound {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@OneToMany(cascade={CascadeType.ALL})
@JoinColumn(name="messagesounds_id")
private Set<Message> message;
...
}
我可以成功创建引用 4 个引用表的第一条记录的第一条消息记录。但是 postgresql db 中的消息表如下所示:
id |...| messagesounds_id | messagevibrations_id | messagecolors_id | messageplatforms_id |
1 |...| 2 | 3 | 4 | 5 |
我想让 4 个引用表的每个主键都自动递增,这样消息表的第一条记录应该如下所示:
id |...| messagesounds_id | messagevibrations_id | messagecolors_id | messageplatforms_id |
1 |...| 1 | 1 | 1 | 1 |
如何使用注释来实现这一点?