Blocking Updating or Inserting an Entity when using Cascade in Hibernate

1.2k views Asked by At

I have an entity , EntHesaplasma , this entity has a relationship with EntCariHareketler entity. I have a foreign key in the EntHesaplasma entity for EntCariHareketler. I want to set a relationship between them , but I dont want to update,insert or remove operations on EntCariHareketler entity. Only I want to read operation on the second entity (EntCariHareketler). I tried Cascading types , but i could not get a success.

Below you can see partial of my entities

@Entity
@Table(name = "XOZ_HESAPLASMA")
public class EntHesaplasma {

    Integer hesaplasmaid;
    EntCariHareketler carihareket;
    String hesaplasmagrup;

    @Id
    @GeneratedValue
    @Column(name = "hesaplasmaid", unique = true, nullable = false)
    public Integer getHesaplasmaid() {
        return hesaplasmaid;
    }

    public void setHesaplasmaid(Integer hesaplasmaid) {
        this.hesaplasmaid = hesaplasmaid;
    }

    @OneToOne(fetch= FetchType.LAZY)
    @JoinColumn(name="carihareketid")
    public EntCariHareketler getCarihareket() {
        return carihareket;
    }

public void setCarihareket(EntCariHareketler carihareket) {
    this.carihareket = carihareket;
}

@Column(name="hesaplasmagrup")
public String getHesaplasmagrup() {
    return hesaplasmagrup;
}

public void setHesaplasmagrup(String hesaplasmagrup) {
    this.hesaplasmagrup = hesaplasmagrup;
}

And this is EntCarihareketler entity

@Entity
@Table(name = "CARI_HESAP_HAREKETLERI")
public class EntCariHareketler {

    private Integer cha_RECno;

    @Id
    @GeneratedValue
    @Column(name = "cha_recno", unique = true, nullable = false)
    public Integer getCha_RECno() {
        return cha_RECno;
    }

    public void setCha_RECno(Integer cha_RECno) {
        this.cha_RECno = cha_RECno;
    }

When I want to save the enthesaplasma object, i do not want to update or insert entCariHareketler entity, i got an exception like that the entcarihareketler entity could not insert. But i do not want to insert entcarihareketler entity. hibernate try to insert that entity, (btw in entcarihareket entity i do not use some fields which are part of primary keys. i only use cha_Recno as primary key. this is important field in the composite primary keys.)

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;

try {


         EntHesaplasma hesaplasma= new EntHesaplasma();
         hesaplasma.setHesaplasmagrup("a record");
         // related entcarihareketler entity
         EntCariHareketler entCariHareketler= new EntCariHareketler();
         // we set entcarihareketler id (1212) as primary key )
         entCariHareketler.setCha_RECno(1212);

tx=session.beginTransaction();
session.save(hesaplasma); 
tx.commit();

} catch (HibernateException e) {
if (tx != null)tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
1

There are 1 answers

2
Vlad Mihalcea On BEST ANSWER
  1. If you never want to modify the EntCariHareketler, you could simply annotate it with @Immutable.

  2. If you the entity is mutable but you want to disable updates from the other side, you need to set to false the insertable and updatable @JoinColumn attribute:

    @OneToOne(fetch= FetchType.LAZY)
    @JoinColumn(name="carihareketid", insertable = false, updatable = false)
    public EntCariHareketler getCarihareket() {
        return carihareket;
    }