我试图使用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_id中的board_id始终为null。 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, ?, ?, ?, ?, ?, ?, ?, ?, ?)
任何想法?
答案 0 :(得分:0)
要扩展@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
。
答案 1 :(得分:0)
快速提示:
将@GeneratedValue
用于@Id
字段时,最好避免在该字段上明确设置值。
@GeneratedValue
表示Hibernate或您选择的数据库将设置实体的ID(其中任何一个取决于您的GenerationType
),因此设置显式值或允许它为公开设置可能会导致抛出特定于Hibernate的StaleStateException
。
因此,您不应在构造函数中包含id
,也请删除:
public void setId(long id)
{
this.id = id;
}