Mysql JPA集';s id,但实体不';不存在

Mysql JPA集';s id,但实体不';不存在,mysql,spring-data,eclipselink,Mysql,Spring Data,Eclipselink,我正在使用JPA(Eclipse链接)、Spring数据和MySQL。我在事务(提交)和id生成方面有问题。我持久化了一个实体(其中包含一个分离的实体),并且在事务之后我想使用该id。生成了该id,但实体管理器找不到该实体 例如: Pojo: } JPA实体 @Entity public class Action { @ManyToOne(fetch = FetchType.LAZY, optional = false) private ActionTy

我正在使用JPA(Eclipse链接)、Spring数据和MySQL。我在事务(提交)和id生成方面有问题。我持久化了一个实体(其中包含一个分离的实体),并且在事务之后我想使用该id。生成了该id,但实体管理器找不到该实体

例如: Pojo:

}

JPA实体

 @Entity
    public class Action {
        @ManyToOne(fetch = FetchType.LAZY, optional = false)
        private ActionType actionType;

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        Long id;
    }




@Entity
public class ActionType {
    String name;
}
服务:

    @Transactional(readOnly = true)
public class ActionManagement {

    EntityManager entityManager;

    @Transactional(isolation = Isolation.READ_COMMITTED)
    public Assignment assignAction(final ActionType type) {

        entityManager.detach(type);

        Action action = new Action();
        action.setActionType(type);

        action = entityManager.persist(action);
        // id is null, because here is commit or fluh triggerd.
        System.out.println(action.getId());

        return new Assignment(action);

    }
}
单元测试:

public void unitTest(){

    ActionType type = createRandomActionType();

    Assignment assignAction = actionManagement.assignAction(type);
    //ID is NOT null (e.d id:2), but the entity is null
    entityManager.find(Action.class,assignAction.getAction().getId())
}
当前的问题是,在事务之后,操作实体有一个id,但在查找之后,该实体始终为空

有人知道怎么回事吗


提前感谢

打开JPA日志记录,并在执行查找之前检查事务是否已完全提交。当然,我打开了JPA日志记录。看起来不错。find是否正在发出SQL查询?执行查询是否返回预期结果?也有助于向我们显示日志。您必须删除@Transactional(readOnly=true)。
public void unitTest(){

    ActionType type = createRandomActionType();

    Assignment assignAction = actionManagement.assignAction(type);
    //ID is NOT null (e.d id:2), but the entity is null
    entityManager.find(Action.class,assignAction.getAction().getId())
}