Answer the question
In order to leave comments, you need to log in
Is it possible to describe a one-to-many relationship with the ability to change a field with a foreign key?
The standard description of a one-to-many relationship using jpa looks something like this (for example)
public class User {
.......
@ManyToOne
@JoinColumn(name="group_id")
Group group;
.......
}
public class Group {
@OneToMany
Set<User> users;
}
user.setGroup( group );
public class User {
@ManyToOne
@JoinColumn(name="group_id")
long groupId;
}
user.setGroupId( id );
Answer the question
In order to leave comments, you need to log in
Even though Hibernate requires a Group object at the object level, it actually only needs the group_id to store it correctly in the database. You can create an empty / fake (not read from the database) Group instance and specify the group_id you need for it. After that, it can be safely passed to user.setGroup(group); If the number of groups in the System is limited and all of them are known in advance (the subtext of the question implies this), then Group instances with a filled ID field can be stored as constants.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question