0

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; } } 

1 Answer 1

2

Ralationships are created in hibernate like this:

@Entity @Table(name="CART") public class Cart { //... @OneToMany(mappedBy="cart") private Set<Item> items; // getters and setters } 

Please note that the @OneToMany annotation is used to define the property in Item class that will be used to map the mappedBy variable. That is why we have a property named “cart” in the Item class:

@Entity @Table(name="ITEMS") public class Item { //... @ManyToOne @JoinColumn(name="cart_id", nullable=false) private Cart cart; public Item() {} // getters and setters } 

Soin your case you just have to add

@Entity @Table(name = "address") public class Address { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private Long id; //... @OneToOne(mappedBy = "address") private User user; 

something lilke this to your Adress Table. Because one Adress also have one user.

For more information visit this site

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it added this to my source code. But i still got the issue that if i use the same adress for two users the adress gets added twice instead of just having it once in the database

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.