118

同じデータエントリのバージョンが必要です。つまり、エントリを別のバージョン番号で複製したいと思います。

id - Version主キーになります。

エンティティはどのように見えるべきですか?別のバージョンと複製するにはどうすればよいですか?

id Version ColumnA

1   0      Some data
1   1      Some Other data
2   0      Data 2. Entry
2   1      Data
4

4 に答える 4

255

2つのキーを含むを作成しEmbedded class、のようにそのクラスへの参照を作成できEmbeddedIdますEntity

@EmbeddedId@Embeddable注釈が必要になります。

@Entity
public class YourEntity {
    @EmbeddedId
    private MyKey myKey;

    @Column(name = "ColumnA")
    private String columnA;

    /** Your getters and setters **/
}
@Embeddable
public class MyKey implements Serializable {

    @Column(name = "Id", nullable = false)
    private int id;

    @Column(name = "Version", nullable = false)
    private int version;

    /** getters and setters **/
}

このタスクを実行する別の方法は、@IdClassアノテーションを使用して、両方idをその中に配置することIdClassです。@Idこれで、両方の属性に通常の注釈を使用できます

@Entity
@IdClass(MyKey.class)
public class YourEntity {
   @Id
   private int id;
   @Id
   private int version;

}

public class MyKey implements Serializable {
   private int id;
   private int version;
}
于 2012-10-23T14:42:10.127 に答える
11

Serializable使用している場合は、MyKeyクラスを実装する必要があります@IdClass

于 2015-09-24T06:45:50.407 に答える
5

キークラス:

@Embeddable
@Access (AccessType.FIELD)
public class EntryKey implements Serializable {

    public EntryKey() {
    }

    public EntryKey(final Long id, final Long version) {
        this.id = id;
        this.version = version;
    }

    public Long getId() {
        return this.id;
    }

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

    public Long getVersion() {
        return this.version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    public boolean equals(Object other) {
        if (this == other)
            return true;
        if (!(other instanceof EntryKey))
            return false;
        EntryKey castOther = (EntryKey) other;
        return id.equals(castOther.id) && version.equals(castOther.version);
    }

    public int hashCode() {
        final int prime = 31;
        int hash = 17;
        hash = hash * prime + this.id.hashCode();
        hash = hash * prime + this.version.hashCode();
        return hash;
    }

    @Column (name = "ID")
    private Long id;
    @Column (name = "VERSION")
    private Long operatorId;
}

エンティティクラス:

@Entity
@Table (name = "YOUR_TABLE_NAME")
public class Entry implements Serializable {

    @EmbeddedId
    public EntryKey getKey() {
        return this.key;
    }

    public void setKey(EntryKey id) {
        this.id = id;
    }

    ...

    private EntryKey key;
    ...
}

別のバージョンと複製するにはどうすればよいですか?

プロバイダーから取得したエンティティを切り離し、エントリのキーを変更して、新しいエンティティとして永続化することができます。

于 2012-10-23T14:52:14.833 に答える
2

MyKeyクラス(@Embeddable)には、@ManyToOneのような関係があってはなりません。

于 2017-07-11T01:15:22.457 に答える