当前位置: 首页 > 知识库问答 >
问题:

保存spring data rest中具有一对一映射的实体

邓正真
2023-03-14

我有一个非常简单的spring boot+spring data rest应用程序。我试图使用spring data rest保存具有一对一映射的实体,但看起来只保存了父实体,子实体没有保存。下面是我的代码

@SpringBootApplication
public class Application{
    @Autowired
    private PersonRepository personRepo;    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args); 
    }
    @Bean
    CommandLineRunner init(){
        Address address = new Address();
        address.setCountry("US");
        address.setState("SV");
        Person person = new Person();
        person.setName("Vincent");
        person.setAddress(address);
        personRepo.save(person);
        return null;
    }   
}


@Entity
public class Address implements Serializable{

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private int id;
    private String country;
    private String state;
}

@Entity
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private int id;
    private String name;
    @OneToOne(cascade={CascadeType.ALL})
    private Address address;
}

@Projection(name="inlineAddress",types={Person.class})
public interface InlineAddress {

    String getName();
    Address getAddress();
}

@RepositoryRestResource(excerptProjection=InlineAddress.class)
public interface PersonRepository extends JpaRepository<Person, Integer> {
    Person findByName(@Param("name") String name);

    Person findById(@Param("id") int id);

    Page<Person> findByNameStartsWith(@Param("name") String name, Pageable page);
}

public interface AddressRepository extends JpaRepository<Address, Integer> {

}

启动后,如果我访问http://localhost:8080/api/,我会看到下面的响应

   {
        _links: {
             addresses: {
                 href: "http://localhost:8080/api/addresses{?page,size,sort}",
                 templated: true
             },
             persons: {
                 href: "http://localhost:8080/api/persons{?page,size,sort,projection}",
                 templated: true
             },
             profile: {
                 href: "http://localhost:8080/api/profile"
             }
        }
    }
{
    "_embedded": {
        "persons": [
            {
                "address": {
                    "country": "US",
                    "state": "SV"
                },
                "name": "Vincent",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/api/persons/1"
                    },
                    "person": {
                        "href": "http://localhost:8080/api/persons/1{?projection}",
                        "templated": true
                    },
                    "address": {
                        "href": "http://localhost:8080/api/persons/1/address"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/api/persons"
        },
        "profile": {
            "href": "http://localhost:8080/api/profile/persons"
        },
        "search": {
            "href": "http://localhost:8080/api/persons/search"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 1,
        "totalPages": 1,
        "number": 0
    }
}
{
    "name": "Michael",
    "address": {
        "country": "US",
        "state": "SV"
        }

}
{
    "_embedded": {
        "persons": [
            {
                "address": {
                    "country": "US",
                    "state": "SV"
                },
                "name": "Vincent",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/api/persons/1"
                    },
                    "person": {
                        "href": "http://localhost:8080/api/persons/1{?projection}",
                        "templated": true
                    },
                    "address": {
                        "href": "http://localhost:8080/api/persons/1/address"
                    }
                }
            },
            {
                "address": null,
                "name": "Michael",
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/api/persons/2"
                    },
                    "person": {
                        "href": "http://localhost:8080/api/persons/2{?projection}",
                        "templated": true
                    },
                    "address": {
                        "href": "http://localhost:8080/api/persons/2/address"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/api/persons"
        },
        "profile": {
            "href": "http://localhost:8080/api/profile/persons"
        },
        "search": {
            "href": "http://localhost:8080/api/persons/search"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 2,
        "totalPages": 1,
        "number": 0
    }
}

我的代码有什么问题吗?我尝试使用旧的方法,不使用spring data rest,但使用rest控制器,我发布的相同json运行良好。不确定为什么spring data rest在这里不起作用。

共有1个答案

郎魁
2023-03-14

好吧。似乎没办法这么做。我必须首先通过{“name”=“michael”}发布一个人,然后通过发布一个地址{“country”:“美国”,“state”:“sv:},最后将地址发送给此人{”name“:”michael“,”address“:”localhost:8080/addresses/1“}

 类似资料:
  • 我有一个实体: 要创建上述实体并将其保存到JPA存储库中,我将执行以下操作: 由于DB中的表存储了候选对象的FK,我认为设置一个id就足够了。但JPA希望我设置候选对象。这迫使我查询候选存储库。 是否需要从候选库中查询候选人以保存,或者如果我有可用的候选人id,我不能直接设置它?

  • 我有两个表,第一个表包含公司id、公司名称和国家名称,第二个表包含公司相关的详细信息和一对一的映射。 我想使用Hibernate进行以下映射:- 公司表的Company_id应该映射company_detail表的company_id,所以我在POJO中设置了以下映射 另外,在setter中添加以下代码:- 这段代码运行良好,在保存公司时只有一个问题,null存储在company_detal的co

  • 有两张数据表,其中A表的某个字段的值指向B表的主键。因为A表的任何一条记录只能对应B表的一条且唯一一条记录,所以称这种 映射为A表对B表数据的一对一映射。(当然,反过来,你也可是说,是B表对A表的一对多映射)。 上述结构,如果用 POJO 来表示的话,可以参看下图: 如上图, Pet 中就可以有一个字段 master,通过自身的 masterId 指向一个 Master 对象,那么我们说 Pet.

  • 我有一对一的关系 我可以添加多个地址。例如 我想看到address2被添加时抛出,但jpa允许。我想在spring jpa级别阻止这一点,我该怎么做?

  • 如何定义两个类之间的< code >一对一关系?我想我在某个地方出了问题,在概念上。我不知道是什么,但确实是。 让我们假设有两个类命名为 和 : 现在对于一个国家来说,只能有一个PM,PM只能属于一个国家。如何在映射文件中执行此操作? 我在尝试这个: 但是这种有效的映射只会在运行时产生异常。它说属性在类中找不到!但是它在类中搜索吗?它应该在类中搜索。 也帮我完成这两个类之间的< code >一对一

  • 问题内容: 我有一个用户表和一个具有一对一映射表的表,其中一个字段用于此关系,该字段存储相应用户的ID字段值。 如何为此关系编写hibernate文件? 更新 我的问题是用户的主键是,user_detail的外键是 我在Internet用户user_id中获得的所有示例均作为用户主键,与其他表中的外键相同 问题答案: 对于用户映射…。 用于UserDetail映射。