I have an web application and I am using hibernate for data storage. I have two entity classes USERS AND FILES
the users can upload the files to the server so users entity is for storing user data and files entity for storing files. In users entity I write following code
@OneToMany(mappedBy="user")
@Column(nullable = true)
private Collection<Files> files=new ArrayList<Files>();
public Collection<Files> getFiles() {
return files;
}
public void setFiles(Collection<Files> files) {
this.files = files;
}
and in files entity class
@ManyToOne
@JoinColumn(name="uid")
private Users user;
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
whenever I run application the hibernate creates the tables fine with foreign key and all but when I try to save user object by session.save(u)
It wont get saved. The problem that I can think of is I am not adding any value to the files collection and also just saving the user object not the file object but this is required because it may not be necessary that a user will upload the file. He can just have the account and he may have any files on the server. How can I achieve this in hibernate.