Простое сопоставление OneToOne, однонаправленное.
Объекты
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"})
@Data
public abstract class ID implements Serializable {
@Id
@GeneratedValue
@Column(name = "id")
private UUID id;
}
@RequiredArgsConstructor(onConstructor = @__(@PersistenceConstructor))
@AllArgsConstructor
@Builder
@Data
@Entity(name = "gencontractor")
@Table(name = "gencontractor")
public class GenContractor extends ID {
@Column(updatable = false)
private String email;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "address_id", referencedColumnName = "id")
private Address address;
}
@RequiredArgsConstructor(onConstructor = @__(@PersistenceConstructor))
@AllArgsConstructor
@Builder
@Data
@Entity(name = "address")
@Table(name = "address")
public class Address extends ID {
@Column
private String name;
@Column
private String street;
@Column
private String city;
@Column
private String phone;
}
Адрес таблицы имеет PK "id".Таблица GenContractor имеет PK "id" и внешний ключ "address_id" для Address.id.
Репозиторий
@RepositoryRestResource(collectionResourceRel = "gencontractors", path = "gencontractors")
public interface GenContractorRepository extends JpaRepository<GenContractor, UUID> {
}
@RepositoryRestResource(collectionResourceRel = "addresses", path = "addresses")
public interface AddressRepository extends JpaRepository<Address, UUID> {
}
Когда я POST / gencontractors {"email:" bla "," address ": {" name ":" bla ", ...}}, адресне сохранены в таблице адресов.
Что мне не хватает?