[英]How to add a data to mysql table via Hibernate(one-to-Many)
I have 2 mysql tables: Accounts and Mails. 我有2个mysql表:帐户和邮件。 Accounts consist of id and user data.
帐户由ID和用户数据组成。 Mails consist of id, user_id, data and foreign key user_id -> Accounts.key (fk_Users)
邮件包含id,user_id,数据和外键user_id-> Accounts.key(fk_Users)
How I can add and get data to Mails table for current user_id via hibernate? 如何通过休眠为当前user_id添加数据并向其中获取数据? I tried some examples from google, but they create excess tables or foreign keys and I don't like it.
我尝试了一些来自Google的示例,但它们创建了多余的表或外键,我不喜欢它。
Account class: 帐户类别:
public class Account {
private Integer id;
private Something userData;
@OneToMany(mappedBy="account", cascade=CascadeType.ALL);
private Set<Mail> mails;
}
Mail class: 邮件类别:
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;
}
And then when you are retrieving in your DAO class 然后当您在学习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.