0

To finish up my API I need to update an element of my Database using Hibernate-Criteria.

So in my database there is any entity called Task. I get this Taks using Hibernate .createQuery and I make some modifications. The Task objekt will have an id and also some other values.

I want to update the respective object in my database but how can I pass the index

public void megreTask(Task entity, String id) {
    getSession().merge(entity); // how to pass id ?
}

As you can see from the example above I seem only to be able to find the .merge() method, which only allows me to pass in an object but not the id.

How can update my entity in the database. I've already done a fair bit of SpringBoot and I just want something simmilar to the put method?

Miger
  • 1,175
  • 1
  • 12
  • 33
  • No need to pass the ID. your `Task` should already have an ID assigned as you fetched it from the database. Also if you only want to simply update the `Task` then `getSession().update(entity);` would be appropriate. for more info read [this](https://stackoverflow.com/questions/161224/what-are-the-differences-between-the-different-saving-methods-in-hibernate) – XtremeBaumer Feb 22 '19 at 08:40
  • Thanks, as I asked below, how can I check wether the object is managed by Hibernate? – Miger Feb 22 '19 at 09:02

3 Answers3

0

If your entity is transient object (is not managed by hibernate) then you should do update like this

 public void megreTask(Task entity, String id) {
    Task toBeUpdated = getSession().get(Task.class, id); 
    toBeUpdated.setName(entity.getName()); //set properties which should be updated
    getSession().update(toBeUpdated); 
}
Tijkijiki
  • 792
  • 7
  • 19
  • How can I check wether it is transient? The backend wasn't built by me? – Miger Feb 22 '19 at 09:02
  • 1
    `transient` means that its a newly created instance. An instance you didn't fetch from the database or anything alike. If you arent sure whether ID already exists, just do like shown in the answer. – XtremeBaumer Feb 22 '19 at 09:11
0

You can call saveOrUpdate() method.

public void megreTask(Task entity, String id) {
    entity.setId(id);
    getSession().saveOrUpdate(entity); 
}

In another way you need to load data from database update the data in db entity

 public void megreTask(Task entity, String id) {
    Task dbTask = getSession().get(Task.class, id);
    dbTask .setValue1(entity.getValue1());
    dbTask .setValue2(entity.getValue2());
    getSession().update(dbTask ); 
}
eHowToNow
  • 24
  • 5
0

This method is using CriteriaBuilder.

    CriteriaBuilder cb = this.getSession().getCriteriaBuilder();
    CriteriaUpdate<Entity> update = cb.createCriteriaUpdate(Entity.class);

    // set the root class
    Root e = update.from(Entity.class);

    // set update and where clause
    update.set("property", newValue);
    update.where(cb.equalTo(e.get("Entity-Id"), valueOfEntityId));

    // perform update
    this.getSession().createQuery(update).executeUpdate();

source : Hibernate Criteria

if you want to Update All properties of that Entity, you can use

//load entity by its id
Entity entity = (Entity)this.getSession().load(Entity , entityId);

if(entity != null) {
    // accessing setter
    entity.setParameter(updatedValue);
    this.getSession().update(entity);
}
Kiran S
  • 78
  • 1
  • 10