I want to retrieve the records from a table without its associations mentioned in the entity. How to do this using Hibernate?
I want to get the record in Animal table without trying to retrieve the Breed in the below example.
Below are the associations i have:
@Entity
@Table(name = "ANIMAL")
public class Animal {
private Long id;
private String type;
private String subtype;
private String category;
private Set<Breed> associatedBreeds = new HashSet<Breed>();
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idSeq")
@SequenceGenerator(name = "idSeq", sequenceName = "ID_SEQ")
public Long getId() {
return id;
}
@Column(name = "TYPE")
public String getType() {
return type;
}
@Column(name = "SUBTYPE")
public String getSubtype() {
return subtype;
}
public void setId(Long id) {
this.id = id;
}
public void setType(String type) {
this.type = type;
}
public void setSubtype(String subtype) {
this.subtype = subtype;
}
public void setCategory(String category) {
this.category = category;
}
@Column(name = "CATEGORY", insertable = false, updatable = false)
public String getCategory() {
return category;
}
@OneToMany(mappedBy = "propertyUsage", fetch = FetchType.EAGER, cascade = { CascadeType.ALL })
public Set<Breed> getAssociatedBreeds() {
return associatedBreeds;
}
public void setAssociatedBreeds(
Set<Breed> associatedFinancials) {
this.associatedBreeds = associatedBreeds;
}
}
@Entity
@Table(name = "Breed")
public class Breed {
private Long id;
private Animal animal;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idSeq1")
@SequenceGenerator(name = "idSeq1", sequenceName = "ID_SEQ1")
public Long getId() {
return id;
}
public void setPropertyUsage(PropertyUsage propertyUsage) {
this.animal = animal;
}
@ManyToOne
@JoinColumn(name="ANIMAL_ID", referencedColumnName="ID")
public PropertyUsage getAnimal() {
return animal;
}
}
In the @OneToMany annotation of your getAssociatedBreeds() method you set the fetch property to EAGER. Change this to LAZY. This will cause your associated elements not to be loaded until you actually access them.