Hej,
napisałem takie encje
person
@Entity
@Data
@Inheritance( strategy = InheritanceType.SINGLE_TABLE )
@DiscriminatorColumn( name = "personType" )
public abstract class Person {
@Id
@GeneratedValue( strategy = IDENTITY)
private Long id;
@NotNull
private String acronym;
@NotNull
private String firstName;
private String middleName;
@NotNull
private String lastName;
}
klient
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@DiscriminatorValue( value = "CLIENT" )
public class ClientCard extends Person{
@NotNull
@Column(columnDefinition="TEXT")
private String description;
@NotNull
@OneToMany(mappedBy="person", cascade=CascadeType.ALL)
private Collection<PostalAddress> address = new ArrayList<PostalAddress>();
}
adres
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PostalAddress {
@Id
@GeneratedValue( strategy = IDENTITY )
private Long id;
@ManyToOne
private Person person;
@NotNull
@Column(length = 100, nullable = false)
private String addressLocality;
@NotNull
@Column(length = 6, nullable = false)
private String postalCode;
@NotNull
@Column(length = 100, nullable = false)
private String streetAddress;
}
gdy tworzę kartę klienta, wszystko jest OK, dziedziczenie ładnie działa
i mam dwa pytania / problemy:
- czy relecje do adresów są ok? chodzi o to aby client mial wiele adresów
- nie potrafię zapisać adresu do bazy
@RequestMapping(value = "/create", method = POST )
public String saveClientCard(@ModelAttribute("clientForm") ClientCard clientCard,
BindingResult bindingResult, Model model) {
clientRepository.save(clientCard); // tutaj zapisuję tylko kartotekę
return "redirect:/client/list";
}