JPA持久化实体,其键值对映射

时间:2016-07-12 14:05:10

标签: java jpa persistence key-value persist

很抱歉,如果这是一个微不足道的问题,或者我误解了某些内容并且这个概念是错误的。

我有休息表:

External_Users_Details: +------------------+--------+------------+ | External_User_Id | Key | Value | +------------------+--------+------------+ | 1 | f_name | firstname | | 1 | l_name | lastneme | | 2 | f_name | firstname2 | | 2 | l_name | lastneme2 | +------------------+--------+------------+

Response_Details ╔════════════╦═════════════╦══════════════╦══════════════════╗ ║ Meeting_Id ║ Attendee_Id ║ Response_Seq ║ Response_Messege ║ ╠════════════╬═════════════╬══════════════╬══════════════════╣ ║ 123 ║ 1 ║ 1 ║ I will be there ║ ║ 123 ║ 1 ║ 2 ║ Changed my mind ║ ║ 123 ║ 2 ║ 1 ║ Yep. ║ ║ 123 ║ 123456 ║ 1 ║ I will be there. ║ ╚════════════╩═════════════╩══════════════╩══════════════════╝

现在,外部用户存储在具有键值对的表中,复杂的@Id {External_User_Id,[Key]}。

当用户邀请外部与会者参加会议时,他需要填写表格(输入名字,姓氏,公司名称,一些联系方式),同时他“确认”外部与会者的参与。

现在,我想保留实体Response_Details,但为了获取Attendee_Id,我需要以某种方式在External_Users_Details表中创建此用户。

Response_Details此外部与会者的实体映射如下所示:

@Entity
@IdClass(CResponsePK.class)
@Table(name = "Response_Details")
public class CalendarResponse implements Serializable {
  @Id
  @Column(name = "Meeting_Id")
  private Integer meetingId;
  @Id
  @Column(name = "Attendee_Id")
  private Integer attendeeId;
  @Id
  @Column(name = "Response_Seq")
  private Integer responseSeq;
  @Transient //this is transient because I was getting exception that ID has to be the same like in  CResponsePK
  private Map<String, String> externalAttendee;
  //...
}

@Entity @IdClass(CResponsePK.class) @Table(name = "Response_Details") public class CalendarResponse implements Serializable { @Id @Column(name = "Meeting_Id") private Integer meetingId; @Id @Column(name = "Attendee_Id") private Integer attendeeId; @Id @Column(name = "Response_Seq") private Integer responseSeq; @Transient //this is transient because I was getting exception that ID has to be the same like in CResponsePK private Map<String, String> externalAttendee; //... }

另外我有一个会话定制器:

ClassDescriptor cd = session.getClassDescriptor(CalendarResponse.class);
DirectMapMapping dmm = new DirectMapMapping();
dmm.dontUseIndirection();
dmm.setReferenceTableName("External_Users_Details");
dmm.setDirectKeyFieldName("External_Users_Details.[key]");
dmm.setDirectFieldName("External_Users_Details.[value]");
dmm.setAttributeName("externalAttendee");
dmm.addReferenceKeyFieldName("AttendeeID", "External_User_Id");

cd.addMapping(dmm);
主要问题是:

  • 当我想为外部用户添加响应时,如何创建External_Users_Details?
  • 我应该创建External_Users_Details实体吗? (如果是,如何在此实体中添加键值对映射)
  • 当我持久化CalendarResponse实体时,它应该自动创建吗?
  • 我是否搞砸了CalendarResponse和External_Users_Details的映射? (如果是这样,我该怎么做?)

0 个答案:

没有答案