Max Usanin
Max Usanin

Reputation: 2499

Can I update only one relation (one field) using Hibernate

I have next relations:

@Entity
@Table(name = "STOCK", uniqueConstraints = @UniqueConstraint(columnNames = { "CODE", "INTERVAL", "DATE" }) )
public class StockEntity {

.....
many other fields like collections
.....

@ElementCollection
@CollectionTable(name = "PARAMETERS", joinColumns = @JoinColumn(name="SETTING_ID"))
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@MapKeyEnumerated(EnumType.STRING)
private Map<LocalSetting.SettingType, LocalSetting> parameters = new HashMap<>();

}


@Entity
@Table(name = "LOCAL_SETTING")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class LocalSetting {

    @Id
    @Column(name = "SETTING_ID", unique = true, nullable = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long settingId;
    .....
}

when I use my DAO:

@Override
public void updateStocks(List<StockEntity> stocks) {
    Session session = sessionFactory.getCurrentSession();

    stocks.forEach(stock -> {
        session.update(stock);
        session.flush();
        session.clear();
    });
}

in log trace i notice that will be updated all fields in StockEntity, when I was changing only "parameters", it mean i also was updating lot of other parameters which i could simply skip

when I try to update only "parameters" using next code:

@Override
public void updateParameters(List<StockEntity> stocks) {
    stocks.forEach(stock -> {
        for (Map.Entry<LocalSetting.SettingType, LocalSetting> entry : stock.getParameters().entrySet()) {
            sessionFactory.getCurrentSession().saveOrUpdate(entry.getValue());
        }
    });
}

but process will be same like i will update whole StockEntity ,maybe I can detach another fields until I will finish update "parameters"

Upvotes: 1

Views: 354

Answers (2)

fabballe
fabballe

Reputation: 771

You have to use dynamic-insert in order to update only one field. The syntax is:

@org.hibernate.annotations.Entity(
    dynamicInsert = true
)

You can see an example here: Dynamic insert example

You also can read this post why hibernate dynamic insert false by default in order to know problem you can have with dynamicInsert=true

Upvotes: 1

Viraj Nalawade
Viraj Nalawade

Reputation: 3227

As specified here

session.update()

update takes an entity as parameter.But you are passing a Map (which I assume are entities of LocalSetting that you want to save ).

And as far as I know it is not possible to persist a collection of objects using the update(..) method. Try the following code to check if it works for you:

@Override
public void updateParameters(Map<LocalSetting.SettingType, LocalSetting> parameters) 
{

    for (Map.Entry<String, String> entry : map.entrySet())
    {
        sessionFactory.getCurrentSession().update(entry.getValue());
    }
    sessionFactory.getCurrentSession().commit();
    sessionFactory.getCurrentSession().close();  
}

What makes me suggest this answer more is the exception in your question(org.hibernate.MappingException) which is basically telling us that we are updating the entity which isnt mapped.

Upvotes: 1

Related Questions