0
のオブジェクトtopic_id
をtopic_name
でデータベースに保存して同時に検出する方法はありますか?Hibernate:リンクされたフィールドを検出してオブジェクトを保存する
public void updateResult(String topic_name, int userId) {
Session session = sessionFactory.getCurrentSession();
Beginner bgn = new Beginner();
bgn.setUserId(userId);
bgn.setScore(100);
// how to detect `topic_id` here by `topic_name` from the `grammar.topics`
bgn.setTopic_id(...);
session.save(bgn);
}
@Entity
@Table(name = "beginner", uniqueConstraints =
{@UniqueConstraint(columnNames = {"user_id", "topic_id"})})
public class Beginner implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "user_id", nullable = false)
private int user_id;
@Id
@Column(name = "topic_id", nullable = false)
private int topic_id;
@Column(name = "score", nullable = false)
private int score;
//getters&setters