@OneToMany(cascade=CascadeType.ALL, mappedBy = "veryOldPerson") @Fetch(FetchMode.SELECT) public Collection<Stay> getVeryOldStays() { return veryOldStays; }
if ( fetch.value() == org.hibernate.annotations.FetchMode.JOIN ) { toOne.setFetchMode( FetchMode.JOIN ); toOne.setLazy( false ); toOne.setUnwrapProxy( false ); else if ( fetch.value() == org.hibernate.annotations.FetchMode.SELECT ) { toOne.setFetchMode( FetchMode.SELECT ); else if ( fetch.value() == org.hibernate.annotations.FetchMode.SUBSELECT ) { throw new AnnotationException( "Use of FetchMode.SUBSELECT not allowed on ToOne associations" ); throw new AssertionFailure( "Unknown FetchMode: " + fetch.value() );
if ( fetch.value() == org.hibernate.annotations.FetchMode.JOIN ) { collection.setFetchMode( FetchMode.JOIN ); collection.setLazy( false ); else if ( fetch.value() == org.hibernate.annotations.FetchMode.SELECT ) { collection.setFetchMode( FetchMode.SELECT ); else if ( fetch.value() == org.hibernate.annotations.FetchMode.SUBSELECT ) { collection.setFetchMode( FetchMode.SELECT ); collection.setSubselectLoadable( true ); throw new AssertionFailure( "Unknown FetchMode: " + fetch.value() );
@JoinTable(name = "REVCHANGES", joinColumns = @JoinColumn(name = "REV")) @Column(name = "ENTITYNAME") @Fetch(FetchMode.JOIN) @ModifiedEntityNames private Set<String> modifiedEntityNames = new HashSet<>();
private void applyFetchMode(Root<T> root) { for (Field field : getDomainClass().getDeclaredFields()) { Fetch fetch = field.getAnnotation(Fetch.class); if (fetch != null && fetch.value() == FetchMode.JOIN) { root.fetch(field.getName(), JoinType.LEFT); } } }
@ManyToOne(targetEntity = Team.class) @Fetch(FetchMode.SELECT) @JoinColumn(name = "team_id") public Team getTeam() { return team; }
if ( fetch.value() == org.hibernate.annotations.FetchMode.JOIN ) { toOne.setFetchMode( FetchMode.JOIN ); toOne.setLazy( false ); toOne.setUnwrapProxy( false ); else if ( fetch.value() == org.hibernate.annotations.FetchMode.SELECT ) { toOne.setFetchMode( FetchMode.SELECT ); else if ( fetch.value() == org.hibernate.annotations.FetchMode.SUBSELECT ) { throw new AnnotationException( "Use of FetchMode.SUBSELECT not allowed on ToOne associations" ); throw new AssertionFailure( "Unknown FetchMode: " + fetch.value() );
@JoinTable(name = "REVCHANGES", joinColumns = @JoinColumn(name = "REV")) @Column(name = "ENTITYNAME") @Fetch(FetchMode.JOIN) @ModifiedEntityNames private Set<String> modifiedEntityNames = new HashSet<>();
if ( fetch.value() == org.hibernate.annotations.FetchMode.JOIN ) { collection.setFetchMode( FetchMode.JOIN ); collection.setLazy( false ); else if ( fetch.value() == org.hibernate.annotations.FetchMode.SELECT ) { collection.setFetchMode( FetchMode.SELECT ); else if ( fetch.value() == org.hibernate.annotations.FetchMode.SUBSELECT ) { collection.setFetchMode( FetchMode.SELECT ); collection.setSubselectLoadable( true ); throw new AssertionFailure( "Unknown FetchMode: " + fetch.value() );
@javax.persistence.Entity @Table(name="entity") public static class AnEntity { @Id @GeneratedValue private Long id; @ManyToOne private OtherEntity otherEntityDefault; @ManyToOne @Fetch(FetchMode.JOIN) private OtherEntity otherEntityJoin; @ManyToOne @Fetch(FetchMode.SELECT) private OtherEntity otherEntitySelect; // @Fetch(FetchMode.SUBSELECT) is not allowed for ToOne associations }
@ManyToOne(cascade = CascadeType.ALL) @LazyToOne(LazyToOneOption.PROXY) @Fetch(FetchMode.SELECT) @JoinColumn(name = "oldperson") public Person getOldPerson() { return oldPerson; }
@OneToMany(cascade=CascadeType.ALL, mappedBy = "oldPerson") @LazyCollection(LazyCollectionOption.EXTRA) @Fetch(FetchMode.SUBSELECT) public Collection<Stay> getOldStays() { return oldStays; }
@OneToMany(targetEntity = Player.class, mappedBy = "team", fetch = FetchType.EAGER) @Fetch(FetchMode.SELECT) @Loader(namedQuery = "loadByTeam") public Set<Player> getPlayers() { return players; }
@OneToMany(targetEntity = ChildEntity.class, mappedBy = "parent") @LazyCollection(LazyCollectionOption.EXTRA) @Fetch(FetchMode.SELECT) public Set<ChildEntity> getChildren() { return children; }
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @LazyToOne(LazyToOneOption.PROXY) @Fetch(FetchMode.JOIN) @JoinColumn(name = "veryoldperson") public Person getVeryOldPerson() { return veryOldPerson; }
@Entity(name = "Department") public static class Department { @Id private Long id; @OneToMany(mappedBy = "department", fetch = FetchType.LAZY) @Fetch(FetchMode.SELECT) private List<Employee> employees = new ArrayList<>(); //Getters and setters omitted for brevity //end::fetching-strategies-fetch-mode-select-mapping-example[] public Long getId() { return id; } public void setId(Long id) { this.id = id; } public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } //tag::fetching-strategies-fetch-mode-select-mapping-example[] }
@Entity(name = "Department") public static class Department { @Id private Long id; //tag::fetching-strategies-fetch-mode-join-mapping-example[] @OneToMany(mappedBy = "department") @Fetch(FetchMode.JOIN) private List<Employee> employees = new ArrayList<>(); //end::fetching-strategies-fetch-mode-join-mapping-example[] //Getters and setters omitted for brevity public Long getId() { return id; } public void setId(Long id) { this.id = id; } public List<Employee> getEmployees() { return employees; } public void setEmployees(List<Employee> employees) { this.employees = employees; } }
@Entity(name = "MainEntity") public static class MainEntity { @Id @GeneratedValue private Long id; @ManyToOne(fetch = FetchType.LAZY) @Fetch(org.hibernate.annotations.FetchMode.JOIN) private SubEntity sub; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public SubEntity getSub() { return sub; } public void setSub(SubEntity sub) { this.sub = sub; } }
@Entity(name = "Product") public static class Product { @Id @GeneratedValue private int id; @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @NotFound(action = NotFoundAction.IGNORE) @JoinColumn(name = "categoryId") @Fetch(FetchMode.SELECT) private Category category; private ContainedCategory containedCategory; @ElementCollection(fetch = FetchType.EAGER) private Set<ContainedCategory> containedCategories = new HashSet<>(); }
@Entity(name = "Product") public static class Product { @Id @GeneratedValue private int id; @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @NotFound(action = NotFoundAction.IGNORE) @JoinColumn(name = "categoryId") @Fetch(FetchMode.JOIN) private Category category; private ContainedCategory containedCategory; @ElementCollection(fetch = FetchType.EAGER) private Set<ContainedCategory> containedCategories = new HashSet<>(); }