休眠@ManyToOne @JoinColumn始终为null [英] Hibernate @ManyToOne @JoinColumn is always null

查看:51
本文介绍了休眠@ManyToOne @JoinColumn始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用休眠在两个表之间实现一对多关系.这是我的代码:

I'm trying to implement One-to-Many relation between two tables using hibernate. Here is my code:

@Entity
public class Board
{
@Id
@Column(name = "board_id")
@GeneratedValue
private long id;

@Column
private String owner;

@Column
private String title;

@Column
private String refresh;

@Column
private Timestamp  createDate;

@Column
private Timestamp modifyDate;

@OneToMany(mappedBy="board", cascade=CascadeType.ALL)
private List<Item> items;

public long getId()
{
    return id;
}
public void setId(long id)
{
    this.id = id;
}

public String getOwner()
{
    return owner;
}
public void setOwner(String owner)
{
    this.owner = owner;
}

public String getTitle()
{
    return title;
}
public void setTitle(String title)
{
    this.title = title;
}

public String getRefresh()
{
    return refresh;
}
public void setRefresh(String refresh)
{
    this.refresh = refresh;
}

public Timestamp getCreateDate()
{
    return createDate;
}
public void setCreateDate(Timestamp createDate)
{
    this.createDate = createDate;
}

public Timestamp getModifyDate()
{
    return modifyDate;
}
public void setModifyDate(Timestamp modifyDate)
{
    this.modifyDate = modifyDate;
}

public List<Item> getItems()
{
    return items;
}
public void setItems(List<Item> items)
{
    this.items = items;
}
}

和第二张桌子:

@Entity
public class Item
{
public enum Type
{  
     link,
     image, 
     presentation;  
}

public enum JavaScript
{  
     enable,
     disable;  
}  

@Id
@Column
@GeneratedValue
private long id;

@ManyToOne(fetch = FetchType.EAGER)  
@JoinColumn(name = "board_id")  
private Board board; 

@Column
private Type type;

@Column(length = 10000)
private String link;

@Column
private String image;

@Column
private String presentation;

@Column
private String time;

@Column
private JavaScript javaScript;

@Column
private String first;

@Column
private String last;

@Transient
private MultipartFile imageFile;

@Transient
private MultipartFile presentationFile;

public long getId()
{
    return id;
}
public void setId(long id)
{
    this.id = id;
}

public Board getBoard()
{
    return board;
}
public void setBoard(Board board)
{
    this.board = board;
}

public Type getType()
{
    return type;
}
public void setType(Type type)
{
    this.type = type;
}

public String getLink()
{
    return link;
}
public void setLink(String link)
{
    this.link = link;
}

public String getImage()
{
    return image;
}
public void setImage(String image)
{
    this.image = image;
}
public String getPresentation()
{
    return presentation;
}
public void setPresentation(String presentation) 
{
    this.presentation = presentation;
}

public String getTime()
{
    return time;
}
public void setTime(String time)
{
    this.time = time;
}

public JavaScript getJavaScript()
{
    return javaScript;
}
public void setJavaScript(JavaScript javaScript)
{
    this.javaScript = javaScript;
}

public String getFirst()
{
    return first;
}
public void setFirst(String first)
{
    this.first = first;
}

public String getLast() 
{
    return last;
}
public void setLast(String last)
{
    this.last = last;
}

public MultipartFile getImageFile()
{
    return imageFile;
}
public void setImageFile(MultipartFile imageFile)
{
    this.imageFile = imageFile;
}

public MultipartFile getPresentationFile()
{
    return presentationFile;
}
public void setPresentationFile(MultipartFile presentationFile) 
{
    this.presentationFile = presentationFile;
}
}

但是我无法正常工作.board_id在项目表中始终为null.休眠输出看起来很奇怪:

but I can't get it working. board_id is always null in item table. Hibernate output looks strange:

Hibernate: insert into Board (board_id, createDate, modifyDate, owner, refresh, title) values (null, ?, ?, ?, ?, ?)
Hibernate: insert into Item (id, board_id, first, image, javaScript, last, link, presentation, time, type) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?)

有什么想法吗?

推荐答案

要扩展@Antoniossss的注释,需要在持久化之前设置双方的关系.

To expand on the comment by @Antoniossss, you need to set the relation on both sides before persisting.

// Create owning entity first
Board board = new Board();
// Create child entities
Item item1 = new Item();
item1.setBoard(board); // set relation on Item side
board.getItems().add(item1); // set relation on Board side

还请注意,立即初始化集合字段是一种好习惯,这样, Board#getItems()永远不会返回 null .

Also note that it is considered good practice to initialize collection fields immediately, so that Board#getItems() never returns null.

这篇关于休眠@ManyToOne @JoinColumn始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆