3

我正在尝试使用@JoinColumn注释加入一列,但我的列总是返回空值,我不知道为什么。

@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 不知道为什么。

4

2 回答 2

1

尝试一下

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "blender_id")
private Blender blender;
于 2016-10-28T10:04:19.970 回答
0
@OneToMany(mappedBy = "association", cascade = { CascadeType.ALL })
private List<Company> company;

@ManyToOne
@JoinColumn(name = "association_id")
private Association association;

你可以试试这个模式。


为你阅读。

如何在 Hibernate 中启用延迟加载 在进一​​步讨论之前,重要的是回顾一下在使用 hibernate 映射与注释的情况下延迟加载的默认行为。默认行为是加载 'property values eagerly' 和加载 'collections lazily'。如果您之前使用过普通的 Hibernate 2(映射文件),您可能记得的相反,默认情况下,所有引用(包括集合)都被急切地加载。另请注意,@OneToMany 和 @ManyToMany 关联默认为 LAZY 加载;@OneToOne 和 @ManyToOne 默认为 EAGER 加载。记住这一点很重要,以避免将来出现任何陷阱。要显式启用延迟加载,您必须在使用休眠注释时要延迟加载的关联上使用“fetch = FetchType.LAZY”。示例用法如下所示: @OneToMany( mappedBy = "category", fetch = FetchType.LAZY ) private Set products; 与“FetchType.LAZY”平行的另一个属性是“FetchType.EAGER”,它与 LAZY 正好相反,即当第一次获取所有者实体时,它也会加载关联实体。Hibernate 中的延迟加载如何工作 Hibernate 可以将延迟加载行为应用于您的实体和关联的最简单方法是提供它们的代理实现。Hibernate 通过用派生自实体类的代理替换对实体的调用来拦截对实体的调用。如果缺少请求的信息,它将在控制权交给父实体的实现之前从数据库中加载。请注意,当关联表示为集合类时,然后创建一个包装器(本质上是集合的代理,而不是它包含的实体)并替换原始集合。当您访问此集合代理时,您在返回的代理集合中获得的不是代理实体;相反,它们是实际的实体。你不需要对理解这个概念施加太大的压力,因为在运行时它几乎不重要。

于 2016-10-28T11:52:06.920 回答