I want to create a Person with an Address, each of them are an entity. My Entities seem to work, the part where i begin to struggle is on how to create a Person using the constructor where i also have to put in the Address.
personRepository.save(new Person(new Name("Test","Test"),new Adress("Street","Number","PLZ","Town"),LocalDate.parse("2000-01-01"),"[email protected]","911"); This sadly does not work so my question is how can i create a Person object with the Address.
I'm also wondering how i would add the address if i already got the address in my Address repository, is there a way to get the address or use the adress ID?
adresseRepository.save(new Adresse("Street","Number","PLZ","Town")); Here's the code for both of the shortend.
Person:
@Entity @Table(name = "Person") public class Person { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "PersonID") private Long personID; @Column(name = "FullName") @Convert(converter = NameConverter.class) private Name fullName; @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name="AdresseID") private Adresse adresse; @Column(name = "Geburtsdatum") private LocalDate geburtsdatum; @Column(name = "EMail") private String email; @Column(name = "Telefonnummer") private String telefonnummer; private Person() {} public Person(Name fullName, Adresse adresse, LocalDate geburtsdatum, String email, String telefonnummer) { this.fullName = fullName; this.adresse = adresse; this.geburtsdatum = geburtsdatum; this.email = email; this.telefonnummer = telefonnummer; } } Address:
@Entity @Table(name = "Adresse") public class Adresse { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "AdresseID") private Long adresseID; @Column(name = "Strasse") private String strasse; @Column(name = "Hausnummer") private String hausnummer; @Column(name = "PLZ") private String plz; @Column(name = "Ort") private String ort; protected Adresse() {} public Adresse(String strasse, String hausnummer, String plz, String ort) { this.strasse = strasse; this.hausnummer = hausnummer; this.plz = plz; this.ort = ort; } }