Reputation: 499
I am trying to join a column using the @JoinColumn
annotation but my column is always returning a null and I am not sure why.
@Entity
public class Blender implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "blender_id")
private int id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "blender", fetch = FetchType.EAGER)
private List<Ingredients> ingredients;
private Status status;
private String type;
public Blender() {
}
public Blender(List<Ingredients> ingredients) {
this.ingredients = ingredients;
}
public List<Ingredients> getIngredients() {
return ingredients;
}
public void setIngredients(List<Ingredients> ingredients) {
this.ingredients = ingredients;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public int getId() {
return id;
}
@Override
public String toString() {
String result = String.format(
"Blender[id=%d, type=%s, status=%s]%n",id,type,status);
if(ingredients!=null){
for (Ingredients ingredient: ingredients) {
result += String.format(
"ingredients[id=%d,fruit=%s,juice=%s,ice=%s]%n",
ingredient.getId(),
ingredient.getFruit(),
ingredient.getJuice(),
ingredient.getIce());
}
}
return result;
}
}
and Ingredients
@Entity
public class Ingredients implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private int fruit;
private int juice;
private int ice;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(columnDefinition="integer", name = "blender_id")
private Blender blender;
public Ingredients() {
}
public Long getId() {
return id;
}
public int getFruit() {
return fruit;
}
public void setFruit(int fruit) {
this.fruit = fruit;
}
public int getJuice() {
return juice;
}
public void setJuice(int juice) {
this.juice = juice;
}
public int getIce() {
return ice;
}
public void setIce(int ice) {
this.ice = ice;
}
public Blender getBlender() {
return blender;
}
public void setBlender(Blender blender) {
this.blender = blender;
}
@Override
public String toString() {
return "Ingredients{" +
"id=" + id +
", fruit='" + fruit + '\'' +
", juice='" + juice + '\'' +
", ice='" + ice + '\'' +
'}';
}
}
@JoinColumn(columnDefinition="integer", name = "blender_id")
is returning null not sure why.
Upvotes: 4
Views: 8501
Reputation: 1423
@OneToMany(mappedBy = "association", cascade = { CascadeType.ALL })
private List<Company> company;
@ManyToOne
@JoinColumn(name = "association_id")
private Association association;
You can try this pattern.
Read for you.
How to Enable Lazy Loading in Hibernate Before moving further, it is important to recap the default behavior of lazy loading in case of using hibernate mappings vs annotations. The default behavior is to load ‘property values eagerly’ and to load ‘collections lazily’. Contrary to what you might remember if you have used plain Hibernate 2 (mapping files) before, where all references (including collections) are loaded eagerly by default. Also note that @OneToMany and @ManyToMany associations are defaulted to LAZY loading; and @OneToOne and @ManyToOne are defaulted to EAGER loading. This is important to remember to avoid any pitfall in future. To enable lazy loading explicitly you must use "fetch = FetchType.LAZY" on a association which you want to lazy load when you are using hibernate annotations. An example usage will look like this: @OneToMany( mappedBy = "category", fetch = FetchType.LAZY ) private Set products; Another attribute parallel to "FetchType.LAZY" is "FetchType.EAGER" which is just opposite to LAZY i.e. it will load association entity as well when owner entity is fetched first time. How Lazy Loading Works in Hibernate The simplest way that Hibernate can apply lazy load behavior upon your entities and associations is by providing a proxy implementation of them. Hibernate intercepts calls to the entity by substituting a proxy for it derived from the entity’s class. Where the requested information is missing, it will be loaded from the database before control is ceded to the parent entity’s implementation. Please note that when the association is represented as a collection class, then a wrapper (essentially a proxy for the collection, rather than for the entities that it contains) is created and substituted for the original collection. When you access this collection proxy then what you get inside returned proxy collection are not proxy entities; rather they are actual entities. You need not to put much pressure on understanding this concept because on runtime it hardly matters.
Upvotes: 0
Reputation: 11017
try with just
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "blender_id")
private Blender blender;
Upvotes: 1