0

各クラスがエンティティを拡張するモデルがあります:

@MappedSuperclass
public abstract class Entity implements Serializable {
    private Long id;
    .
    .
    .
}

さて、私のモデルでは、各エンティティは属性を持つことができます:

@javax.persistence.Entity
public class Attribute extends Entity {
    private Entity entity;
    private String name;
    private String value;
    .
    .
    .
}

私の質問は次のとおりです。クラス Attribute のエンティティ属性をマップするにはどうすればよいですか? 私の考えは、この属性をentityClassとentityIdとして保存することですが、それでもJPAでこれを達成する方法がわかりませんか?


エンティティを拡張する具象クラスの例:

@javax.persistence.Entity
public class Ball extends Entity {
    private Set<Attribute> attributes;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "entity")
    public Set<Attribute> getAttributes() {
        return attributes;
    }

    public void setAttributes(Set<Attribute> attributes) {
        this.attributes = attributes;
    }
}
4

2 に答える 2

1

JPAでこれを行う簡単な方法はありません。関係を@Transientとしてマッピングし、IDとクラスに2つの@Basicマッピングを追加できます(おそらくプロパティのget / setメソッドを使用します)。次に、モデルコードでクエリを実行します。

EclipseLinkを使用している場合は、@VariableOneToOneを使用できる可能性があります。

見る、

http://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_variableonetoone.htm#CHDDFDGF

于 2013-01-28T15:26:02.523 に答える
0

コメントによると、マップされたスーパークラスに双方向の関係が必要です。残念ながら、JPA ではそれを行うことはできません。その理由は、 aMappedSuperClassがエンティティではなく、 のフィールドがMappedSuperClass異なるサブクラスに属しているためです。

ソースJPA 2.0 仕様(2.11.2)

(あなたが何をしようとしているのか正確にはわかりませんが:) 永続化モデルのメタモデルを永続化しようとしているようです。読む必要がある場合:使用できますEntityManagerFactory.getMetamodel()

于 2013-01-27T08:49:49.397 に答える