java - Joincolumn 返回空值

标签 java spring jpa spring-boot joincolumn

我正在尝试使用 @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") 返回 null 不知道为什么。

最佳答案

尝试一下

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "blender_id")
private Blender blender;

关于java - Joincolumn 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40301829/

相关文章:

java - EclipseLink/JPA 是否有用于修改生成的 SQL 的插件框架?

java - 使用 Java 从特定 URL 获取整个网页

Java Generics - 抽象类抽象函数覆盖

java - 如何通过传递泛型类型创建多个用途的函数

java - 注入(inject) Guice 的 DAO 应该是单例吗?

java - eclipselink 和 jpa : select id association with left join return "0" instead null

java - 获取股票代码的当前价格

spring - 处理 UserRedirectRequiredException(需要重定向才能获得用户批准)

java - 构建具有安全性的 Java 桌面应用程序的最佳方式

java - 关于jsf bean注入(inject)spring bean的一个问题