Marcos
Marcos

Reputation: 25

JPA ignore mapping row by column value

I am making an application based on JPA/Eclipselink and entities, and delete operations must be logical (attribute / column true or false) to keep a log in the database. I wanted to know if it is possible ignore rows of the database whose "deleted" is "true" in relations OneToMany or ManyToMany. I have entities similar to the following:

@Entity
public class Employee {
  @Id
  @Column(name="EMP_ID")
  private long id;
  ...
  @OneToMany(mappedBy="owner")
  private List<Phone> phones;
  ...
}

@Entity
public class Phone {
  @Id
  private long id;

  @NotNull
  @Column(name = "deleted")
  private boolean deleted = false;
  ...
  @ManyToOne(fetch=FetchType.LAZY)
  @JoinColumn(name="OWNER_ID")
  private Employee owner;
  ...
}

Thanks in advance!

Upvotes: 0

Views: 523

Answers (1)

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23562

Yes, you can use the @AdditionalCriteria for this purpose:

@AdditionalCriteria("this.deleted = false")

Upvotes: 1

Related Questions