2

Using JPA, I try to modify a persisted object for multilingual content, which means I need a composite key, with an id and a language. The id also needs to increment using a sequence (I'm using Oracle). How can it be done?

For the moment, I get a Contenu class:

@Entity
@Table(name = "CONTENUS")
public class Contenus implements java.io.Serializable {

    private ContenusId id;
    ...
}

and a ContenusId class:

@Embeddable
public class ContenusId implements java.io.Serializable {

    private Long id;
    private String langue;
    ...
}

But this doesn't handle the incremental id. Any idea?

4

1 に答える 1

2

まず、2 つのオブジェクトが等しいかどうかを確認する方法を知るために、JPA エンティティ マネージャーの ID クラスを作成する必要があります。この ID クラスはメソッドをオーバーライドする必要がequals()ありhashcode()ます。

/**
 * ContenusPK.java
 * 
 * $Source$
 */
import java.io.Serializable;

public class ContenusPK implements Serializable
{
    public static final long serialVersionUID = 1L;

    private Long id;
    private String langue;

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((langue == null) ? 0 : langue.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ContenusPK other = (ContenusPK) obj;
        if (id == null)
        {
            if (other.id != null)
                return false;
        }
        else if (!id.equals(other.id))
            return false;
        if (langue == null)
        {
            if (other.langue != null)
                return false;
        }
        else if (!langue.equals(other.langue))
            return false;
        return true;
    }

    /**
     * @return the serialversionuid
     */
    public static long getSerialversionuid()
    {
        return serialVersionUID;
    }

    /**
     * @return the id
     */
    public Long getId()
    {
        return id;
    }

    /**
     * @return the langue
     */
    public String getLangue()
    {
        return langue;
    }

    /**
     * @param id the id to set
     */
    protected void setId(Long id)
    {
        this.id = id;
    }

    /**
     * @param langue the langue to set
     */
    protected void setLangue(String langue)
    {
        this.langue = langue;
    }

}

次に、次のように、エンティティの複合主キー属性を宣言し、ID クラスをエンティティに関連付け、ID 生成戦略を定義する必要があります。

@IdClass(ContenusPK.class)
@Entity
@TableName(name="")
public class Contenus {

    @Id
    @GeneratedValue(
        strategy = GenerationType.SEQUENCE, 
        generator = "GENERATOR_NAME")
    @SequenceGenerator(
        name = "GENERATOR_NAME", 
        sequenceName = "SEQUENCE NAME IN DB")
    @Column(name = "CONTENUS_ID")
    private Long id;

    @Id
    @Column(name = "LANGUE")
    private String langue;

    ...
}
于 2012-10-25T10:02:57.673 に答える