简体   繁体   English

spring - CrudRepository和具有瞬态字段的bean

[英]spring - CrudRepository and beans with transient fields

I have a Spring CrudRepository that I am using to persist an Entity. 我有一个Spring CrudRepository,我用它来持久化实体。 However, my Entity has some transient fields that I do not want stored in the database, however, I don't want to lose them when I call save() but it appears I always get a brand new, different object back from the save() method. 但是,我的实体有一些我不希望存储在数据库中的瞬态字段,但是,当我调用save()时我不想丢失它们但看起来我总是从保存中获得一个全新的,不同的对象() 方法。 And thus I lose my transient values. 因此我失去了我的瞬态价值。

If I don't use the object that is returned and just keep using my original object that I called save() on, I can only call save() on it once, otherwise it complains the Entity was updated by another session. 如果我不使用返回的对象并继续使用我调用save()的原始对象,我只能在其上调用save()一次,否则它会抱怨实体已被另一个会话更新。

Any idea how to either get save() to return me my same object back? 知道如何让save()返回我的同一个对象吗? Should I be using a different repository? 我应该使用不同的存储库吗? Can I add some sort of annotations to change the behavior? 我可以添加某种注释来改变行为吗?

@Override
@Transactional
public TestRun saveTestRun(TestRun run) 
{
    logger.debug("Saving TestRun with Id: " + run.getId() + " " + run.toString());
    logger.debug("Timeout 1: " + run.getTimeout());
    run.setTimeout(10000000);  // set transient field

    logger.debug("Timeout 2: " + run.getTimeout());
    TestRun r2 = this.repository.save(run);

    logger.debug("Timeout 3: " + r2.getTimeout());

    logger.debug("Saved TestRun with Id: " + r2.getId() + " " + r2.toString() );

    return r2;
}

And this produces: 这会产生:

Saving TestRun with Id: 2 TestRun@4c4d810e
Timeout 1: 90
Timeout 2: 10000000
Payload no-arg constructor
TestRun constructor
TestRun no-arg constructor
Timeout 3: 90
Saved TestRun with Id: 2 TestRun@25a14d5c

If I don't use the object that is returned and just keep using my original object that I called save() on, I can only call save() on it once, otherwise it complains the Entity was updated by another session. 如果我不使用返回的对象并继续使用我调用save()的原始对象,我只能在其上调用save()一次,否则它会抱怨实体已被另一个会话更新。

You can use saveOrUpdate(Object object) method (of Hibernate session ), if you wanted to use same entity object over multiple sessions OR the other option is to use persist() (return type void ), but with persist() (generated id will not be returned), you can look here 你可以使用saveOrUpdate(Object object)方法(Hibernate session ),如果你想在多个会话上使用相同的实体对象,或者另一个选择是使用persist() (返回类型为void ),但是使用persist() (生成的id)不会退货),你可以看看这里

You can look here for session API. 你可以在这里查看session API。

Any idea how to either get save() to return me my same object back? 知道如何让save()返回我的同一个对象吗?

save(object) will return the generated identifier only and NOT the object you have passed to it. save(object)将仅返回生成的identifier而不是您传递给它的对象。 so get that identifier returned by the save method, then set it to the object manually if you need that id. 因此,获取save方法返回的identifier ,如果需要该id,则手动将其设置为对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM