[英]How to add a data to mysql table via Hibernate(one-to-Many)
我有2個mysql表:帳戶和郵件。 帳戶由ID和用戶數據組成。 郵件包含id,user_id,數據和外鍵user_id-> Accounts.key(fk_Users)
如何通過休眠為當前user_id添加數據並向其中獲取數據? 我嘗試了一些來自Google的示例,但它們創建了多余的表或外鍵,我不喜歡它。
帳戶類別:
public class Account {
private Integer id;
private Something userData;
@OneToMany(mappedBy="account", cascade=CascadeType.ALL);
private Set<Mail> mails;
}
郵件類別:
public class Mail {
private Integer id;
private Othersomething data;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="user_id" , referencedColumnName="your foreign key here in Account table")
@Column(insertable=false, updatable=false)
private Account account;
}
然后當您在學習DAO課時
Query query =sess.createQuery("FROM Account");//hql or criteria here or native sql
List result = query.list();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.