ostry
ostry

Reputation: 117

How to disable delete for JoinTable?

I have problem with disable delete for JoinTable.

@Entity 
class Employee
{
     @Id
     Long id;

     @ManyToOne( cascade = { CascadeType.REFRESH } )
     @JoinTable( name = "Employee2AddressOracleView",
                 joinColumns = @JoinColumn( name = "employee_id" ), 
                 inverseJoinColumns = @JoinColumn( name = "address_id" ) 
     private Address address;
)

Address for Employee is calculated in View. It works, I can load Employee but when I want delete employee JPA want delete row from view to. It is possible to disable this delete query?

JPA query in console:

delete from Employee where employee_id = ?
delete from Employee2AddressOracleView where employee_id = ?

Upvotes: 2

Views: 1550

Answers (2)

alan.sambol
alan.sambol

Reputation: 265

The accepted answer has a link to hibernate forums which are dead. I managed to pull the link out on archive.org.

The solution is to create a separate entity representing the join table, mapped to the view, instead of using @JoinTable.

Main entity mappings:

@Entity
@Table(name="Main")
   public class MainEntity {
   @Id
   @Column(name="id")
   private Integer id;

   @OneToOne
   @PrimaryKeyJoinColumn
   private JoinTableViewEntity joinEntity;
}

Join table view entity mappings:

@Entity
@Table(name="TableView")
public class JoinTableViewEntity {
   @Id
   @Column(name="id")
   private Integer mainEntityId;

   @ManyToOne
   @JoinColumn(name="other_id", updatable=false, insertable=false)
   private OtherEntity other;
}

It also works without updateable and insertable attributes.

Upvotes: 1

James
James

Reputation: 18389

If you are using EclipseLink you can use a DescriptorCustomizer to make the mapping readOnly.

Upvotes: 0

Related Questions