具有一对多子实体的Spring-JPA保存实体

时间:2015-11-26 21:34:22

标签: spring jpa transactions

环境:Java 8,spring platform-b​​om-1.1.4-RELEASE,mysql 5.7

当使用自引用的一对多子实体保存实体时,我遇到问题。

这是实体:

public class ActivityEntity extends MDMBaseDescriptiveEntity {
    @Column(name="project_id")
    private Integer projectId;

    @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    @JoinColumn(name="container_id", referencedColumnName="id")
    @OrderColumn(name="order")
    private List<ActivityEntity> activities = new ArrayList<ActivityEntity>();
    
    @Enumerated(EnumType.STRING)
    private Activity.Type type;

    @Enumerated(EnumType.STRING)
    private Activity.Mode mode;
    
    private String tag;
...
}

这是存储库:

@Transactional
public interface IActivityDao extends JpaRepository<ActivityEntity, Integer> {
    
}

这是服务:

    @Transactional
    @Override
    public Activity saveActivity(Activity activity)
    {
        Map<Activity, ActivityEntity> converted = ModelConverter.convert(activity);
        ActivityEntity activityEntity = converted.get(activity);
        activityDao.save(activityEntity);
        
        List<ResourceDefinitionEntity> resourceEntities = new ArrayList<>();
        for (Activity act : converted.keySet()) {
            ActivityEntity entity = converted.get(act);
            for (ResourceDefinition resource : act.getInputResources()) {
                ResourceDefinitionEntity resourceEntity = ModelConverter.convert(resource);
                resourceEntity.setScope(ResourceScope.ACTIVITY);
                resourceEntity.setResourceOwnerId(entity.getId());
                resourceEntities.add(resourceEntity);
            }
            for (ResourceDefinition resource : act.getOutputResources()) {
                ResourceDefinitionEntity resourceEntity = ModelConverter.convert(resource);
                resourceEntity.setScope(ResourceScope.ACTIVITY);
                resourceEntity.setResourceOwnerId(entity.getId());
                resourceEntity.setType(ResourceDefinition.Type.INSTANCE);
                resourceEntities.add(resourceEntity);
            }
        }
        
        if (!resourceEntities.isEmpty()) {
            resourceDefinitionDao.save(resourceEntities);
        }
        return getActivity(activityEntity.getId());
    }

这是dao测试:

    @Transactional
    @Test
    public void testSave() throws Exception {
        ActivityEntity activity = setup();
        printActivity(activity, 0);
        activityDao.save(activity);
        
        ActivityEntity retrieved = activityDao.findOne(activity.getId());
        printActivity(retrieved, 0);
    }

这是服务测试:

    @Transactional
    @Test
    public void testSaveActivity() {
        Activity container = TestDataProvider.activity();
        Activity saved = activityService.saveActivity(container);
        
        checkEquals(container, saved);
    }

部署环境中的服务impl在保存没有子活动的活动时工作正常;但未能通过子活动保存活动。 以下是失败的日志消息:

Hibernate: insert into activity (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, project_id, script, tag, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into activity (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, project_id, script, tag, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into activity (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, project_id, script, tag, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into activity (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, project_id, script, tag, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into activity (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, project_id, script, tag, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into activity (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, project_id, script, tag, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into resource_definition (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, value, owner_id, scope, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into resource_definition (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, value, owner_id, scope, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into resource_definition (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, value, owner_id, scope, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into resource_definition (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, value, owner_id, scope, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into resource_definition (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, value, owner_id, scope, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into resource_definition (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, value, owner_id, scope, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into resource_definition (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, value, owner_id, scope, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into resource_definition (created_date, created_user_id, deleted_date, deleted_user_id, updated_date, updated_user_id, description, name, mode, value, owner_id, scope, type) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: select resourcede0_.id as id1_12_, resourcede0_.created_date as created_2_12_, resourcede0_.created_user_id as created_3_12_, resourcede0_.deleted_date as deleted_4_12_, resourcede0_.deleted_user_id as deleted_5_12_, resourcede0_.updated_date as updated_6_12_, resourcede0_.updated_user_id as updated_7_12_, resourcede0_.description as descript8_12_, resourcede0_.name as name9_12_, resourcede0_.mode as mode10_12_, resourcede0_.value as value11_12_, resourcede0_.owner_id as owner_i12_12_, resourcede0_.scope as scope13_12_, resourcede0_.type as type14_12_ from resource_definition resourcede0_ where resourcede0_.owner_id=? and resourcede0_.scope=? and resourcede0_.mode=?
Hibernate: update activity set container_id=?, order=? where id=?
2015-11-26 16:07:55,331 [main]  WARN o.h.e.j.s.SqlExceptionHelper - SQL Error: 1064, SQLState: 42000
2015-11-26 16:07:55,333 [main] ERROR o.h.e.j.s.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order=0 where id=707' at line 1
2015-11-26 16:07:55,335 [main]  WARN o.h.e.j.s.SqlExceptionHelper - SQL Warning Code: 1064, SQLState: 42000
2015-11-26 16:07:55,336 [main]  WARN o.h.e.j.s.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order=0 where id=707' at line 1

所以我回到服务测试,如果我在测试方法testSaveActivity()上有@Transactional注释,测试工作正常。如果我从测试方法中删除@Transactional注释,测试将失败,并显示与上面显示的相同的错误。我试图将带有不同参数的@Transactional注释添加到服务方法中,它根本没有帮助;试图将@Transational注释添加到控制器方法中,没有用。

对测试方法的注释工作对我来说是一个谜。不明白为什么它在其他地方不起作用。

以下堆栈跟踪到失败点。

enter image description here

0 个答案:

没有答案