'Hibernate update table and connected second table with foreign key

Can I update 2 tables with save changes only for one with Hibernate? My tables are linked like this:

@Entity
@Table(name = "tablea")
public class TableA extends AbstractBean {

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "tableBId", foreignKey = @ForeignKey(name = "fk_tableBId"))
    private TableB tableB;
}

@Entity
@Table(name = "tableb")
public class TableB extends AbstractBean {

    @OneToMany(mappedBy = "tableB")
    private Set<TableA> tablesA = new HashSet<>();

    public void setTablesA (Set<TableA> tablesA) {
        this.tablesA = tablesA ;
    }
}

Now I take object from database TableB and over write some values in this table but also overwrite tablesA value:

TableB tableB = ...;
tableB.setTablesA(...);
repository.save(tableB);

all columns from TableB are update, but it don't update tableA

Am I wrong with defining the attributes that the second table is not updating?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source