如何在 Hibernate 实体类中创建下一种关系?用户通过创建消息(发送者、接收者、消息)向另一个用户发送消息。数据库看起来像这样:
+-----------------------------------+
| user |
+-----------------------------------+
| id | nickname | password |
+----+-------------+----------------+
| 1 | admin | pass |
+----+-------------+----------------+
+------------------------------------------------+
| message |
+-----------------------------------|------------+
| id | sender_id | receiver_id | message_t |
+----+-------------+----------------+------------+
| 1 | 10 | 12 | hi |
+----+-------------+----------------+------------+
实体类必须是什么样子?
@Entity
@Table(name="profiles")
public class Message {
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "sender")
private User sender;
@ManyToOne
@JoinColumn(name = "receiver")
private User receiver;
@Column(name = "message")
String message;
}
@Entity
@Table(name = "users")
public class User {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = )
List<Message> messages;
}
映射...什么?我需要找到所有消息,其中当前用户作为发送者 OR/AND 接收者存在。(“AND”,因为用户可以向自己发送消息)。