Reputation: 920
I have a problem with setting up some hibernate annotations in my entity classes.
My question is: How can I tell Hibernate, that it should store the Address, Name and Customer information in just one table. This table should have the following columns: ID, Given, Surname, Street, HouseNumber, Zip, City, Phone, Comment. At the moment Hibernate generates for each entity a single table in the mysql database. Therefore it is necessary to define an @Id in each entity class (Customer, Name, Address). But I want to keep all the information in one table with just one @Id for the customer.
How can I solve this problem?
Below you can find an extract of the Customer, the Name and the Address entity classes:
@Entity
@Table(name = "customer")
@XmlRootElement
public class Customer {
@Id
@GeneratedValue
private int id;
@OneToOne
private Name name;
@OneToOne
private Address address;
private String phone;
private String comment;
public Customer() { }
}
@Entity
@XmlRootElement
public class Name {
private String given;
private String surname;
public Name() { }
}
@Entity
@XmlRootElement
public class Address {
private String street;
private String houseNumber;
private String zip;
private String city;
public Address() { }
}
Upvotes: 0
Views: 270
Reputation: 691635
Only Customer
should be annotated with @Entity
. The other classes are not entities, but only a small part of the Customer entity. They should thus be annotated with @Embeddable.
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue
private int id;
@Embedded
private Name name;
@Embedded
private Address address;
private String phone;
private String comment;
public Customer() { }
}
@Entity
public class Name {
private String given;
private String surname;
public Name() { }
}
@Embeddable
public class Address {
private String street;
private String houseNumber;
private String zip;
private String city;
public Address() { };
}
Upvotes: 3