BiDirectional relationship isn't setting relationship?
Consider the following two classes
public class ServerDAOHb implements Serializable {
....
@OneToMany(cascade = CascadeType.ALL, mappedBy = "server")
private Set<ChannelDAOHb> channels = new HashSet();
}
public class ChannelDAOHb implements Serializabl开发者_运维百科e {
...
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "SERVER_ID" /*, nullable = false*/)
private ServerDAOHb server;
}
Why is it that when I add a new channel to the Server (both not being saved yet), the Channel's server is still null? Even in the database the SERVER_ID of the channel is null. Strangely though with one server it still returns the channel.
Why isn't SERVER_ID automatically filled in? How can I get it to be filled in?
You need to set the server reference in the channel, as this is the owning side (think of the table having the foreign key as being the owning side). Just adding the channel to the set won't work.
精彩评论