JPA orphanRemoval不能与嵌入式类一对一关联

时间:2016-10-06 14:59:07

标签: java hibernate jpa

我使用JPA 2.0/Hibernate 4.3.11实现了从可嵌入类到另一个实体的一对一关联。我发现orphanRemoval无效。下面是我的实体:(与http://docs.oracle.com/javaee/7/api/javax/persistence/OneToOne.html的示例3完全相同)

Employee.java

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Employee {

    @Id
    @GeneratedValue
    int id;

    @Embedded
    LocationDetails location;

    public int getId() {
        return id;
    }

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

    public LocationDetails getLocation() {
        return location;
    }

    public void setLocation(LocationDetails location) {
        this.location = location;
    }

}

LocationDetails.java

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.OneToOne;

@Embeddable
public class LocationDetails {

    int officeNumber;

    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    ParkingSpot parkingSpot;

    public int getOfficeNumber() {
        return officeNumber;
    }

    public void setOfficeNumber(int officeNumber) {
        this.officeNumber = officeNumber;
    }

    public ParkingSpot getParkingSpot() {
        return parkingSpot;
    }

    public void setParkingSpot(ParkingSpot parkingSpot) {
        this.parkingSpot = parkingSpot;
    }

}

ParkingSpot.java

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class ParkingSpot {

    @Id
    @GeneratedValue
    int id;

    String garage;

    @OneToOne(mappedBy = "location.parkingSpot")
    Employee assignedTo;

    public int getId() {
        return id;
    }

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

    public String getGarage() {
        return garage;
    }

    public void setGarage(String garage) {
        this.garage = garage;
    }

    public Employee getAssignedTo() {
        return assignedTo;
    }

    public void setAssignedTo(Employee assignedTo) {
        this.assignedTo = assignedTo;
    }

}

当我将parkingSpot设置为null或其他新实例时。 parkingSpot的原始记录未被删除。

0 个答案:

没有答案