spring jpa inheritance, OneToMany

0

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:

  1. czy relecje do adresów są ok? chodzi o to aby client mial wiele adresów
  2. 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";
  }
0

jak ma być @ManyToOne w , to brakuje @OneToMany w klasie Person

https://en.wikibooks.org/wiki/Java_Persistence/OneToMany

najlepiej zwrócić listę adresów, cóś takiego:

@OneToMany
List<Address> addresses
0
Duży Terrorysta napisał(a):

jak ma być @ManyToOne w , to brakuje @OneToMany w klasie Person

https://en.wikibooks.org/wiki/Java_Persistence/OneToMany

najlepiej zwrócić listę adresów, cóś takiego:

@OneToMany
List<Address> addresses

a nie wróć, ślepy jestem, post do kosza

1 użytkowników online, w tym zalogowanych: 0, gości: 1