Micronaut R2DBC 父子关系父 ID 未插入子项(一对多)

问题描述 投票:0回答:1

我是 R2DBC 的新手,我有一个相当标准的父子关系 Person / Address,当尝试保存具有地址的人员对象时,人员和地址都会被保存,但人员 ID 不会保存到地址表中。
我将 Postgres 与 Micronaut 和 R2DBC 一起使用。以下是代码

基础字段:

@Data
@Introspected
@Serdeable
@MappedEntity
public class BaseFields {
    @GeneratedValue
    @Id
    private Long id;
    private String note;
    private String recordType;

人:

@Data
@Introspected
@Serdeable
@MappedEntity
public class Person extends BaseFields{
    private String firstName;
    private String lastName;
    private String phoneNumber;

    @JoinColumn(name = "personId")
    @Relation(value = Relation.Kind.ONE_TO_MANY, cascade = Relation.Cascade.ALL)
    private Set<Address> addresses;
}

地址:

@Data
@Introspected
@Serdeable
@MappedEntity
public class Address extends BaseFields {
    private String street;
    private String street2;
    private String suite;
    private String city;
    private String stateProvince;
    private String country;
    private String postalCode;
    private Boolean active;

    private Long personId;

}

人员存储库:

@Introspected
@R2dbcRepository(dialect = Dialect.POSTGRES)
public interface PersonRepository extends ReactiveStreamsCrudRepository<Person, Long>{
    @NonNull
    @Override
    @Join(value = "addresses", type = Join.Type.FETCH)
    Flux<Person> findAll();

    @NonNull
    @Override
    @Join(value = "addresses", type = Join.Type.FETCH)
    Mono<Person> findById(@NonNull @NotNull Long id);

}

人控制器:

@Controller("/person")
public class PersonController {
    private final PersonRepository personRepository;
    private final PersonService personService;

    PersonController(PersonRepository personRepository, PersonService personService) {
        this.personRepository = personRepository;
        this.personService = personService;

    @SingleResult
    @Post
    Mono<Person> create(@Valid @Body Person person){
//        return personService.createPerson(person);
          return Mono.from(personRepository.save(person));
    }
}

邮递员身体和结果:

{
        "firstName": "Kelly",
        "lastName": "Kingster",
        "phoneNumber": "451.881.9745",
        "addresses": [
            {
                "street": "155379 W12th Street",
                "city": "Fargo",
                "stateProvince": "ND",
                "country": "USA",
                "postalCode": "44577",
                "active": true
            }
        ]
}

Result:

{
    "id": 11,
    "firstName": "Kelly",
    "lastName": "Kingster",
    "phoneNumber": "451.881.9745",
    "addresses": [
        {
            "id": 12,
            "street": "155379 W12th Street",
            "city": "Fargo",
            "stateProvince": "ND",
            "country": "USA",
            "postalCode": "44577",
            "active": true
        }
    ]
}

两条记录都插入了,但地址记录上没有 person_id

"id","street","street2","suite","city","state_province","country","postal_code","active","person_id","record_type","note","created","modified"

12,"155379 W12th Street",NULL,NULL,"Fargo","ND","USA","44577",True,NULL,NULL,NULL,"2024-07-09 15:22:20.637259","2024-07-09 15:22:20.637259"
postgresql one-to-many micronaut r2dbc
1个回答
0
投票

尝试像这样更新实体上的注释。


public class Person ...{

    // ...
    @Relation(value = Relation.Kind.ONE_TO_MANY, cascade = Relation.Cascade.ALL)
    private Set<Address> addresses;
}


public class Address...{

    @Relation(value = Relation.Kind.MANY_TO_ONE)
    @JoinColumn("person_id") // move here
    private Person person;

}

查看我的 Micronaut R2dbc 示例项目此处,其中演示了一对多关系。

顺便说一句:Micronaut Data 在 Micronaut data Jdbc/R2dbc 中支持 JPA 注解,如果你比较熟悉可以直接使用 JPA 注解。

© www.soinside.com 2019 - 2024. All rights reserved.