1

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"

Max Usanin
  • 2,479
  • 6
  • 40
  • 70

2 Answers2

1

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.

Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44
  • yes it works but still updating all StockEntity, it will be same like you will be use session.update(stock); – Max Usanin Nov 25 '15 at 12:08
  • I updated my post, post about how to update only one field .. error was like subproblem, i cannot close my post yet – Max Usanin Nov 25 '15 at 12:17
  • @MaxUsanin the update is much like a new question.. So please open a new question and close this one... we cannot be here as your RnD team and you need to acknowledge other person's effort.. and your update really leads to complete new question but still this http://www.mkyong.com/hibernate/hibernate-dynamic-insert-attribute-example/ may be helpful.. – Viraj Nalawade Nov 25 '15 at 12:19
1

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

Community
  • 1
  • 1
fabballe
  • 761
  • 4
  • 12