gpt4 book ai didi

java - Joincolumn 返回空值

转载 作者:行者123 更新时间:2023-11-30 10:37:01 25 4
gpt4 key购买 nike

我正在尝试使用 @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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com