简体   繁体   中英

How to write a domain entity object to multiple database tables using Spring Data Jpa?

I have a domain model that corresponds to multiple tables when I use a relational database to design a data store. Now when I create this domain entity, I want to use Spring Data Jpa to write to multiple data tables at once. How to do it?

1.Domain Model:

{
    "id": 2,
    "startTime": "2018-07-09T08:59:08.853Z",
    "endTime": "2018-07-09T08:59:08.853Z",
    "group": 2,
    "entries": [
        {
            "id": 1,
            "target": 90,
            "isFinished": false,
            "name": "俯卧撑",
            "groups": [
                {
                    "id": 1,
                    "name": "第一组",
                    "number": 30
                },
                {
                    "id": 2,
                    "name": "第一组",
                    "number": 30
                }
            ]
        }
    ]
}

2.Data tables:

tb_execise_plan(id, start_time, end_time, group)
tb_execise_entry(id,name,target, is_finished, execise_plan_id, project_id)
tb_execise_entry_group(id,name,number,execise_entry_id)

3.Table relationship:

tb_execise_plan  1:m tb_execise_entry
tb_execise_entry 1:m tb_execise_entry_group

I recommend you look at Olivier Gierke/Drotbohm presentations and code samples about DDD and Spring data. It's a different approach than JHipster's entity generator which is about one JPA repository per entity.

But starting from JHipster generated repositories, you could aggregate your entities within a @Service annotated class using @Transactional annotated methods.

You can find an example of this approach in your JHipster generated project with UserService class which aggregates a User and its Authorities .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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