Hibernate @ManyToOne @JoinColumn is always null

非 Y 不嫁゛ 提交于 2021-02-08 04:12:11

问题


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

and second table:

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

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, ?, ?, ?, ?, ?, ?, ?, ?, ?)

any ideas?


回答1:


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

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




回答2:


Quick tip: When using @GeneratedValue for an @Id field, it's best to avoid explicitly setting a value on that field.

@GeneratedValue means that either Hibernate or your database of choice will set the entity's id(either of which depends on your GenerationType), so setting an explicit value or allowing it to be publicly set possibly will result in throwing a Hibernate-specific StaleStateException.

So you should not include id in your constructor and also remove:

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


来源:https://stackoverflow.com/questions/26876137/hibernate-manytoone-joincolumn-is-always-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!