使用@Embedded在数据库中使用Hibernate null

时间:2016-03-22 09:12:50

标签: java hibernate

你好我正在学习Hibernate,目前我正在尝试使用@Embedded注释,但是我的数据库中的@Embeddable对象得到了空值。

代码如下:

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
    var push = PushNotification.init({
        "android": {
            "senderID": "540732047871"
        },
        "ios": {"alert": "true", "badge": "true", "sound": "true"}, 
        "windows": {} 
    });

    push.on('registration', function(data) {
        console.log("registration event");
        document.getElementById("regId").innerHTML = data.registrationId;
        console.log(JSON.stringify(data));
    });

    push.on('notification', function(data) {
        console.log("notification event");
        console.log(JSON.stringify(data));
        var cards = document.getElementById("cards");
        var card = '<div class="row">' +
              '<div class="col s12 m6">' +
              '  <div class="card darken-1">' +
              '    <div class="card-content black-text">' +
              '      <span class="card-title black-text">' + data.title + '</span>' +
              '      <p>' + data.message + '</p>' +
              '    </div>' +
              '  </div>' +
              ' </div>' +
              '</div>';
        cards.innerHTML += card;

        push.finish(function () {
            console.log('finish successfully called');
        });
    });

    push.on('error', function(e) {
        console.log("push error");
    });
}};

这是我的Employee类,其中包含来自Adress.class的@embedded字段 Adress课程来了:

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

那是我的主要课程:

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


}

因此,与Employee类相关的字段没有问题地转到数据库,但是与类Adress相关的字段被设置为null。我想把它们作为setValues。在此先感谢所有人。

2 个答案:

答案 0 :(得分:1)

您缺少setAddress调用

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

答案 1 :(得分:0)

您需要在保留员工之前在员工中设置您创建的Adress对象,就像您当前的实施中一样,您的员工不知道他的地址。

employee.setAdress(adress);