Reputation: 397
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.
Upvotes: 0
Views: 352
Reputation: 14413
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();
Upvotes: 1