简体   繁体   English

实体在应进行管理时分离

[英]Entity gets detached when should be managed

Consider Controller-Service-Repository arch. 考虑Controller-Service-Repository拱。

Successful method in TokenService declaring that I am correct to the point. TokenService中成功的方法声明我是正确的。

@Transactional
public Token getByString(String tokenString) {


    Token t = tr.loadTokenByString(tokenString);

    t.setTokenType("SERVICE MODIF"); // note this test line works and changes are propagated to db
    em.flush();


    return t;
}

Now we are inside controller handler mapping calling previous service method and extracting the token: 现在,我们在控制器处理程序映射内部,调用先前的服务方法并提取令牌:

logger.info(urlToken);

        Object obj;

        Token token;

        obj = tokenService.getByString(urlToken);
        User u;
        if (obj != null) {



            token = (Token) obj;

            // tokenService.save(token); - am angry lost detached entity throwing exception that i am detached

            token.setTokenType("helllo"); // this does not propagate to db since entity is detached


            logger.info(token.toString());

            u = token.getUser();
            userService.activateUser(u);

        }

As you can see from the comments the next setTokenType inside controller call does not do anything thus I called save to check the state which without further guessing notified me that entity became detached. 从注释中可以看到,控制器调用中的下一个setTokenType不执行任何操作,因此我调用save检查状态,而无需进一步猜测就通知我该实体已分离。

Does it imply that I have to do all the changes inside service classes? 这是否意味着我必须在服务类中进行所有更改? What's wrong with controller context, if I just need to call one mutator I don't want to be forced to create a whole chain of repository-service to make it from controller Oo 控制器上下文有什么问题,如果我只需要调用一个mutator,我就不会被迫创建整个存储库服务链来由控制器Oo来实现

I recall somewhere deep in my brain that there is a requirement for some magical filter to account for controller context, but that was long time ago and I cannot recall what kind of filter is required. 我回想起在大脑深处的某个地方,需要一些神奇的过滤器来说明控制器的上下文,但是那是很久以前的事了,我无法回忆起需要哪种过滤器。 Maybe I am completely wrong. 也许我是完全错误的。

I doubt I would able to merge inside controller since asfaik even .flush throws exception whenever I forget to declare service transactional. 我怀疑我是否能够在控制器内部merge ,因为每当我忘记声明服务为事务性时,即使.flush甚至.flush引发异常。 And I doubt it is either possible or even correct to declare controller handler method transactional and autowire persistence context directly just to call merge. 我怀疑直接声明控制器处理程序方法事务性和自动装配持久性上下文以调用合并是否可能甚至正确。

Ideal scenario of course would be to maintain persistent state of the entity even when it gets retrieved by Controller chain call. 当然,理想的情况是即使通过Controller链调用检索到实体,也要保持实体的持久状态。

When you declare that method transactional it will be transactional. 当您声明该方法为事务性时,它将是事务性的。 That and only that. 那并且只有那。

After the transaction is complete you cannot make any changes to that in a sense that whatever the reference object you have associated with that is no longer managed by the session. 事务完成后,您将无法对此进行任何更改,从某种意义上说,与您关联的任何引用对象都不再由会话管理。 (Because the transaction is over). (因为交易结束)。

If you want to change the object in a managed state do that in the transactional method it self (within the same transaction). 如果要更改处于托管状态的对象,请使用自身(在同一事务中)的事务方法进行。

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

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