Nabil
Nabil

Reputation: 1811

Override JPA Mapping in leaf classes

I am using JPA 2 and my problem is about inheritence and overidde Mapping in JPA 2.

I have one Abstract Class [AbstractCompte] and two leafs classes [Compte , CompteTmp].

I want to redefine the Mapping for one Field nrCompte.

nrCompte must be unique in Compte Class . nrCompte is non unique in CompteTmp class .

I already test putting the @Column in the getter methods of COmpte and CompteTmp and it doesn't work and the result is that nrCompte is always not unique .

@MappedSuperclass
public abstract class AbstractCompte{
  @Id
  @GeneratedValue
  private Long id;

  private String nrCompte;
  ....
  ....
}

@Entity
public class CompteTmp extends AbstractCompte {

    @Column(length=16, unique = false)
    public String getNrCompte() {
         return super.getNrCompte();
    }
}

@Entity
public class Compte extends AbstractCompte {

    @Column(length=16, unique = true)
    public String getNrCompte() {
          return super.getNrCompte();
   }
}

Thanks in advance for your help .

Upvotes: 1

Views: 79

Answers (1)

joe776
joe776

Reputation: 1116

JPA offers AttributeOverride, so you can map it like this:

@Entity
@AttributeOverride(name="nrCompte", column=@Column(unique=false))
public class CompteTmp extends AbstractCompte { ... }

@Entity
@AttributeOverride(name="nrCompte", column=@Column(unique=true))
public class Compte extends AbstractCompte { ... }

Upvotes: 1

Related Questions