java - Hibernate @ManyToOne @JoinColumn 始终为 null

标签 java spring hibernate one-to-many h2

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

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

但我无法让它工作。 item 表中的 board_id 始终为空。 Hibernate 输出看起来很奇怪:

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的评论,您需要在持久之前设置双方的关系。

// 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

关于java - Hibernate @ManyToOne @JoinColumn 始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26876137/

相关文章:

java - 哪个方面/拦截器处理@Transactional注释

spring - Spring Integration DSL 的错误处理中不包含消息 header

java - 使用 spring boot 启动器的更好方法

spring - Hibernate 5 与 Spring JTA

Java - 将对象的 ArrayList 传递给使用对象实现的接口(interface)的 ArrayList 的函数

Java JPA "Error compiling the query"当它使用枚举时

java - 与Hibernate结合使用时,设置C3P0参数值的最佳方法是什么?

java - hibernate/jpa 提示 "flush during cascade"

java - 重构大型 JSON 多次调用

java - SwingUtilities.InvokeLater 和 ButtonListeners