2

hello im learning Hibernate, currently I'm trying to use @Embedded annotation, but i got nulls from @Embeddable object in my DB.

the code is as follows:

@Entity
public class Employee {

@Id
@GeneratedValue
private int id;
private String firstName;
private String lastName;
private double salary;

@Embedded
private Adress adress;

public Adress getAdress() {
    return adress;
}


public void setAdress(Adress adress) {
    this.adress = adress;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public double getSalary() {
    return salary;
}

public void setSalary(double salary) {
    this.salary = salary;
}

So that is my Employee class which hold, @embedded field from Adress.class Here comes Adress class:

@Embeddable
public class Adress {
private String locality;
private String streetNumber;
private String zipCode;


public String getLocality() {
    return locality;
}
public void setLocality(String locality) {
    this.locality = locality;
}
public String getStreetNumber() {
    return streetNumber;
}
public void setStreetNumber(String streetNumber) {
    this.streetNumber = streetNumber;
}
public String getZipCode() {
    return zipCode;
}
public void setZipCode(String zipCode) {
    this.zipCode = zipCode;
}


}

and thats my main class:

public class Main {

public static void main(String[] args) {

    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("myDatabase");
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Employee employee = new Employee();
    Adress adress = new Adress();
    adress.setLocality("New York");
    adress.setZipCode("55-5555");
    adress.setStreetNumber("55");

    employee.setFirstName("Andy");
    employee.setLastName("Cole");
    employee.setSalary(3333);

    entityManager.getTransaction().begin();
    entityManager.persist(employee);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();

}

}

So fields related to class Employee go to the database without problem, but fields related to class Adress are set to null. I'd like to have them as setValues. Thanks in advance to all.

2 Answers 2

1

You're missing setAddress call

public class Test {

    public static void main(String[] args) {

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("myDatabase");
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        Employee employee = new Employee();
        Adress adress = new Adress();
        adress.setLocality("New York");
        adress.setZipCode("55-5555");
        adress.setStreetNumber("55");

        employee.setFirstName("Andy");
        employee.setLastName("Cole");
        employee.setSalary(3333);

        employee.setAdress(adress);

        entityManager.getTransaction().begin();
        entityManager.persist(employee);
        entityManager.getTransaction().commit();
        entityManager.close();
        entityManagerFactory.close();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks!, i was kind of thinking like that, but thought that annotation handles it all.
Did you give it a try?
Then, you may give answer an "Accept" ;)
i did long time ago , but theres notification about me having not enough rep points
0

You need to set your created Adress object in your employee before persisting the employee, as in your current implementation your employee is not aware of his adress.

employee.setAdress(adress);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.