4

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.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Limpep
  • 499
  • 3
  • 11
  • 22
  • What does that mean? A JoinColumn annotation doesn't "return" anything. It simply marks a field as having a join column to another table. A JPA provider would issue SQL that you could look at. Perhaps when you do you find your answer ... – Neil Stockton Oct 28 '16 at 10:02
  • @NeilStockton Well doesn't `@JoinColumn` create a foreign key to the referenced table? – Limpep Oct 28 '16 at 10:22
  • "It simply marks a field as having a join column to another table.". If you are going to say "it is retrieved as null" please at least POST YOUR CODE to retrieve the object(s) and mark on the transaction and where you say it is null in that code. That gives a basis for comment. Some mapping code without SQL/DDL doesn't – Neil Stockton Oct 28 '16 at 10:24
  • @NeilStockton ok thanks, so what would i need to do to be able to set the value of blender_id to the same one as the ingredients? thanks – Limpep Oct 28 '16 at 10:27

2 Answers2

1

try with just

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "blender_id")
private Blender blender;
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • that didn't work. Still getting null SELECT * FROM INGREDIENTS; ID FRUIT ICE JUICE BLENDER_ID 1 3 1 4 null – Limpep Oct 28 '16 at 10:08
0
@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.

0gam
  • 1,343
  • 1
  • 9
  • 21
  • that didn't work either still returning null on `blender_id` in the `ingredients table` I am not sure what I am doing wrong here. – Limpep Oct 28 '16 at 12:04
  • complete joinColumn. after you detail see your code. – 0gam Oct 28 '16 at 12:13
  • I have tried the following with now luck ` @OneToMany(mappedBy = "blender", cascade = { CascadeType.ALL }) private List ingredients;` and ` @ManyToOne() @JoinColumn(name = "blender_id") private Blender blender;` – Limpep Oct 28 '16 at 13:11
  • Ok. what do you want query? – 0gam Oct 28 '16 at 13:16
  • I want to be able to to ingredients to be associated with a blender, so when I search for a blender with an id i get back the ingredients associated with that blender. – Limpep Oct 28 '16 at 13:38
  • Here is my full code please take a look thanks https://www.dropbox.com/s/m9p0b1e1swt2d84/IgtJava.zip?dl=0 – Limpep Oct 28 '16 at 14:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126918/discussion-between-ergun-polat-and-byeon0gam). – Limpep Oct 28 '16 at 14:44