Announcement Announcement Module
Collapse
No announcement yet.
related one-to-many objects not obtained via JPA2 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • related one-to-many objects not obtained via JPA2

    Must be something very silly...
    Have a simple one to many relationship...One user related to many primaryPeople
    And the entity code captures this well as shown below:
    User.java
    @OneToMany(mappedBy = "fkUserId", cascade = CascadeType.ALL)
    private Set<PrimaryPerson> primarypeople;

    PrimaryPerson.java
    @ManyToOne
    @JoinColumn(name = "fk_user_id", referencedColumnName = "pk_user_id", nullable = false)
    private User fkUserId;

    In my service layer..have this
    public User findUser(java.lang.Integer id) {
    return userRepository.findOne(id);
    }

    But when I get the user back it is not populated with a list/set of primaryPeople
    Why is there no eager-fetching? Should be by default.

    I have this code too for the UserRepository
    @Repository
    public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User> {
    ...
    }
    Also pom says this
    <dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.0-api</artifactId>
    <version>1.0.1.Final</version>
    </dependency>

    I could do it via a Specification. or a NamedQuery..but that would be crude when using JPA
    This kinda thing is out of the box..so what is amiss?

  • #2
    I plan to use this solution from one Graham Smith..
    http://www.wobblycogs.co.uk/index.ph...n-a-collection

    Basically it schools me "OneToMany relationship is lazy loaded by default under JPA"
    So will try this: @OneToMany( fetch= FetchType.EAGER )

    Comment

    Working...
    X