razor2013 wrote: » Hello, If I have an entity (e.g. Employee) with an embedded object (e.g. Address) do I have to make extra columns in the Employee table for the fields in Address when I am creating a MySQL database? Thanks.
Yes
package com.jamesanthony527.top.domain; import static javax.persistence.GenerationType.IDENTITY; import javax.persistence.*; import com.jamesanthony527.top.domain.Address; @NamedQueries({ @NamedQuery(name="Employee.findById", query="select distinct c from Employee c where c.id = :id") }) @Entity @Table(name = "employee") public class Employee { private Long id; private String firstName; private String lastName; public Employee(){} @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "ID") public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @Column(name = "firstname") public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } @Column(name = "surname") public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } @Embedded private Address address = new Address(); public Address getAddress() { return this.address; } public void setAddress(Address address) { this.address = address; } public String toString() { return "Employee - Id: " + id + ", First name: " + firstName + ", Last name: " + lastName + ", street and city:" ; } }
package com.jamesanthony527.top.domain; import java.io.Serializable; import javax.persistence.Embeddable; import com.jamesanthony527.top.domain.Address; @Embeddable public class Address implements Serializable{ private static final long serialVersionUID = 1L; private String street; private String city; public Address() { super(); } public Address(Address source) { super(); this.street = source.street; this.city = source.city; } public String getStreet() { return this.street; } public void setStreet(String street) { this.street = street; } public String getCity() { return this.city; } public void setCity(String city) { this.city = city; } }
package com.jamesanthony527.top; import java.util.List; import org.springframework.context.support.GenericXmlApplicationContext; import com.jamesanthony527.top.dao.*; import com.jamesanthony527.top.domain.*; public class EmloyeeTester { public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load("META-INF/spring/app-context.xml"); ctx.refresh(); EmployeeDao empDao = ctx.getBean("employeeDao", EmployeeDao.class); Employee ee = new Employee(); ee.setFirstName("James"); ee.setLastName("Moles"); ee.getAddress().setCity("Dawson"); ee.getAddress().setStreet("hull"); empDao.save(ee); List<Employee> emp = empDao.findAll(); listContacts(emp); } private static void listContacts(List<Employee> e) { System.out.println(""); System.out.println("Listing employees without details:"); for (Employee ee: e) { System.out.println(ee); System.out.println(); } } }