3

注釈を使用して列を結合しようとしてい@JoinColumnますが、列が常に null を返し、その理由がわかりません。

@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;
 }
}

と成分

@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")理由がわかりません。

4

2 に答える 2

1

だけで試してください

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "blender_id")
private Blender blender;
于 2016-10-28T10:04:19.970 に答える