Saving a foreign key using entity with only id valorized

后端 未结 1 1213
心在旅途
心在旅途 2021-01-19 18:51

If I have two hibernate entities like:

@Entity
class Company {
     @Id
     Integer id;
     String name;
}

@Entity
class Person {
     Integer id;
     St         


        
相关标签:
1条回答
  • 2021-01-19 19:38

    Yes, you can do it.

    Does it save just the reference, or also update the company with name=null?

    With default cascade, like that, Hibernate will do nothing. So answer is: It saves just a foreign key.

    The most valid (for JPA too) approach is using session.load(Company.class, 1). It returns a proxy without do any request to the database. But, you need to have a session for it, of course.

    0 讨论(0)
提交回复
热议问题