I have 2 mysql tables: Accounts and Mails. Accounts consist of id and user data. Mails consist of id, user_id, data and foreign key user_id -> Accounts.key (fk_Users)
How I can add and get data to Mails table for current user_id via hibernate? I tried some examples from google, but they create excess tables or foreign keys and I don't like it.
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
Query query =sess.createQuery("FROM Account");//hql or criteria here or native sql
List result = query.list();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.