一对一多列外键的断列映射

时间:2017-02-10 05:43:24

标签: java sql spring jpa

我有两个实体AAAAAAAAAAAAAAAAAAAAAAA- BBBBBBBBB .dont-break-out { /* These are technically the same, but use both */ overflow-wrap: break-word; word-wrap: break-word; -ms-word-break: break-all; /* This is the dangerous one in WebKit, as it breaks things wherever */ word-break: break-all; /* Instead use this non-standard one: */ word-break: break-word; /* Adds a hyphen where the word breaks, if supported (No Blink) */ -ms-hyphens: auto; -moz-hyphens: auto; -webkit-hyphens: auto; hyphens: auto; } ,其中RoomNight在两列(BookingCorporateDetailsBookingCorporateDetails)上引用RoomNight。

BookingCorporateDetails是可选的,但RoomNight是强制性的。我正在尝试在BookingCorporateDetails表上创建一个外键加入 -

AbstractJpaEntity.java

booking_id

AbstractBookingEntity.java

room_night_booking_id

BookingCorporateDetailsEntity.java

@MappedSuperclass
@SuppressWarnings("serial")
public abstract class AbstractJpaEntity implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  protected Long id;

  @Column(name = "created_at")
  @Temporal(TemporalType.TIMESTAMP)
  protected Date createdAt;

  @Column(name = "created_by", length = 32, nullable = true)
  protected String createdBy;

  @Column(name = "updated_at")
  @Temporal(TemporalType.TIMESTAMP)
  protected Date updatedAt;

  @Column(name = "updated_by", length = 32, nullable = true)
  protected String updatedBy;

  @Column(name = "is_published", columnDefinition = "tinyint(1) default 1")
  protected boolean isPublished;

  @Column(name = "is_deleted", columnDefinition = "tinyint(1) default 0")
  protected boolean isDeleted;

  // Getters and Setters
}

RoomNightEntity.java

@MappedSuperclass
public abstract class AbstractBookingEntity extends AbstractJpaEntity {

  private static final long serialVersionUID = 6980195228404112444L;

  @Column(name = "booking_id", nullable = false)
  protected String bookingId;

  @Column(name = "room_night_booking_id", nullable = false)
  protected String roomNightBookingId;

  public String getBookingId() {
    return bookingId;
  }

  public void setBookingId(String bookingId) {
    this.bookingId = bookingId;
  }

  public String getRoomNightBookingId() {
    return roomNightBookingId;
  }

  public void setRoomNightBookingId(String roomNightBookingId) {
    this.roomNightBookingId = roomNightBookingId;
  }
}

当我执行代码时,我得到以下异常 -

@Entity(name = "booking_corporate_details")
@Table(name = "booking_corporate_details", uniqueConstraints = { @UniqueConstraint(name = "key_bcd_order_sub_order", columnNames = { "booking_id", "room_night_booking_id" }) })
public class BookingCorporateDetailsEntity extends AbstractBookingEntity {

  private static final long serialVersionUID = -5577224607005257364L;

  @Column(name = "btc_company")
  private String billToCompany;

  @Column(name = "cr_company_name", nullable = false)
  private String companyName;

  @Column(name = "cr_manager_name")
  private String managerName;

  @Column(name = "cr_manager_email")
  private String managerEmail;

  @Column(name = "cr_manager_phone")
  private String managerPhone;

  @OneToOne(optional = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  @JoinColumns({ 
    @JoinColumn(name = "booking_id", referencedColumnName = "booking_id", insertable = false, updatable = false),
    @JoinColumn(name = "room_night_booking_id", referencedColumnName = "room_night_booking_id", insertable = false, updatable = false) 
  })
  private RoomNightEntity roomNightEntity;

  public RoomNightEntity getRoomNightEntity() {
    return roomNightEntity;
  }

  public void setRoomNightEntity(RoomNightEntity roomNightEntity) {
    this.roomNightEntity = roomNightEntity;
  }

  // Getters And Setters
}

PS:我已经在这里看到了多个示例,但即使在尝试这些解决方案后也无法解决问题。

1 个答案:

答案 0 :(得分:0)

我已经完成了一些测试,并且它与两个连接列一起使用的唯一方法是从mappedBy实体中移除RoomNightEntity

您最终会得到一种没有父子关系的双向映射: 的 BookingCorporateDetailsEntity

@OneToOne(optional = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumns({
          @JoinColumn(name = "booking_id", referencedColumnName = "booking_id", insertable = false, updatable = false),
            @JoinColumn(name = "room_night_booking_id", referencedColumnName = "room_night_booking_id", insertable = false, updatable = false)
    })
private RoomNightEntity roomNightEntity;

<强> RoomNightEntity

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumns({
            @JoinColumn(name = "booking_id", referencedColumnName = "booking_id", insertable = false, updatable = false),
            @JoinColumn(name = "room_night_booking_id", referencedColumnName = "room_night_booking_id", insertable = false, updatable = false)
    })
private BookingCorporateDetailsEntity bookingCorporateDetailsEntity;