Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringBoot:这是保存具有多个关系的新条目的正确方法吗?_Spring_Hibernate_Jpa_Spring Boot - Fatal编程技术网

SpringBoot:这是保存具有多个关系的新条目的正确方法吗?

SpringBoot:这是保存具有多个关系的新条目的正确方法吗?,spring,hibernate,jpa,spring-boot,Spring,Hibernate,Jpa,Spring Boot,我有两个实体Person和Visit 个人与访问有一方关系。 我想知道是否要保存一个新的访问条目,以及使用RestController的过程。我的方法正确吗?还是有其他更有效的方法 因此,我有一个从RequestBody获取VisitModel的控制器,这样调用它是否正确 VisitModel具有person ID和访问实体所需的属性。我使用person的ID在personRepository中查找相关的person条目,然后将其发布到新的Visit实例,然后使用VisitRespository

我有两个实体
Person
Visit

个人
访问
有一方关系。 我想知道是否要保存一个新的访问条目,以及使用RestController的过程。我的方法正确吗?还是有其他更有效的方法

因此,我有一个从RequestBody获取
VisitModel
的控制器,这样调用它是否正确

VisitModel具有person ID和访问实体所需的属性。我使用person的ID在personRepository中查找相关的person条目,然后将其发布到新的Visit实例,然后使用VisitRespository保存它

@RequestMapping(value="", method=RequestMethod.POST)
public String checkIn(@RequestBody VisitModel visit) {

    Person person = personRepository.findById(visit.personId);

    Visit newVisit = new Visit(visit.getCheckIn, person);
    visitRepository.save(newVisit);

    return "success";
}
访问实体如下所示

@Entity
public class Visit {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @JsonProperty("check_in")
    private Date checkIn;

    @JsonProperty("check_out")
    private Date checkOut;

    @ManyToOne
    @JoinColumn(name="personId")
    private Person person;

    public Visit(Date checkIn, Person person) {
        this.checkIn = checkIn;
        this.person = person;
    }

    public Date getCheckIn() {
        return checkIn;
    }

    public void setCheckIn(Date checkIn) {
        this.checkIn = checkIn;
    }

    public Date getCheckOut() {
        return checkOut;
    }

    public void setCheckOut(Date checkOut) {
        this.checkOut = checkOut;
    }

    public Person getPerson() {
        return person;
    }

}

我想知道以下方法是否正确。或者还有其他更好的方法吗?

是的,在我看来,这是映射双向关系的标准方法。编辑:
personId
列指向
Person
实体的“id”字段。例如:

@Id
private Long id;

更新:1:VisitModel是“DTO”或数据传输对象。任何单独的包装都可以。您可以考虑将它们放入单独的jar中,以便使用API的任何人(使用java)可以在调用之前使用jar创建数据。2) 就我所知,您保存它的方式很好。

当然,您不需要从数据库中获取一个
人员来将它与
访问关联起来。因为,您只需要拥有
Person
id
即可将其保存在外键列
personId

如果您使用JPA
EntityManager

  Person person = entityManager.getReference(Person.class, visit.personId);
对于Hibernate
会话

  Person person = session.load(Person.class, visit.personId);
此方法只创建一个代理,不执行任何数据库请求

在Hibernate
Session
中,我使用了@madhusudanareddysunapu建议的
新人物(personId)
。一切都很顺利


那么您将把
VisitModel
放在哪个软件包中。现在我有实体和存储库类,包含在域包中。但是VisitModel不适合这里。所以为了保存一个访问条目,你也会像我一样做吗?首先使用
personId
查找此人,然后将其发送给新的访问实例?谢谢。不知道DTO是它的概念名称:)不明白,为什么应该是:personId列在Person实体中也应该命名为personId,如果它是namedone,我可以看到的优化不是使用repository方法获取person对象,而是使用new操作符创建person对象并填充ID字段并使用它。这将保存一个DB命中以获取person对象。@MadhusudanaReddySunnapu您的意思如下
newPerson(personId)?是的。您是否发现了任何问题?当使用
新人(personId)
时,它没有从数据库加载条目。然而,使用EntityManager它工作得很好。使用
会话
时,我无法使用
@Autowired
注入它。如何获取会话实例?@starcorn如果使用
EntityManager
-使用
getReference()
newperson(personId)
(当然,如果有效的话)。不要混合使用
EntityManager
会话
。回答您的问题:您可以从
EntityManager
session session=EntityManager.unwrap(session.class)创建会话