The best option is to make the assembly lazy loaded, and then change the queries in which the collection is needed for active loading (using connection interception). If you have a very good reason not to do this, you can try the following workarounds.
You can use projection query. This will result in an [employee, name] array for each result.
select employee, employee.dept.name from Employee employee
You can use @Formula to map an attribute in the Employee table to a column in the Department table (note that this solution applies to Hibernate)
class Employee { @Formula("(select deptName from Department where Department.id = DEPT_ID)" String deptName; }
Another option is to create a new DeptLite class that does not have a collection. Match it as readonly - @org.hibernate.annotations.Entity(mutable=false) .
@Entity public class Employee { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; @OneToOne(cascade = {CascadeType.PERSIST}) @JoinColumn(name="DEPT_ID") private Dept dept; @OneToOne(updatable=false,insertable=false) @JoinColumn(name="DEPT_ID") private DeptLite deptLite; ... } @Entity @org.hibernate.annotations.Entity(mutable=false) class DeptLite { }
source share