3

I've been reviewing various JPA implementations (OpenJPA, Eclipselink, Hibernate so far) and while testing Hibernate 5 I've run into an issue with obtaining relationships explicitly defined by a SQL Select statement. Here's an example set of Entities:

@Entity
@Table(name = "classroom")
public class ClassRoom {

    @Column(name = "id")
    protected Long id;

    @Column(name = "room_number")
    protected String roomNumber;

    @OneToMany(mappedBy = "classRoom", orphanRemoval = true, cascade = CascadeType.ALL)
    protected Set<Desk> desks;

    @OneToMany(mappedBy = "classRoom", orphanRemoval = true, cascade = CascadeType.ALL)
    protected Set<Computer> computers;

}

@Entity
@Table(name = "desk")
public class Desk {

    @Column(name = "id")
    protected Long id;

    @ManyToOne
    @JoinColumn(name = "classroom_id")
    protected ClassRoom classRoom;

}

@Entity
@Table(name = "computer")
public class Computer {

    @Column(name = "id")
    protected Long id;

    @ManyToOne
    @JoinColumn(name = "classroom_id")
    protected ClassRoom classRoom;

}

I have the following method in the ClassRoomService:

    public ClassRoom findClassroom(Long id) {
        List<ClassRoom> retVal = em.createQuery("select distinct c from ClassRoom c " +
                                                "left join fetch c.desks " + 
                                                "left join fetch c.computers " +
                                                "where c.id = :id",
                                            ClassRoom.class)
                                    .setParameter("id", id)
                                    .getResultList();

        return retVal.isEmpty()? null : retVal.get(0);
    }

Without static weaving, the ClassRoom object is obtained as expected, containing the desks/computers. With static weaving enabled, I'm seeing that the desks/computers aren't being populated and if you try to call a getter method to retrieve them, it attempts to lazy load them.

Here's the static weaving I'm doing during the maven build:

        <plugin>
            <groupId>org.hibernate.orm.tooling</groupId>
            <artifactId>hibernate-enhance-maven-plugin</artifactId>
            <version>${hibernate.version}</version>
            <executions>
                <execution>
                    <configuration>
                        <failOnError>true</failOnError>
                        <enableLazyInitialization>true</enableLazyInitialization>
                        <enableDirtyTracking>true</enableDirtyTracking>
                        <enableAssociationManagement>false</enableAssociationManagement>
                        <enableExtendedEnhancement>false</enableExtendedEnhancement>
                        </configuration>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                </execution>
                </executions>
                <dependencies>
                    <dependency>
                    <groupId>javax.transaction</groupId>
                    <artifactId>jta</artifactId>
                    <version>1.1</version>
                </dependency>                            
            </dependencies>
        </plugin>

Is there any way to get Hibernate to populate those while still using static weaving? Is there any obvious problems with what I'm attempting?

Any help would be appreciated. Thanks,

0 Answers0