Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

@Embedded annotation, MySQL database

Options
  • 29-04-2013 10:24pm
    #1
    Registered Users Posts: 40


    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.


Comments

  • Moderators, Sports Moderators, Regional Abroad Moderators Posts: 2,646 Mod ✭✭✭✭TrueDub


    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 - @Embedded simply means that you generate another object from specific columns in the same table as the parent.


  • Registered Users Posts: 2,781 ✭✭✭amen


    Yes

    Or No.

    Say the Employee Entity has multiple embedded Address entities with one for home address, one for work address etc

    then having additional columns in the Employee table won't work.

    If I had an entity with an embedded entity I would create a new table for the embedded entity. This gives greater flexibility.


  • Moderators, Sports Moderators, Regional Abroad Moderators Posts: 2,646 Mod ✭✭✭✭TrueDub


    That's probably a better design for an Address class, but it's a different type of relationship.

    @embedded - Defines a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the entity

    See http://www.objectdb.com/api/java/jpa/Embeddable


  • Registered Users Posts: 2,781 ✭✭✭amen


    Well I learnt something new today.


  • Registered Users Posts: 40 razor2013


    Apologies for stoking up this thread again, but I am just curious about the following code (The code works fine. I am using Spring and Hibernate). I have attached a screenshot of MySQL Workbench showing my Employee table with columns.

    My question, is there any advantage to having an embedded object like Address when I have to create extra columns in the database table for the fields in the Address class. I tried persisting the Employee without the street and city columns in the employee table and I received an error. Is there no way to persist an Employee without having to create these extra columns?
    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();
            }
        }
        
    }
    


  • Advertisement
  • Moderators, Sports Moderators, Regional Abroad Moderators Posts: 2,646 Mod ✭✭✭✭TrueDub


    I'm not sure what you mean - if you declare in a class that a column exists (by declaring the variable), you need to ensure the column exists in the relevant table.

    You can set the column to not require a value, but it must at least be present.

    By the way, did you know that you can set Hibernate to auto-generate your tables based on your classes? It wipes any data, but if you're making a lot of change to your classes it can be useful. Never never never let this setting go to production though!!


Advertisement