如何将嵌入列指定为id,其引用在JPA中包含id

时间:2014-12-04 12:59:34

标签: jpa

我想构建一个结构,其中embeddable既用作id又用作常规嵌入。它是许多其他表中的基类使用,在一些表中它既是id又是常规嵌入。我似乎无法在类之间获得正确的注释。在VCenterDatastore类中。

如果我在@EmbeddedId字段中指定VCenterManagedObjectReference id,我会

org.cru.cloud.management.core.repository.managedobjectReference.
VCenterManagedObjectReference 
must not have @Id properties when used as an @EmbeddedId: 
    org.cru.cloud.management.core.repository.datastore.VCenterDatastore.id

如果我在@Embedded课程中将其指定为VCenterDatastore,那么我

No identifier specified for entity: 
    org.cru.cloud.management.core.repository.datastore.VCenterDatastore".

ManagedObjectReference类是通过外部API提供的

@SuppressWarnings("all")
@XmlType
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ManagedObjectReference {
    public String val;
    public String type;

    public String getVal() {
        return val;
    }

    public void setVal(String val) {
        this.val = val;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String get_value() {
        return val;
    }

    public void set_value(String val) {
        this.val = val;
    }
}

MOR类是ManagedObjectReference的Serializable实现

@Embeddable
public class MOR implements Serializable
{
    @Column(nullable = false)
    private String val;
    public String getVal() {return this.val;}
    public void setVal(String val) {this.val = val;}

    @Column(nullable = false)
    private String type;
    public String getType() {return this.type;}
    public void setType(String type) {this.type = type;}

    public MOR(ManagedObjectReference managedObjectReference)
    {
        this.val = managedObjectReference.val;
        this.type = managedObjectReference.type;
    }
}

VCenterManagedObjectReference被设计为代表MOR的实体。这是因为我需要一种方法来检索MOR,如果它已经存在,那么我就不会产生重复的对象。因此getManagedObjectReference方法。此类在许多地方都用作id和常规“列”。

@Embeddable
public class VCenterManagedObjectReference implements Serializable
{
    @Transient
    private ManagedObjectReference managedObjectReference;

    @EmbeddedId
    private MOR id;
    public MOR getId() {return this.id;}
    public void setId(MOR id) {this.id = id;}

    public VCenterManagedObjectReference() {}

    public VCenterManagedObjectReference(VCenterManagedObjectReference managedObjectReference)
    {
        this.id = managedObjectReference.id;
    }

    private VCenterManagedObjectReference(ManagedObjectReference managedObjectReference)
    {
        this.managedObjectReference = managedObjectReference;
    }

    public void populate() { }

    public static VCenterManagedObjectReference getManagedObjectReference(ManagedObjectReference managedObjectReference)
    {
        MOR mor = new MOR(managedObjectReference);
        VCenterManagedObjectReferenceRepository repository = VCenterManagedObjectReferenceRepositoryManagedObjectReferenceDetailsService.vCenterDatacenterRepository;
        VCenterManagedObjectReference vCenterManagedObjectReference = repository.findById(mor);
        return (vCenterManagedObjectReference == null)?new VCenterManagedObjectReference(managedObjectReference):vCenterManagedObjectReference;
    }
}

VCenterDatastore类是一个类的示例,用于反映从Datastore对象检索的数据,该对象与ManagedObjectReference类一样,由外部API提供。

@Entity
@Embeddable
@Table(name="datastore")
public class VCenterDatastore
{
    @Transient
    private Datastore datastore;

    @Embedded
    private VCenterManagedObjectReference id;
    public VCenterManagedObjectReference getId() {return this.id;}
    public void setId(VCenterManagedObjectReference id) {this.id = id;}

    ...
}

0 个答案:

没有答案