JPA: How can an @Embeddable object get a reference to its owner?

前端 未结 2 1477
醉梦人生
醉梦人生 2021-02-08 02:35

I have a User class that has @Embedded a class Profile. How can I give the instances of Profile a reference to their owner the User class?

@Entity
class User imp         


        
2条回答
  •  南旧
    南旧 (楼主)
    2021-02-08 03:11

    Refer to the official documentation ,section 2.4.3.4. , http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/ , you can use @org.hibernate.annotations.Parent to give the Profile object a back-pointer to its owning User object and implement the getter of the user object .

    @Embeddable
    class Profile implements Serializable {
    
       @org.hibernate.annotations.Parent
       User user; // how to make this work?
    
       setURL(String url) {
          if (user.active() ) { // for this kind of usage
             // do something
          }
       }
    
       User getUser(){
           return this.user;
       }
    
       // .. other properties ..
    }
    

提交回复
热议问题