How can I prevent Hibernate fetching joined entities when I access only the foreign key id?

后端 未结 1 329
一向
一向 2020-12-30 05:24

I have a Hibernate entity Parent that is joined to another: Child. In the database I have a column parent.child_id that has a foreign

相关标签:
1条回答
  • 2020-12-30 05:47

    It's caused by the fact that Child uses field access (since annotations are placed on fields), therefore Hibernate simply initializes the proxy when you call any of its methods.

    If you move annotations to properies, it would work as expected.

    Since JPA 2.0 (Hibernate 3.5) you can configure it in fine-grained way:

    @Access(AccessType.FIELD) // Default is field access
    class Child {
        private Integer id;
    
        @Column(name = "name")
        private String name;
    
        @Access(AccessType.PROPERTY) // Use property access for id
        @Id @Column(name = "id", unique = true, nullable = false)
        public Integer getId() { ... }
    
        ...   
    }
    
    0 讨论(0)
提交回复
热议问题